Skip to content

Commit 9f16f10

Browse files
authored
Merge branch 'gcarreno:main' into main
2 parents df8a8be + 7a282a4 commit 9f16f10

File tree

9 files changed

+160
-18
lines changed

9 files changed

+160
-18
lines changed

entries/ghatem-fpc/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
## Usage
88
- -t flag to specify the thread-count (default reads the thread-count available on the CPU)
9+
10+
currently there are 3 configurations that can be compiled / run:
11+
- `HASHMOD`: uses modulus for hashing, least collisions
12+
- `HASHMULT`: alternative hashing, more collisions, faster on my PC, but seemingly slower on test PCs
13+
- `LEMIRE`: faster hash function calculation, most collisions it seems, yet the fastest on my PC
914

1015
## Hardware + Environment
1116
host:
@@ -233,3 +238,12 @@ a few performance improvements, and measurements as per gcarreno on a busy machi
233238
- using mORMot's `crc32c` function instead of the native `crc32`, time dropped to 3.8 seconds
234239
- I had removed my pre-allocated records implementation. restored it in the custom dictionary class, time dropped to 3.2 seconds
235240
- skipping a few chars that we don't need to bother with, no timing yet
241+
242+
## v.5 (2024-04-27)
243+
244+
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.
245+
246+
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.
247+
Can be tested with the HASHMULT build option
248+
249+
Finally, it seems choosing a dictionary size that is a prime number is also recommended: shaves 1 second out of 20 on my PC.

entries/ghatem-fpc/src/OneBRCproj.lpi

Lines changed: 112 additions & 1 deletion
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="3">
18+
<BuildModes Count="7">
1919
<Item1 Name="Default" Default="True"/>
2020
<Item2 Name="Debug">
2121
<CompilerOptions>
@@ -84,6 +84,117 @@
8484
</Other>
8585
</CompilerOptions>
8686
</Item3>
87+
<Item4 Name="Valgrind">
88+
<CompilerOptions>
89+
<Version Value="11"/>
90+
<PathDelim Value="\"/>
91+
<Target>
92+
<Filename Value="..\..\..\bin\ghatem"/>
93+
</Target>
94+
<SearchPaths>
95+
<IncludeFiles Value="$(ProjOutDir)"/>
96+
<UnitOutputDirectory Value="..\..\..\bin\lib\$(TargetCPU)-$(TargetOS)"/>
97+
</SearchPaths>
98+
<Linking>
99+
<Debugging>
100+
<DebugInfoType Value="dsDwarf3"/>
101+
<TrashVariables Value="True"/>
102+
<UseValgrind Value="True"/>
103+
<UseExternalDbgSyms Value="True"/>
104+
</Debugging>
105+
</Linking>
106+
<Other>
107+
<CustomOptions Value="-dDEBUG"/>
108+
</Other>
109+
</CompilerOptions>
110+
</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>
87198
</BuildModes>
88199
<PublishOptions>
89200
<Version Value="2"/>

entries/ghatem-fpc/src/onebrc.pas

Lines changed: 17 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 = 45007;
1515

1616
type
1717

@@ -60,7 +60,7 @@ TOneBRC = class
6060
FThreads: array of TThread;
6161
FStationsDicts: array of TMyDictionary;
6262

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

6565
public
6666
constructor Create (const aThreadCount: UInt16);
@@ -128,11 +128,26 @@ 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
139+
{$IFDEF HASHMULT}
140+
vDbl := aKey * cHashConst;
141+
vDbl := vDbl - Trunc (vDbl);
142+
vIdx := Trunc (vDbl * cDictSize);
143+
{$ENDIF}
144+
{$IFDEF LEMIRE}
145+
vIdx := aKey * cDictSize shr 32;
146+
{$ENDIF}
147+
{$IFDEF HASHMOD}
135148
vIdx := aKey mod cDictSize;
149+
{$ENDIF}
150+
136151
aFound := False;
137152

138153
if FHashes[vIdx] = aKey then begin

entries/gklark/src/wcontest.lpr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
Generics.Defaults;
1414

1515
const
16-
lineendinglength = 2;
16+
lineendinglength = 1;
1717
type
1818

1919
{ tdataitem }

entries/hgrosser/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@
33
**1 billion row Challenge entry**
44

55
## Version
6-
Version 1.60
6+
Version 1.61
77

88
## How to compile
99
The program was developed with FPC 3.2.2 and Lazarus 2.2.4
1010

11-
There is a new Conditional "noCR" which is neccessary, if the input file has no CR's
12-
1311
## How to start
1412
```
1513
Usage: <path to input file> <bit-width for hash-list (16..28)>
1614
Example: hgrosser measurements.txt 16
15+
- input file: must NOT have CR's (only LF's)
1716
- bit-width for hash-list: sets the size of the hash list, e.g. '16' => 65536 entries
1817
```
1918
There are no switches like `-i` etc, only 2 values.
@@ -40,3 +39,4 @@ To speed things up:
4039
- Version 1.50: hash-list optimized, small improvements in parsing the file
4140
- Version 1.51: small improvements in asm function
4241
- Version 1.60: hash-list optimized, some minor improvements, Conditional "noCR" added
42+
- Version 1.61: Conditional "noCR" constantely enabled => input files must NOT have CR's

entries/hgrosser/src/1brc.pas

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
{ $DEFINE XTEST} {please keep this UNDEFINED / compile a safe or a fast program}
66
{ $DEFINE XINLINE} {please keep this UNDEFINED / use INLINE for routines or not}
7-
{ $DEFINE noCR} {neccessary for input-files without CR's (only LF's)}
7+
{$DEFINE noCR} {neccessary for input-files without CR's (only LF's)}
88

99
{$IFDEF XTEST}
1010
{$R+} {$Q+} {$S+} {slow but safe}
@@ -21,7 +21,7 @@
2121
sysutils, strutils, math;
2222

2323
const
24-
M_version = '1.60'; {version number}
24+
M_version = '1.61'; {version number}
2525

2626
{------------------------------ Common routines: ------------------------------}
2727

@@ -520,10 +520,7 @@ procedure weatherClass.process_measurements;
520520
begin
521521
Inc(p2, pred(p1)); {p2:=position of 'LF'}
522522
{get cityname and temperature, without CR: }
523-
len := p2 - p1
524-
{$IFNDEF noCR}
525-
- 1;
526-
{$ENDIF} {'-1' skips the CR}
523+
len := p2 - p1 {$IFNDEF noCR} -1 {$ENDIF} ; {'-1' skips the CR}
527524
city[0] := chr(len);
528525
move(s[p1], city[1], len);
529526
{extract temperature: }

entries/ikelaiah/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ Iwan Kelaiah
136136
* Use `ShortString` whenever possible. This saves approx 20 seconds.
137137
* Pre-allocate initial size for dictionaries. saves approx 20 seconds.
138138

139+
* 1.10
140+
* Revision release - Sequential approach. 3-5 mins on my Inspiron 15 7510 laptop, around 2m55s (a little improvement on speed).
141+
* Reduced buffer for `TStreamReader` to `65536 * 2`. This saves approx 5 seconds.
142+
* Changed hashmap from `TGHashMapLP` (linear probing) to `TGHashMapQP` (quadratic probing). This saves approx 5 seconds.
143+
139144
## License
140145

141146
This project is licensed under the MIT License - see the LICENSE.md file for details

entries/ikelaiah/src/weatherstation.pas

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ TStat = record
3535

3636
type
3737
// Using this dictionary, now approx 4 mins faster than Generics.Collections.TDictionary
38-
TWeatherDictionaryLG = specialize TGHashMapLP<ShortString, PStat>;
38+
TWeatherDictionaryLG = specialize TGHashMapQP<ShortString, PStat>;
3939

4040
type
4141
// a type for storing valid lookup temperature
42-
TValidTemperatureDictionary = specialize TGHashMapLP<ShortString, int64>;
42+
TValidTemperatureDictionary = specialize TGHashMapQP<ShortString, int64>;
4343

4444
type
4545
// Create a class to encapsulate the temperature observations of each weather station.
@@ -322,7 +322,7 @@ procedure TWeatherStation.ReadMeasurements;
322322
// Open the file for reading
323323
fileStream := TFileStream.Create(self.fname, fmOpenRead);
324324
try
325-
streamReader := TStreamReader.Create(fileStream, 65536 * 32, False);
325+
streamReader := TStreamReader.Create(fileStream, 65536 * 2, False);
326326
try
327327
// Read and parse chunks of data until EOF -------------------------------
328328
while not streamReader.EOF do

entries/ocoddo/src/Project1.lpr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,12 @@ TStationSummary = record
200200
if P[C + 1] = '.' then //X.X
201201
begin
202202
T *= (U8(P[C]) - 48) * 10 + U8(P[C + 2]) - 48;
203-
C += 5; //X.XCRLF
203+
C += 4; //X.XLF
204204
end
205205
else //XX.X
206206
begin
207207
T *= (U8(P[C]) - 48) * 100 + (U8(P[C + 1]) - 48) * 10 + U8(P[C + 3]) - 48;
208-
C += 6; //XX.XCRLF
208+
C += 5; //XX.XLF
209209
end;
210210

211211
Add(P, NS, NE, T, ATemperatures, ACoordinator);

0 commit comments

Comments
 (0)