Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bytewords: replace match with precomputed perfect has function #224

Merged
merged 1 commit into from
Feb 6, 2025

Conversation

dspicher
Copy link
Owner

@dspicher dspicher commented Feb 6, 2025

The two giant match statements with 256 arms incur linear lookup cost. To avoid this, we have defined a simple family of hash functions linear in the first and last character of the encoded string (this lines up nicely with the fact that those two characters are identical for the default and minimal encodings). Exhaustive search has then yielded a perfect hash function (with no collisions) that only needs a domain of roughly double the theoretical minimum (which would be 256 elements).

Other families of hash functions might theoretically yield smaller domains, but we don't judge this to be a relevant optimization for now.

This design achieves the following goals:

  • Constant lookup time at runtime.

  • no_std compliance, i.e. no OnceLocks and no HashMaps.

  • No unsafe code.

The recently introduced decoding test [1] suggests performance improvements of 66%.

[1] bbf09e4

Copy link

codecov bot commented Feb 6, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.56%. Comparing base (bbf09e4) to head (a804fa3).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #224      +/-   ##
==========================================
- Coverage   98.92%   98.56%   -0.37%     
==========================================
  Files           5        5              
  Lines        1766     1251     -515     
==========================================
- Hits         1747     1233     -514     
+ Misses         19       18       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

The two giant match statements with 256 arms incur linear
lookup cost. To avoid this, we have defined a simple family
of hash functions linear in the first and last character of
the encoded string (this lines up nicely with the fact that
those two characters are identical for the default and minimal
encodings). Exhaustive search has then yielded a perfect
hash function (with no collisions) that only needs a domain
of roughly double the theoretical minimum (which would be
256 elements).

Other families of hash functions might theoretically yield
smaller domains, but we don't judge this to be a relevant
optimization for now.

This design achieves the following goals:

 - Constant lookup time at runtime.

 - `no_std` compliance, i.e. no `OnceLock`s and no
   `HashMap`s.

 - No `unsafe` code.

The recently introduced decoding test [1] suggests performance
improvements of 66%.

[1] bbf09e4
@dspicher dspicher merged commit c7fd1ae into master Feb 6, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant