You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Use Fast-Mod algorithm for native EEHashTableBase #65778
in VTune I see div as the most expensive part (if I read it correctly):
It turns out to be a EEHashTable lookup:
And It does make sense since div is a very expensive operation, on some CPU its latency can be bigger than 100 cycles. On my Coffee Lake it's 26/6 while e.g. mul is 3/1 (Latency/rec.trput)
The managed Dictionary was optimized with "Fast Mod" algorithm where we pre-calculate a special multiplier every time a new bucket is added and then can avoid expensive idiv operation by using mul see dotnet/coreclr#27299 and #406
Quick demo:
publicstaticuintTest(uintx){uintresult=0;ulongcachedMul=GetFastModMultiplier(y);for(inti=0;i<x;i++){// result = i % x;result=FastMod(i,x,cachedMul);}returnresult;}publicstaticulongGetFastModMultiplier(uintdivisor)=>ulong.MaxValue/divisor+1;[MethodImpl(MethodImplOptions.AggressiveInlining)]publicstaticuintFastMod(uintvalue,uintdivisor,ulongmultiplier)=>(uint)(((((multiplier*value)>>32)+1)*divisor)>>32);
NOTE: the algorithm adds an overhead on dictionary expansion
NOTE2: CompareKeys is not inlined
Reacted by Yahor Sinkevich, nietras, SupinePandora43 and Paulus Pärssinen
If the number of buckets is a power of 2 then mod is just a masking operation. (Though depending on the distribution of dwHash values it may create overweighted buckets)
Using FastMod could be an easy way to speed up EEHashTableBase. A few thoughts:
FastMod has two components - divisor and multiplier. They must match thus needing a bit more care regarding races. Storing multiplier in the table, the same way we store length, could be sufficient.
Are we sure the real problem is not collisions?
Sometimes excessive hashing expenses is what we see, but the real problem is collisions.
Whether EEHashTableBase is a good choice for caching is another good question.
If scenario that we have here is similar to the type casting cache (i.e. the values can be relatively cheaply recomputed), then a solution similar to cast cache could fit better.
Activity
EgorBo commentedon Feb 23, 2022
cc @VSadov
jkotas commentedon Feb 23, 2022
I think this can use the same or very similar hashtable as casting https://github.com/dotnet/runtime/blob/main/src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/CastHelpers.cs
bartonjs commentedon Feb 23, 2022
If the number of buckets is a power of 2 then mod is just a masking operation. (Though depending on the distribution of
dwHashvalues it may create overweighted buckets)EgorBo commentedon Feb 23, 2022
I applied the FastMod locally and got 22% improvement for the benchmark - but I am not sure I do it in a race-condition free fashion.
VSadov commentedon Feb 23, 2022
Using FastMod could be an easy way to speed up EEHashTableBase. A few thoughts:
FastMod has two components - divisor and multiplier. They must match thus needing a bit more care regarding races. Storing multiplier in the table, the same way we store length, could be sufficient.
Are we sure the real problem is not collisions?
Sometimes excessive hashing expenses is what we see, but the real problem is collisions.
Whether EEHashTableBase is a good choice for caching is another good question.
If scenario that we have here is similar to the type casting cache (i.e. the values can be relatively cheaply recomputed), then a solution similar to cast cache could fit better.
EgorBo commentedon Feb 23, 2022
Prototype: EgorBo@c99fe02
the only problem that it assumes- they always do, DWORD is 32bitNumBucketsfits into 32bit (pretty normal) anddwHashI agree but that sounds like a more invasive change, while FastMod could also improve other EEHashTable users?