[stdlib] Faster hashing logic in string-keyed dicts (~x2 speed-up) #3427
+31
−8
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Faster
String.__hash__()
algorithm by not using the builtinhash
function, which uses SIMD underneath, and somehow it's slowing the hash logic down (maybe because unneeded data conversion ormemcpy
). The hashing logic was slowing down Mojo string-keyed Dicts compared with Python, and with this optimization it is way faster now.I'm now using a very simplified DJBX33A algorithm version only for the
String
struct. If the reviewer thinks the approach is valid, I can refine it.See #1747
Note: there is a less disruptive approach here: #3436. It should have the same x2 outcome but, unfortunately, it's failing when compiling, maybe because of this compiler issue #3437
Benchmark 1
Same benchmark used in this other optimization: #3071
Results (lower is better)
Before :
After:
This is roughly x2 speed-up in
Dict[String, X]
.Benchmark 2
Results (the last column is timing, lower is better)
Before:
After:
This means at least a x2 speed-up in my tests
Benchmark algorithm used:
My machine: