Skip to content

Commit 5bcccda

Browse files
committed
improve hash function
1 parent 61faa8b commit 5bcccda

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

entries/ghatem-fpc/src/onebrc.pas

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ interface
1111
function RoundExDouble(const ATemp: Double): Double; inline;
1212

1313
const
14-
cDictSize: Integer = 45000;
14+
cDictSize: Integer = 45003;
1515

1616
type
1717

@@ -128,11 +128,18 @@ function Compare(AList: TStringList; AIndex1, AIndex2: Integer): Integer;
128128

129129
{ TMyDictionary }
130130

131+
const
132+
cHashConst: Double = (Sqrt(5) - 1) / 2;
133+
131134
procedure TMyDictionary.InternalFind(const aKey: Cardinal; out aFound: Boolean; out aIndex: Integer);
132135
var vIdx: Integer;
136+
vDbl: Double;
133137
vOffset: Integer;
134138
begin
135-
vIdx := aKey mod cDictSize;
139+
vDbl := aKey * cHashConst;
140+
vDbl := vDbl - Trunc (vDbl);
141+
vIdx := Trunc (vDbl * cDictSize);
142+
136143
aFound := False;
137144

138145
if FHashes[vIdx] = aKey then begin

0 commit comments

Comments
 (0)