From af56858b0c261d955bb6f0a6c32601111f2c536c Mon Sep 17 00:00:00 2001 From: Georges Hatem Date: Thu, 9 May 2024 01:27:23 +0300 Subject: [PATCH 1/4] no iterator / interim vars --- entries/ghatem-fpc/src/OneBRC.lpr | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/entries/ghatem-fpc/src/OneBRC.lpr b/entries/ghatem-fpc/src/OneBRC.lpr index cbf75af..5731b9d 100644 --- a/entries/ghatem-fpc/src/OneBRC.lpr +++ b/entries/ghatem-fpc/src/OneBRC.lpr @@ -563,13 +563,11 @@ function MyFormatInt (const aIn: SmallInt): AnsiString; inline; //--------------------------------------------------- procedure TOneBRC.GenerateOutput; -var vMean: Integer; - vStream: TStringStream; +var vStream: TStringStream; I, N: Int64; vData: PStationData; vHash: Cardinal; vStations: TStringList; - iStationName: AnsiString; vIdx: THashSize; vRes: Boolean; begin @@ -579,8 +577,8 @@ procedure TOneBRC.GenerateOutput; vStations.UseLocale := False; try vStations.BeginUpdate; - for iStationName in FDictionary.FStationNames do begin - vStations.Add(iStationName); + for I := 0 to N - 1 do begin + vStations.Add(FDictionary.FStationNames[I]); end; vStations.EndUpdate; vStations.CustomSort (@Compare); @@ -598,11 +596,9 @@ procedure TOneBRC.GenerateOutput; FDictionary.InternalFind (vHash, vRes, vIdx); vData := @FDictionary.FThreadData[0][FDictionary.FIndexes[vIdx]]; - vMean := RoundExInteger(vData^.Sum/vData^.Count/10); - vStream.WriteString( vStations[i] + '=' + MyFormatInt(vData^.Min) - + '/' + MyFormatInt(vMean) + + '/' + MyFormatInt(RoundExInteger(vData^.Sum/vData^.Count/10)) + '/' + MyFormatInt(vData^.Max) + ', ' ); Inc(I); From d8e6a94a3ad475825a61ec27a38fb77a1c56e123 Mon Sep 17 00:00:00 2001 From: Georges Hatem Date: Thu, 9 May 2024 01:44:48 +0300 Subject: [PATCH 2/4] undo some changes, problematic for 400 stations --- entries/ghatem-fpc/src/OneBRC.lpr | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/entries/ghatem-fpc/src/OneBRC.lpr b/entries/ghatem-fpc/src/OneBRC.lpr index 5731b9d..a791166 100644 --- a/entries/ghatem-fpc/src/OneBRC.lpr +++ b/entries/ghatem-fpc/src/OneBRC.lpr @@ -577,9 +577,11 @@ procedure TOneBRC.GenerateOutput; vStations.UseLocale := False; try vStations.BeginUpdate; - for I := 0 to N - 1 do begin - vStations.Add(FDictionary.FStationNames[I]); + for i := 0 to cNumStations - 1 do begin + if FDictionary.FStationNames[i] <> '' then + vStations.Add (FDictionary.FStationNames[i]); end; + vStations.EndUpdate; vStations.CustomSort (@Compare); From 5df50953dfb47edd4ddce6cc94f6a48ea06e510a Mon Sep 17 00:00:00 2001 From: Georges Hatem Date: Thu, 9 May 2024 01:58:40 +0300 Subject: [PATCH 3/4] reduce int sizes --- entries/ghatem-fpc/src/OneBRC.lpr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entries/ghatem-fpc/src/OneBRC.lpr b/entries/ghatem-fpc/src/OneBRC.lpr index a791166..d838112 100644 --- a/entries/ghatem-fpc/src/OneBRC.lpr +++ b/entries/ghatem-fpc/src/OneBRC.lpr @@ -564,7 +564,7 @@ function MyFormatInt (const aIn: SmallInt): AnsiString; inline; procedure TOneBRC.GenerateOutput; var vStream: TStringStream; - I, N: Int64; + I, N: Int32; vData: PStationData; vHash: Cardinal; vStations: TStringList; From 40a245b072e5d5718423e505dec15b5c1f4c310d Mon Sep 17 00:00:00 2001 From: Georges Hatem Date: Thu, 9 May 2024 02:15:08 +0300 Subject: [PATCH 4/4] unroll loop a bit --- entries/ghatem-fpc/src/OneBRC.lpr | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/entries/ghatem-fpc/src/OneBRC.lpr b/entries/ghatem-fpc/src/OneBRC.lpr index d838112..5b7486d 100644 --- a/entries/ghatem-fpc/src/OneBRC.lpr +++ b/entries/ghatem-fpc/src/OneBRC.lpr @@ -465,8 +465,21 @@ procedure TOneBRC.ProcessData (aThreadNb: TThreadCount; aStartIdx: Int64; aEndId Inc (I, 5); end; - while FData[i] <> #10 do begin - Inc (I); + // unroll a few seems to be improving? + if FData[i] <> #10 then begin + Inc (i); + if FData[i] <> #10 then begin + Inc (i); + if FData[i] <> #10 then begin + Inc (I); + if FData[i] <> #10 then begin + Inc (i); + while FData[i] <> #10 do begin + Inc (I); + end; + end; + end; + end; end; // new line parsed, process its contents