-
Notifications
You must be signed in to change notification settings - Fork 289
Improve hash functions: FNV-1a for strings, MurmurHash3 for ireps #8976
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
Open
tautschnig
wants to merge
1
commit into
diffblue:develop
Choose a base branch
from
tautschnig:change-hash-functions
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+154
−14
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| /*******************************************************************\ | ||
|
|
||
| Module: Unit tests for string_hash.h | ||
|
|
||
| Author: Diffblue Ltd. | ||
|
|
||
| \*******************************************************************/ | ||
|
|
||
| #include <util/string_hash.h> | ||
|
|
||
| #include <testing-utils/use_catch.h> | ||
|
|
||
| #include <set> | ||
| #include <string> | ||
|
|
||
| TEST_CASE( | ||
| "hash_string is deterministic for equal inputs", | ||
| "[core][util][string_hash]") | ||
| { | ||
| // Pure determinism: equal inputs map to equal hash values, no matter | ||
| // whether we go through the std::string overload or the const char * | ||
| // overload, and no matter how many times we compute it. | ||
| const std::string s = "main::1::x!0@1#1"; | ||
| REQUIRE(hash_string(s) == hash_string(s)); | ||
| REQUIRE(hash_string(s) == hash_string(s.c_str())); | ||
| REQUIRE(hash_string("") == hash_string(std::string{})); | ||
| } | ||
|
|
||
| TEST_CASE( | ||
| "hash_string distinguishes two-character strings that differ in one bit", | ||
| "[core][util][string_hash]") | ||
| { | ||
| // The classic worst-case for hash functions with poor avalanche: tiny | ||
| // inputs that differ in a single bit. FNV-1a with the Murmur fmix | ||
| // finalizer should map these to clearly distinct hash values. | ||
| CHECK(hash_string("a") != hash_string("b")); | ||
| CHECK(hash_string("ab") != hash_string("ac")); | ||
| CHECK(hash_string("ab") != hash_string("aa")); | ||
| CHECK(hash_string("ab") != hash_string("ba")); | ||
| } | ||
|
|
||
| TEST_CASE( | ||
| "hash_string distinguishes strings that are prefixes of one another", | ||
| "[core][util][string_hash]") | ||
| { | ||
| // CBMC produces lots of SSA-renamed symbol names that share long | ||
| // common prefixes (e.g., main::1::x!0@1#1, main::1::x!0@1#2). Mixing | ||
| // the length into the hash before processing the bytes ensures that | ||
| // a string and any of its prefixes hash to different values. | ||
| CHECK(hash_string("main::1::x") != hash_string("main::1::x!")); | ||
| CHECK(hash_string("main::1::x!0@1#1") != hash_string("main::1::x!0@1#11")); | ||
| CHECK(hash_string("") != hash_string("a")); | ||
| } | ||
|
|
||
| TEST_CASE( | ||
| "hash_string has acceptable distribution on CBMC-style symbol names", | ||
| "[core][util][string_hash]") | ||
| { | ||
| // Sanity check: hashing a small batch of CBMC-style SSA-renamed | ||
| // symbol names should produce no collisions at all in 64-bit (and | ||
| // very few in 32-bit). The previous djb2-variant hash would | ||
| // collide noticeably on inputs of this shape, which is what | ||
| // motivated this change. | ||
| std::set<std::size_t> seen; | ||
| std::size_t inserted = 0; | ||
| for(int frame = 0; frame < 16; ++frame) | ||
| { | ||
| for(int var = 0; var < 16; ++var) | ||
| { | ||
| for(int ssa = 0; ssa < 16; ++ssa) | ||
| { | ||
| const std::string name = "main::" + std::to_string(frame) + "::x" + | ||
| std::to_string(var) + "!0@1#" + | ||
| std::to_string(ssa); | ||
| seen.insert(hash_string(name)); | ||
| ++inserted; | ||
| } | ||
| } | ||
| } | ||
| // Allow a small margin so 32-bit builds (which truncate to 32 bits) | ||
| // are not flaky if a handful of collisions show up. | ||
| REQUIRE(seen.size() >= inserted * 95 / 100); | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.