Skip to content

Commit 8f3e856

Browse files
authored
Merge pull request #141 from georges-hatem/main
static arrays are faster? cleanup unnecessary code
2 parents 9ec35d2 + 34486d9 commit 8f3e856

2 files changed

Lines changed: 7 additions & 115 deletions

File tree

entries/ghatem-fpc/src/OneBRCproj.lpi

Lines changed: 1 addition & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<UseAppBundle Value="False"/>
1616
<ResourceType Value="res"/>
1717
</General>
18-
<BuildModes Count="7">
18+
<BuildModes Count="4">
1919
<Item1 Name="Default" Default="True"/>
2020
<Item2 Name="Debug">
2121
<CompilerOptions>
@@ -108,93 +108,6 @@
108108
</Other>
109109
</CompilerOptions>
110110
</Item4>
111-
<Item5 Name="HashMult">
112-
<CompilerOptions>
113-
<Version Value="11"/>
114-
<PathDelim Value="\"/>
115-
<Target>
116-
<Filename Value="..\..\..\bin\ghatem"/>
117-
</Target>
118-
<SearchPaths>
119-
<IncludeFiles Value="$(ProjOutDir)"/>
120-
<UnitOutputDirectory Value="..\..\..\bin\lib\$(TargetCPU)-$(TargetOS)"/>
121-
</SearchPaths>
122-
<CodeGeneration>
123-
<SmartLinkUnit Value="True"/>
124-
<Optimizations>
125-
<OptimizationLevel Value="3"/>
126-
</Optimizations>
127-
</CodeGeneration>
128-
<Linking>
129-
<Debugging>
130-
<GenerateDebugInfo Value="False"/>
131-
<RunWithoutDebug Value="True"/>
132-
</Debugging>
133-
<LinkSmart Value="True"/>
134-
</Linking>
135-
<Other>
136-
<CustomOptions Value="-dRELEASE -dHASHMULT"/>
137-
</Other>
138-
</CompilerOptions>
139-
</Item5>
140-
<Item6 Name="HashMod">
141-
<CompilerOptions>
142-
<Version Value="11"/>
143-
<PathDelim Value="\"/>
144-
<Target>
145-
<Filename Value="..\..\..\bin\ghatem"/>
146-
</Target>
147-
<SearchPaths>
148-
<IncludeFiles Value="$(ProjOutDir)"/>
149-
<UnitOutputDirectory Value="..\..\..\bin\lib\$(TargetCPU)-$(TargetOS)"/>
150-
</SearchPaths>
151-
<CodeGeneration>
152-
<SmartLinkUnit Value="True"/>
153-
<Optimizations>
154-
<OptimizationLevel Value="3"/>
155-
</Optimizations>
156-
</CodeGeneration>
157-
<Linking>
158-
<Debugging>
159-
<GenerateDebugInfo Value="False"/>
160-
<RunWithoutDebug Value="True"/>
161-
</Debugging>
162-
<LinkSmart Value="True"/>
163-
</Linking>
164-
<Other>
165-
<CustomOptions Value="-dRELEASE -dHASHMOD"/>
166-
</Other>
167-
</CompilerOptions>
168-
</Item6>
169-
<Item7 Name="LEMIRE">
170-
<CompilerOptions>
171-
<Version Value="11"/>
172-
<PathDelim Value="\"/>
173-
<Target>
174-
<Filename Value="..\..\..\bin\ghatem"/>
175-
</Target>
176-
<SearchPaths>
177-
<IncludeFiles Value="$(ProjOutDir)"/>
178-
<UnitOutputDirectory Value="..\..\..\bin\lib\$(TargetCPU)-$(TargetOS)"/>
179-
</SearchPaths>
180-
<CodeGeneration>
181-
<SmartLinkUnit Value="True"/>
182-
<Optimizations>
183-
<OptimizationLevel Value="3"/>
184-
</Optimizations>
185-
</CodeGeneration>
186-
<Linking>
187-
<Debugging>
188-
<GenerateDebugInfo Value="False"/>
189-
<RunWithoutDebug Value="True"/>
190-
</Debugging>
191-
<LinkSmart Value="True"/>
192-
</Linking>
193-
<Other>
194-
<CustomOptions Value="-dRELEASE -dLEMIRE"/>
195-
</Other>
196-
</CompilerOptions>
197-
</Item7>
198111
</BuildModes>
199112
<PublishOptions>
200113
<Version Value="2"/>

entries/ghatem-fpc/src/onebrc.pas

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
interface
66

77
uses
8-
Classes, SysUtils, Generics.Collections,
8+
Classes, SysUtils,
99
mormot.core.os, mormot.core.base;
1010

1111
function RoundExDouble(const ATemp: Double): Double; inline;
@@ -25,18 +25,17 @@ function RoundExDouble(const ATemp: Double): Double; inline;
2525
Name: AnsiString;
2626
end;
2727
PStationData = ^TStationData;
28-
TStationsDict = specialize TDictionary<Cardinal, PStationData>;
2928

30-
TKeys = array of Cardinal;
31-
TValues = array of PStationData;
29+
TKeys = array [0..45006] of Cardinal;
30+
TValues = array [0..45006] of PStationData;
3231

3332
{ TMyDictionary }
3433

3534
TMyDictionary = class
3635
private
3736
FHashes: TKeys;
3837
FValues: TValues;
39-
FRecords: array of TStationData;
38+
FRecords: array [0..45006] of TStationData;
4039
procedure InternalFind(const aKey: Cardinal; out aFound: Boolean; out aIndex: Integer);
4140
public
4241
constructor Create;
@@ -128,27 +127,11 @@ function Compare(AList: TStringList; AIndex1, AIndex2: Integer): Integer;
128127

129128
{ TMyDictionary }
130129

131-
const
132-
cHashConst: Double = (Sqrt(5) - 1) / 2;
133-
134130
procedure TMyDictionary.InternalFind(const aKey: Cardinal; out aFound: Boolean; out aIndex: Integer);
135131
var vIdx: Integer;
136-
vDbl: Double;
137132
vOffset: Integer;
138133
begin
139-
{$IFDEF HASHMULT}
140-
vDbl := aKey * cHashConst;
141-
vDbl := vDbl - Trunc (vDbl);
142-
vIdx := Trunc (vDbl * cDictSize);
143-
{$ENDIF}
144-
{$IFDEF LEMIRE}
145134
vIdx := aKey * cDictSize shr 32;
146-
{$ENDIF}
147-
{$IFDEF HASHMOD}
148-
vIdx := aKey mod cDictSize;
149-
{$ENDIF}
150-
151-
aFound := False;
152135

153136
if FHashes[vIdx] = aKey then begin
154137
aIndex := vIdx;
@@ -170,8 +153,8 @@ procedure TMyDictionary.InternalFind(const aKey: Cardinal; out aFound: Boolean;
170153
aIndex := vIdx;
171154
aFound := True;
172155
break;
173-
end
174-
else if FHashes[vIdx] = 0 then begin
156+
end;
157+
if FHashes[vIdx] = 0 then begin
175158
// found empty bucket to use
176159
aIndex := vIdx;
177160
aFound := False;
@@ -185,10 +168,6 @@ constructor TMyDictionary.Create;
185168
var
186169
I: Integer;
187170
begin
188-
SetLength (FHashes, cDictSize);
189-
SetLength (FValues, cDictSize);
190-
SetLength (FRecords, cDictSize);
191-
192171
for I := 0 to cDictSize - 1 do begin
193172
FValues[I] := @FRecords[I];
194173
end;

0 commit comments

Comments
 (0)