-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Reintroduce FNV hashing #9860
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
Merged
JanKrivanek
merged 7 commits into
dotnet:main
from
JanKrivanek:proto/stablestringhash-overloads
Mar 18, 2024
Merged
Reintroduce FNV hashing #9860
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
27b7ff1
Revert "Revert "Shorten UTD marker file (#9387)" except Microsoft.Com…
JanKrivanek 5269cd9
Make FNV hash compatible across endianness
JanKrivanek c81a652
Add StableStringHash intrinsic function overloads
JanKrivanek 3bbaf98
Put StringTools functions references behind changewave
JanKrivanek 668b5cf
Prevent StableStringHash inlining
JanKrivanek d247337
Move the changewave description to proper section
JanKrivanek 1afd7ff
Merge branch 'main' into proto/stablestringhash-overloads
JanKrivanek 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
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,145 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Runtime.InteropServices; | ||
| using System; | ||
|
|
||
| namespace Microsoft.NET.StringTools | ||
| { | ||
| /// <summary> | ||
| /// Fowler/Noll/Vo hashing. | ||
| /// </summary> | ||
| public static class FowlerNollVo1aHash | ||
| { | ||
| // Fowler/Noll/Vo hashing. | ||
| // http://www.isthe.com/chongo/tech/comp/fnv/ | ||
| // https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#FNV-1a_hash | ||
| // http://www.isthe.com/chongo/src/fnv/hash_32a.c | ||
|
|
||
| // 32 bit FNV prime and offset basis for FNV-1a. | ||
| private const uint fnvPrimeA32Bit = 16777619; | ||
| private const uint fnvOffsetBasisA32Bit = 2166136261; | ||
|
|
||
| // 64 bit FNV prime and offset basis for FNV-1a. | ||
| private const long fnvPrimeA64Bit = 1099511628211; | ||
| private const long fnvOffsetBasisA64Bit = unchecked((long)14695981039346656037); | ||
|
|
||
| /// <summary> | ||
| /// Computes 32 bit Fowler/Noll/Vo-1a hash of a string (regardless of encoding). | ||
| /// </summary> | ||
| /// <param name="text">String to be hashed.</param> | ||
| /// <returns>32 bit signed hash</returns> | ||
| public static int ComputeHash32(string text) | ||
| { | ||
| uint hash = fnvOffsetBasisA32Bit; | ||
|
|
||
| unchecked | ||
| { | ||
| for (int i = 0; i < text.Length; i++) | ||
| { | ||
| char ch = text[i]; | ||
| byte b = (byte)ch; | ||
| hash ^= b; | ||
| hash *= fnvPrimeA32Bit; | ||
|
|
||
| b = (byte)(ch >> 8); | ||
| hash ^= b; | ||
| hash *= fnvPrimeA32Bit; | ||
| } | ||
| } | ||
|
|
||
| return unchecked((int)hash); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Computes 32 bit Fowler/Noll/Vo-1a inspired hash of a string. | ||
| /// The hashing algorithm process the data by the whole 16bit chars, instead of by bytes. | ||
| /// this speeds up the hashing process almost by 2x, while not significantly increasing collisions rate. | ||
| /// Analysis: https://github.com/KirillOsenkov/MSBuildStructuredLog/wiki/String-Hashing#faster-fnv-1a | ||
| /// </summary> | ||
| /// <param name="text">String to be hashed.</param> | ||
| /// <returns>32 bit unsigned hash</returns> | ||
| public static int ComputeHash32Fast(string text) | ||
| { | ||
| uint hash = fnvOffsetBasisA32Bit; | ||
|
|
||
| unchecked | ||
| { | ||
| for (int i = 0; i < text.Length; i++) | ||
| { | ||
| char ch = text[i]; | ||
|
|
||
| hash = (hash ^ ch) * fnvPrimeA32Bit; | ||
| } | ||
| } | ||
|
|
||
| return unchecked((int)hash); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Computes 64 bit Fowler/Noll/Vo-1a inspired hash of a string. | ||
| /// The hashing algorithm process the data by the whole 16bit chars, instead of by bytes. | ||
| /// this speeds up the hashing process almost by 2x, while not significantly increasing collisions rate. | ||
| /// Analysis: https://github.com/KirillOsenkov/MSBuildStructuredLog/wiki/String-Hashing#faster-fnv-1a | ||
| /// </summary> | ||
| /// <param name="text">String to be hashed.</param> | ||
| /// <returns>64 bit unsigned hash</returns> | ||
| public static long ComputeHash64Fast(string text) | ||
| { | ||
| long hash = fnvOffsetBasisA64Bit; | ||
|
|
||
| unchecked | ||
| { | ||
| for (int i = 0; i < text.Length; i++) | ||
| { | ||
| char ch = text[i]; | ||
|
|
||
| hash = (hash ^ ch) * fnvPrimeA64Bit; | ||
| } | ||
| } | ||
|
|
||
| return hash; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Computes 64 bit Fowler/Noll/Vo-1a hash of a string (regardless of encoding). | ||
| /// </summary> | ||
| /// <param name="text">String to be hashed.</param> | ||
| /// <returns>64 bit unsigned hash</returns> | ||
| public static long ComputeHash64(string text) | ||
| { | ||
| long hash = fnvOffsetBasisA64Bit; | ||
|
|
||
| unchecked | ||
| { | ||
| for (int i = 0; i < text.Length; i++) | ||
| { | ||
| char ch = text[i]; | ||
| byte b = (byte)ch; | ||
| hash ^= b; | ||
| hash *= fnvPrimeA64Bit; | ||
|
|
||
| b = (byte)(ch >> 8); | ||
| hash ^= b; | ||
| hash *= fnvPrimeA64Bit; | ||
| } | ||
| } | ||
|
|
||
| return hash; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Combines two 64 bit hashes generated by <see cref="FowlerNollVo1aHash"/> class into one. | ||
| /// </summary> | ||
| /// <param name="left">First hash value to be combined.</param> | ||
| /// <param name="right">Second hash value to be combined.</param> | ||
| /// <returns></returns> | ||
| public static long Combine64(long left, long right) | ||
| { | ||
| unchecked | ||
| { | ||
| return (left ^ right) * fnvPrimeA64Bit; | ||
| } | ||
| } | ||
| } | ||
| } |
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.