Skip to content

Commit 08ae8d2

Browse files
authored
Merge pull request #121 from georges-hatem/main
back to pre-allocating contiguous records
2 parents 0b177b3 + d96fccc commit 08ae8d2

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

entries/ghatem-fpc/src/onebrc.pas

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,13 @@ function RoundExDouble(const ATemp: Double): Double; inline;
3535
TMyDictionary = class
3636
private
3737
FHashes: TKeys;
38-
FData : TValues;
38+
FValues: TValues;
39+
FRecords: array of TStationData;
3940
procedure InternalFind(const aKey: Cardinal; out aFound: Boolean; out aIndex: Integer);
4041
public
4142
constructor Create;
4243
property Keys: TKeys read FHashes;
43-
property Values: TValues read FData;
44+
property Values: TValues read FValues;
4445
function TryGetValue (const aKey: Cardinal; out aValue: PStationData): Boolean; inline;
4546
procedure Add (const aKey: Cardinal; const aValue: PStationData); inline;
4647
end;
@@ -94,9 +95,6 @@ TBRCThread = class (TThread)
9495

9596
implementation
9697

97-
uses
98-
CRC;
99-
10098

10199
const
102100
c0ascii: ShortInt = 48;
@@ -169,21 +167,24 @@ procedure TMyDictionary.InternalFind(const aKey: Cardinal; out aFound: Boolean;
169167
end;
170168

171169
constructor TMyDictionary.Create;
170+
var
171+
I: Integer;
172172
begin
173173
SetLength (FHashes, cDictSize);
174-
SetLength (FData, cDictSize);
174+
SetLength (FValues, cDictSize);
175+
SetLength (FRecords, cDictSize);
176+
177+
for I := 0 to cDictSize - 1 do begin
178+
FValues[I] := @FRecords[I];
179+
end;
175180
end;
176181

177182
function TMyDictionary.TryGetValue(const aKey: Cardinal; out aValue: PStationData): Boolean;
178183
var
179184
vIdx: Integer;
180185
begin
181186
InternalFind (aKey, Result, vIdx);
182-
183-
if Result then
184-
aValue := FData[vIdx]
185-
else
186-
aValue := nil;
187+
aValue := FValues[vIdx];
187188
end;
188189

189190
procedure TMyDictionary.Add(const aKey: Cardinal; const aValue: PStationData);
@@ -194,7 +195,7 @@ procedure TMyDictionary.Add(const aKey: Cardinal; const aValue: PStationData);
194195
InternalFind (aKey, vFound, vIdx);
195196
if not vFound then begin
196197
FHashes[vIdx] := aKey;
197-
FData[vIdx] := aValue;
198+
FValues[vIdx] := aValue;
198199
end
199200
else
200201
raise Exception.Create ('TMyDict: cannot add, duplicate key');
@@ -351,7 +352,6 @@ procedure TOneBRC.ProcessData (aThreadNb: UInt16; aStartIdx: Int64; aEndIdx: Int
351352
SetString(vStation, pAnsiChar(@FData[vLineStart]), vLenStationName);
352353

353354
// pre-allocated array of records instead of on-the-go allocation
354-
new(vData);
355355
vData^.Min := vTemp;
356356
vData^.Max := vTemp;
357357
vData^.Sum := vTemp;
@@ -420,8 +420,8 @@ procedure TOneBRC.GenerateOutput;
420420
try
421421
vStations.BeginUpdate;
422422
for vData in FStationsDicts[0].Values do begin
423-
// nil value means empty slot: skip
424-
if vData <> nil then
423+
// count = 0 means empty slot: skip
424+
if vData^.Count <> 0 then
425425
vStations.Add(vData^.Name);
426426
end;
427427
vStations.EndUpdate;

0 commit comments

Comments
 (0)