Skip to content

Commit

Permalink
Use bitwise operations instead of rem and div operations when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
farhadi committed Jan 9, 2024
1 parent f4ad1f2 commit 05ee274
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/cuckoo_filter.erl
Original file line number Diff line number Diff line change
Expand Up @@ -407,10 +407,10 @@ index_and_fingerprint(Hash, FingerprintSize) ->
{Index, Fingerprint}.

alt_index(Index, Fingerprint, NumBuckets, HashFunction) ->
Index bxor HashFunction(Fingerprint) rem NumBuckets.
Index bxor HashFunction(Fingerprint) band (NumBuckets - 1).

atomic_index(BitIndex) ->
BitIndex div 64 + 3.
BitIndex bsr 6 + 3.

insert_at_index(Filter, Index, Fingerprint) ->
Bucket = read_bucket(Index, Filter),
Expand Down Expand Up @@ -585,7 +585,7 @@ read_bucket(
BucketBitSize = BucketSize * FingerprintSize,
BitIndex = Index * BucketBitSize,
AtomicIndex = atomic_index(BitIndex),
SkipBits = BitIndex rem 64,
SkipBits = BitIndex band 63,
EndIndex = atomic_index(BitIndex + BucketBitSize - 1),
<<_:SkipBits, Bucket:BucketBitSize/bitstring, _/bitstring>> = <<
<<(atomics:get(Buckets, I)):64/big-unsigned-integer>>
Expand All @@ -606,7 +606,7 @@ update_in_bucket(
) ->
BitIndex = Index * BucketSize * FingerprintSize + SubIndex * FingerprintSize,
AtomicIndex = atomic_index(BitIndex),
SkipBits = BitIndex rem 64,
SkipBits = BitIndex band 63,
AtomicValue = atomics:get(Buckets, AtomicIndex),
case <<AtomicValue:64/big-unsigned-integer>> of
<<Prefix:SkipBits/bitstring, OldValue:FingerprintSize/big-unsigned-integer,
Expand Down

0 comments on commit 05ee274

Please sign in to comment.