Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bloom/internal/bitvec/bitvec.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ pub fn BitVector::unmarshal_bytes(bytes : Bytes) -> BitVector {
let words_needed = words_needed(length)
let data = Array::make(words_needed, 0UL)
for i in 0..<words_needed {
data[i] = bytes[12 + i:20 + i].to_uint64_le()
data[i] = bytes[12 + i*8:20 + i*8].to_uint64_le()
}
Comment on lines 373 to 376

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While the fix is correct, this loop can be made more readable and slightly more efficient by avoiding the multiplication on each iteration. Using an offset variable that is incremented by the word size makes the logic clearer.

For example:

  let mut offset = 12
  for i in 0..<words_needed {
    data[i] = bytes[offset:offset + 8].to_uint64_le()
    offset += 8
  }

As a follow-up, you might consider defining constants for the magic numbers used in serialization (e.g., header size 12, word size 8) to improve maintainability.

{ length, tail_mask, data }
}
Expand Down
9 changes: 9 additions & 0 deletions bloom/internal/bitvec/bitvec_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,12 @@ test "serialization_roundtrip" {
let deserialized = BitVector::from_string(serialized)
assert_eq(original.to_string(), deserialized.to_string())
}

test "marshal_unmarshal_bytes_roundtrip_multiword" {
let original = BitVector::ones(128)
// Modify the second word so the bug cannot be masked by identical bytes
original.clear(64)
let serialized = original.marshal_bytes()
let deserialized = BitVector::unmarshal_bytes(serialized)
assert_eq(original.equal(deserialized), true)
}