Skip to content

Commit

Permalink
Small aesthetic improvement to the FNV implementation
Browse files Browse the repository at this point in the history
An improvement to the FNV implementation that calculates a bitmask using
a power of two minus one instead of hardcoding them.

    2^32-1 == 0xffffffff
  • Loading branch information
brandur committed Jul 2, 2024
1 parent 91f966a commit 2b2a3ed
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 16 deletions.
10 changes: 2 additions & 8 deletions lib/fnv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,18 @@ module River
module FNV
def self.fnv1_hash(str, size:)
hash = OFFSET_BASIS.fetch(size)
mask = MASK.fetch(size)
mask = (2**size-1).to_int # creates a mask of 1s of `size` bits long like 0xffffffff
prime = PRIME.fetch(size)

str.each_byte do |byte|
hash *= prime
hash &= mask # take lower N bits of multiplication product
hash &= mask
hash ^= byte
end

hash
end

MASK = {
32 => 0xffffffff, # mask 32 bits long
64 => 0xffffffffffffffff # mask 64 bits long
}.freeze
private_constant :MASK

OFFSET_BASIS = {
32 => 0x811c9dc5,
64 => 0xcbf29ce484222325
Expand Down
8 changes: 0 additions & 8 deletions spec/fnv_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@
end
end
end

# Just a sanity check to make sure the number of 0xffs is right.
describe "MASK" do
it "contains masks equal to integer limits" do
expect(River::FNV.const_get(:MASK)[32]).to eq(4_294_967_295)
expect(River::FNV.const_get(:MASK)[64]).to eq(18_446_744_073_709_551_615)
end
end
end

#
Expand Down

0 comments on commit 2b2a3ed

Please sign in to comment.