From 5bcccda69d9cad3926395d22e4f8471d934c341b Mon Sep 17 00:00:00 2001 From: Georges Hatem Date: Fri, 26 Apr 2024 13:47:11 +0300 Subject: [PATCH] improve hash function --- entries/ghatem-fpc/src/onebrc.pas | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/entries/ghatem-fpc/src/onebrc.pas b/entries/ghatem-fpc/src/onebrc.pas index 254a5b2..a1afa94 100644 --- a/entries/ghatem-fpc/src/onebrc.pas +++ b/entries/ghatem-fpc/src/onebrc.pas @@ -11,7 +11,7 @@ interface function RoundExDouble(const ATemp: Double): Double; inline; const - cDictSize: Integer = 45000; + cDictSize: Integer = 45003; type @@ -128,11 +128,18 @@ function Compare(AList: TStringList; AIndex1, AIndex2: Integer): Integer; { TMyDictionary } +const + cHashConst: Double = (Sqrt(5) - 1) / 2; + procedure TMyDictionary.InternalFind(const aKey: Cardinal; out aFound: Boolean; out aIndex: Integer); var vIdx: Integer; + vDbl: Double; vOffset: Integer; begin - vIdx := aKey mod cDictSize; + vDbl := aKey * cHashConst; + vDbl := vDbl - Trunc (vDbl); + vIdx := Trunc (vDbl * cDictSize); + aFound := False; if FHashes[vIdx] = aKey then begin