Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .changeset/some-lands-matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tanstack/db-ivm": patch
---

Fix bug where different numbers would hash to the same value. This caused distinct not to work properly.
19 changes: 15 additions & 4 deletions packages/db-ivm/src/hashing/murmur.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ export interface Hasher {
digest: () => number
}

// Allocate these once, outside the hot path
const buf = new ArrayBuffer(8)
// dv and u8 are 2 different views on the same buffer `buf`
const dv = new DataView(buf)
const u8 = new Uint8Array(buf)

/**
* This implementation of Murmur hash uses a random initial seed and random markers.
* This means that hashes aren't deterministic across app restarts.
Expand Down Expand Up @@ -82,10 +88,15 @@ export class MurmurHashStream implements Hasher {
}
return
case `number`:
this._writeByte(chunk & 0xff)
this._writeByte((chunk >>> 8) & 0xff)
this._writeByte((chunk >>> 16) & 0xff)
this._writeByte((chunk >>> 24) & 0xff)
dv.setFloat64(0, chunk, true) // fixed little-endian
this._writeByte(u8[0]!)
this._writeByte(u8[1]!)
this._writeByte(u8[2]!)
this._writeByte(u8[3]!)
this._writeByte(u8[4]!)
this._writeByte(u8[5]!)
this._writeByte(u8[6]!)
this._writeByte(u8[7]!)
return
case `bigint`: {
let value = chunk
Expand Down
2 changes: 2 additions & 0 deletions packages/db-ivm/tests/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ describe(`hash`, () => {

// Same numbers should have same hash
expect(hash(42)).toBe(result1)
expect(hash(2.0)).not.toBe(hash(2.5))
expect(hash(3.14159)).toBe(result4)
})

it(`should hash booleans`, () => {
Expand Down
Loading