Skip to content

Commit 5257c04

Browse files
author
Alex Peck
committed
unroll
1 parent 32973c8 commit 5257c04

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

BitFaster.Caching/BitFaster.Caching.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFrameworks>netstandard2.0;netcoreapp3.1;net6.0</TargetFrameworks>
5-
<LangVersion>10.0</LangVersion>
5+
<LangVersion>11.0</LangVersion>
66
<Authors>Alex Peck</Authors>
77
<Company />
88
<Product>BitFaster.Caching</Product>

BitFaster.Caching/Lfu/CmSketchCore.cs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -186,24 +186,31 @@ private unsafe int EstimateFrequencyStd(T value)
186186

187187
private unsafe void IncrementStd(T value)
188188
{
189-
var index = stackalloc int[8];
190189
int blockHash = Spread(comparer.GetHashCode(value));
191190
int counterHash = Rehash(blockHash);
192191
int block = (blockHash & blockMask) << 3;
193192

194-
for (int i = 0; i < 4; i++)
195-
{
196-
int h = (int)((uint)counterHash >> (i << 3));
197-
index[i] = (h >> 1) & 15;
198-
int offset = h & 1;
199-
index[i + 4] = block + offset + (i << 1);
200-
}
193+
// Loop unrolling improves throughput by 10m ops/s
194+
int h0 = counterHash;
195+
int h1 = counterHash >>> 8;
196+
int h2 = counterHash >>> 16;
197+
int h3 = counterHash >>> 24;
198+
199+
int index0 = (h0 >>> 1) & 15;
200+
int index1 = (h1 >>> 1) & 15;
201+
int index2 = (h2 >>> 1) & 15;
202+
int index3 = (h3 >>> 1) & 15;
203+
204+
int slot0 = block + (h0 & 1);
205+
int slot1 = block + (h1 & 1) + 2;
206+
int slot2 = block + (h2 & 1) + 4;
207+
int slot3 = block + (h3 & 1) + 6;
201208

202209
bool added =
203-
IncrementAt(index[4], index[0])
204-
| IncrementAt(index[5], index[1])
205-
| IncrementAt(index[6], index[2])
206-
| IncrementAt(index[7], index[3]);
210+
IncrementAt(slot0, index0)
211+
| IncrementAt(slot1, index1)
212+
| IncrementAt(slot2, index2)
213+
| IncrementAt(slot3, index3);
207214

208215
if (added && (++size == sampleSize))
209216
{

0 commit comments

Comments
 (0)