Bnaya/objectbuffer

Impl hashCodeExternalValue without auxiliary arraybuffer

Bnaya opened this issue · 0 comments

Bnaya commented

Important note: need to be system endienss aware

As part of the hashmap impl, we have flow (lookup) that we need to hash value that is yet saved inside our main ArrayBuffer.
But out current hash function is working only on arraybuffer, so we have to first save the key in an intermediate arraybuffer so we can hash it.

That's very wasteful, and better be avoided.

export function hashCodeExternalValue(
capacity: number,
value: string | number
): number {
const ab = new ArrayBuffer(typeof value === "string" ? value.length * 3 : 8);
const uint8 = new Uint8Array(ab);
let keyBytesLength = ab.byteLength;
if (typeof value === "string") {
keyBytesLength = stringEncodeInto(new Uint8Array(ab), 0, value);
} else {
new Float64Array(ab)[0] = value;
}
return hashCodeInPlace(uint8, capacity, 0, keyBytesLength);
}