Skip to content

Commit 1bbdf96

Browse files
authored
Merge pull request #163 from georges-hatem/main
micro-optimizations
2 parents 75b13b2 + 797c7b4 commit 1bbdf96

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

entries/ghatem-fpc/src/OneBRC.lpr

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -465,8 +465,21 @@ procedure TOneBRC.ProcessData (aThreadNb: TThreadCount; aStartIdx: Int64; aEndId
465465
Inc (I, 5);
466466
end;
467467

468-
while FData[i] <> #10 do begin
469-
Inc (I);
468+
// unroll a few seems to be improving?
469+
if FData[i] <> #10 then begin
470+
Inc (i);
471+
if FData[i] <> #10 then begin
472+
Inc (i);
473+
if FData[i] <> #10 then begin
474+
Inc (I);
475+
if FData[i] <> #10 then begin
476+
Inc (i);
477+
while FData[i] <> #10 do begin
478+
Inc (I);
479+
end;
480+
end;
481+
end;
482+
end;
470483
end;
471484

472485
// new line parsed, process its contents
@@ -563,13 +576,11 @@ function MyFormatInt (const aIn: SmallInt): AnsiString; inline;
563576
//---------------------------------------------------
564577

565578
procedure TOneBRC.GenerateOutput;
566-
var vMean: Integer;
567-
vStream: TStringStream;
568-
I, N: Int64;
579+
var vStream: TStringStream;
580+
I, N: Int32;
569581
vData: PStationData;
570582
vHash: Cardinal;
571583
vStations: TStringList;
572-
iStationName: AnsiString;
573584
vIdx: THashSize;
574585
vRes: Boolean;
575586
begin
@@ -579,9 +590,11 @@ procedure TOneBRC.GenerateOutput;
579590
vStations.UseLocale := False;
580591
try
581592
vStations.BeginUpdate;
582-
for iStationName in FDictionary.FStationNames do begin
583-
vStations.Add(iStationName);
593+
for i := 0 to cNumStations - 1 do begin
594+
if FDictionary.FStationNames[i] <> '' then
595+
vStations.Add (FDictionary.FStationNames[i]);
584596
end;
597+
585598
vStations.EndUpdate;
586599
vStations.CustomSort (@Compare);
587600

@@ -598,11 +611,9 @@ procedure TOneBRC.GenerateOutput;
598611
FDictionary.InternalFind (vHash, vRes, vIdx);
599612
vData := @FDictionary.FThreadData[0][FDictionary.FIndexes[vIdx]];
600613

601-
vMean := RoundExInteger(vData^.Sum/vData^.Count/10);
602-
603614
vStream.WriteString(
604615
vStations[i] + '=' + MyFormatInt(vData^.Min)
605-
+ '/' + MyFormatInt(vMean)
616+
+ '/' + MyFormatInt(RoundExInteger(vData^.Sum/vData^.Count/10))
606617
+ '/' + MyFormatInt(vData^.Max) + ', '
607618
);
608619
Inc(I);

0 commit comments

Comments
 (0)