Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions entries/ghatem-fpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,12 @@ a few performance improvements, and measurements as per gcarreno on a busy machi
- using mORMot's `crc32c` function instead of the native `crc32`, time dropped to 3.8 seconds
- I had removed my pre-allocated records implementation. restored it in the custom dictionary class, time dropped to 3.2 seconds
- skipping a few chars that we don't need to bother with, no timing yet

## v.5 (2024-04-27)

Various attempts at dictionary sizes, ranging from 45k to 95k. Even though larger dictionaries reduce collision tremendously, a dictionary of size 45k was still optimal.

Another trial with various hash functions, a simple modulus vs. a slightly more complex one: modulus is slower on my PC, remains to try on the test env.
Can be tested with the HASHMULT build option

Finally, it seems choosing a dictionary size that is a prime number is also recommended: shaves 1 second out of 20 on my PC.
31 changes: 30 additions & 1 deletion entries/ghatem-fpc/src/OneBRCproj.lpi
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<UseAppBundle Value="False"/>
<ResourceType Value="res"/>
</General>
<BuildModes Count="4">
<BuildModes Count="5">
<Item1 Name="Default" Default="True"/>
<Item2 Name="Debug">
<CompilerOptions>
Expand Down Expand Up @@ -108,6 +108,35 @@
</Other>
</CompilerOptions>
</Item4>
<Item5 Name="HashMult">
<CompilerOptions>
<Version Value="11"/>
<PathDelim Value="\"/>
<Target>
<Filename Value="..\..\..\bin\ghatem"/>
</Target>
<SearchPaths>
<IncludeFiles Value="$(ProjOutDir)"/>
<UnitOutputDirectory Value="..\..\..\bin\lib\$(TargetCPU)-$(TargetOS)"/>
</SearchPaths>
<CodeGeneration>
<SmartLinkUnit Value="True"/>
<Optimizations>
<OptimizationLevel Value="3"/>
</Optimizations>
</CodeGeneration>
<Linking>
<Debugging>
<GenerateDebugInfo Value="False"/>
<RunWithoutDebug Value="True"/>
</Debugging>
<LinkSmart Value="True"/>
</Linking>
<Other>
<CustomOptions Value="-dRELEASE -dHASHMULT"/>
</Other>
</CompilerOptions>
</Item5>
</BuildModes>
<PublishOptions>
<Version Value="2"/>
Expand Down
12 changes: 2 additions & 10 deletions entries/ghatem-fpc/src/OneBRCproj.lpr
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ TOneBRCApp = class(TCustomApplication)
private
FFileName: string;
FThreadCount: Integer;
FDictSize: Integer;
procedure RunOneBRC;
protected
procedure DoRun; override;
Expand All @@ -34,7 +33,7 @@ procedure TOneBRCApp.RunOneBRC;
var
vOneBRC: TOneBRC;
begin
vOneBRC := TOneBRC.Create (FThreadCount, FDictSize);
vOneBRC := TOneBRC.Create (FThreadCount);
try
try
vOneBRC.mORMotMMF(FFileName);
Expand Down Expand Up @@ -89,17 +88,15 @@ procedure TOneBRCApp.DoRun;
ErrorMsg: String;
begin
// quick check parameters
ErrorMsg:= CheckOptions(Format('%s%s%s%s%s:',[
ErrorMsg:= CheckOptions(Format('%s%s%s%s:',[
cShortOptHelp,
cShortOptThread,
cShortOptDictSize,
cShortOptVersion,
cShortOptInput
]),
[
cLongOptHelp,
cLongOptThread+':',
cLongOptDictSize+':',
cLongOptVersion,
cLongOptInput+':'
]
Expand Down Expand Up @@ -129,11 +126,6 @@ procedure TOneBRCApp.DoRun;
FThreadCount := StrToInt (GetOptionValue(cShortOptThread, cLongOptThread));
end;

FDictSize := 45003;
if HasOption(cShortOptDictSize, cLongOptDictSize) then begin
FDictSize := StrToInt (GetOptionValue(cShortOptDictSize, cLongOptDictSize));
end;

if HasOption(cShortOptInput, cLongOptInput) then begin
FFileName := GetOptionValue(
cShortOptInput,
Expand Down
2 changes: 0 additions & 2 deletions entries/ghatem-fpc/src/baseline.console.pas
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ interface
cLongOptInput = 'input-file';
cShortOptThread: Char = 't';
cLongOptThread = 'threads';
cShortOptDictSize: Char = 's';
cLongOptDictSize = 'size';
{$ELSE}
cOptionHelp: array of string = ['-h', '--help'];
cOptionVersion: array of string = ['-v', '--version'];
Expand Down
16 changes: 8 additions & 8 deletions entries/ghatem-fpc/src/onebrc.pas
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ interface

function RoundExDouble(const ATemp: Double): Double; inline;

{$WRITEABLECONST ON}
const
cDictSize: Integer = 45003;
{$WRITEABLECONST OFF}
cDictSize: Integer = 45007;

type

Expand Down Expand Up @@ -62,10 +60,10 @@ TOneBRC = class
FThreads: array of TThread;
FStationsDicts: array of TMyDictionary;

procedure ExtractLineData(const aStart: Int64; const aEnd: Int64; out aLength: ShortInt; out aTemp: SmallInt); inline;
procedure ExtractLineData(const aStart: Int64; const aEnd: Int64; out aLength: ShortInt; out aTemp: SmallInt);

public
constructor Create (const aThreadCount: UInt16; const aDictSize: Integer);
constructor Create (const aThreadCount: UInt16);
destructor Destroy; override;
function mORMotMMF (const afilename: string): Boolean;
procedure DispatchThreads;
Expand Down Expand Up @@ -138,9 +136,13 @@ procedure TMyDictionary.InternalFind(const aKey: Cardinal; out aFound: Boolean;
vDbl: Double;
vOffset: Integer;
begin
{$IFDEF HASHMULT}
vDbl := aKey * cHashConst;
vDbl := vDbl - Trunc (vDbl);
vIdx := Trunc (vDbl * cDictSize);
{$ELSE}
vIdx := aKey mod cDictSize;
{$ENDIF}

aFound := False;

Expand Down Expand Up @@ -251,11 +253,9 @@ procedure TOneBRC.ExtractLineData(const aStart: Int64; const aEnd: Int64; out aL

//---------------------------------------------------

constructor TOneBRC.Create (const aThreadCount: UInt16; const aDictSize: Integer);
constructor TOneBRC.Create (const aThreadCount: UInt16);
var I: UInt16;
begin
cDictSize := aDictSize;

FThreadCount := aThreadCount;
SetLength (FStationsDicts, aThreadCount);
SetLength (FThreads, aThreadCount);
Expand Down