-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: areCausallyRelated optimisation using BitSets (#133)
- Loading branch information
Showing
6 changed files
with
251 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
BitSet is associated with each vertex and is used to store the indices of the vertices that are reachable. | ||
In other words, all the vertices causally before in the hashgraph. | ||
When processing in the topologically ordered manner, we set the BitSet of the vertex to the bitwise OR of the BitSet of its dependencies. | ||
Then, to check if two vertices are causally related, we check if the BitSet of the first vertex contains the index of the second vertex and vice-versa. | ||
Algorithm for more optimal causality check inspired by https://stackoverflow.com/a/78133041 | ||
*/ | ||
export class BitSet { | ||
private data: Uint32Array; | ||
|
||
constructor(size = 1) { | ||
// Always start with size 32 | ||
this.data = new Uint32Array(size); | ||
} | ||
|
||
clear(): void { | ||
this.data = new Uint32Array(this.data.length); | ||
} | ||
|
||
set(index: number, value: boolean): void { | ||
// (index / 32) | 0 is equivalent to Math.floor(index / 32) | ||
const byteIndex = (index / 32) | 0; | ||
const bitIndex = index % 32; | ||
// if value is false, and with all 1s except the bit at bitIndex | ||
if (value) this.data[byteIndex] |= 1 << bitIndex; | ||
else this.data[byteIndex] &= ~(1 << bitIndex); | ||
} | ||
|
||
get(index: number): boolean { | ||
// (index / 32) | 0 is equivalent to Math.floor(index / 32) | ||
const byteIndex = (index / 32) | 0; | ||
const bitIndex = index % 32; | ||
return (this.data[byteIndex] & (1 << bitIndex)) !== 0; | ||
} | ||
|
||
flip(index: number): void { | ||
// (index / 32) | 0 is equivalent to Math.floor(index / 32) | ||
const byteIndex = (index / 32) | 0; | ||
const bitIndex = index % 32; | ||
this.data[byteIndex] ^= 1 << bitIndex; | ||
} | ||
|
||
// AND two bitsets of the same size | ||
and(other: BitSet): BitSet { | ||
const result = new BitSet(this.data.length); | ||
for (let i = 0; i < this.data.length; i++) { | ||
result.data[i] = this.data[i] & other.data[i]; | ||
} | ||
return result; | ||
} | ||
|
||
// OR two bitsets of the same size | ||
or(other: BitSet): BitSet { | ||
const result = new BitSet(this.data.length); | ||
for (let i = 0; i < this.data.length; i++) { | ||
result.data[i] = this.data[i] | other.data[i]; | ||
} | ||
return result; | ||
} | ||
|
||
// XOR two bitsets of the same size | ||
xor(other: BitSet): BitSet { | ||
const result = new BitSet(this.data.length); | ||
for (let i = 0; i < this.data.length; i++) { | ||
result.data[i] = this.data[i] ^ other.data[i]; | ||
} | ||
return result; | ||
} | ||
|
||
not(): BitSet { | ||
const result = new BitSet(this.data.length * 32); | ||
for (let i = 0; i < this.data.length; i++) { | ||
result.data[i] = ~this.data[i]; | ||
} | ||
return result; | ||
} | ||
|
||
toString(): string { | ||
return Array.from(this.data) | ||
.reverse() | ||
.map((int) => int.toString(2).padStart(32, "0")) | ||
.join(""); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { beforeEach, describe, expect, test } from "vitest"; | ||
import { BitSet } from "../src/hashgraph/bitset.js"; | ||
|
||
describe("BitSet Test", () => { | ||
let bitset: BitSet; | ||
|
||
beforeEach(() => { | ||
// Bitset of size 64 | ||
bitset = new BitSet(2); | ||
}); | ||
|
||
test("Test: BitSet", () => { | ||
bitset.set(0, true); | ||
bitset.set(50, true); | ||
|
||
expect(bitset.get(0)).toBe(true); | ||
expect(bitset.get(49)).toBe(false); | ||
expect(bitset.get(50)).toBe(true); | ||
|
||
bitset.flip(49); | ||
bitset.flip(50); | ||
expect(bitset.get(49)).toBe(true); | ||
expect(bitset.get(50)).toBe(false); | ||
|
||
bitset.clear(); | ||
|
||
let other: BitSet = new BitSet(2); | ||
other.set(0, true); | ||
other = other.or(bitset); | ||
expect(other.get(0)).toBe(true); | ||
|
||
other.set(0, false); | ||
expect(other.get(0)).toBe(false); | ||
|
||
other = other.and(bitset); | ||
expect(other.get(0)).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import test from "node:test"; | ||
import { beforeEach, bench, describe } from "vitest"; | ||
import { AddWinsSet } from "../../crdt/src/cros/AddWinsSet/index.js"; | ||
import { | ||
type Hash, | ||
type TopologyObject, | ||
merge, | ||
newTopologyObject, | ||
} from "../src/index.js"; | ||
|
||
describe("AreCausallyDependent benchmark", async () => { | ||
const samples = 100000; | ||
const tests: Hash[][] = []; | ||
|
||
const obj1 = await newTopologyObject("peer1", new AddWinsSet<number>()); | ||
const obj2 = await newTopologyObject("peer2", new AddWinsSet<number>()); | ||
const obj3 = await newTopologyObject("peer3", new AddWinsSet<number>()); | ||
|
||
const cro1 = obj1.cro as AddWinsSet<number>; | ||
const cro2 = obj2.cro as AddWinsSet<number>; | ||
const cro3 = obj3.cro as AddWinsSet<number>; | ||
|
||
cro1.add(1); | ||
merge(obj2, obj1.hashGraph.getAllVertices()); | ||
|
||
cro1.add(1); | ||
cro1.remove(2); | ||
cro2.remove(2); | ||
cro2.add(2); | ||
|
||
merge(obj3, obj1.hashGraph.getAllVertices()); | ||
cro3.add(3); | ||
cro1.remove(1); | ||
|
||
merge(obj1, obj2.hashGraph.getAllVertices()); | ||
cro1.remove(3); | ||
cro2.remove(1); | ||
|
||
merge(obj1, obj2.hashGraph.getAllVertices()); | ||
merge(obj1, obj3.hashGraph.getAllVertices()); | ||
|
||
const vertices = obj1.hashGraph.getAllVertices(); | ||
for (let i = 0; i < samples; i++) { | ||
tests.push([ | ||
vertices[Math.floor(Math.random() * vertices.length)].hash, | ||
vertices[Math.floor(Math.random() * vertices.length)].hash, | ||
]); | ||
} | ||
|
||
bench("Causality check using BFS", async () => { | ||
const cro1 = obj1.cro as AddWinsSet<number>; | ||
|
||
for (let i = 0; i < samples; i++) { | ||
const result = obj1.hashGraph.areCausallyRelatedUsingBFS( | ||
tests[i][0], | ||
tests[i][1], | ||
); | ||
} | ||
}); | ||
|
||
bench("Causality check using Bitsets", async () => { | ||
const cro1 = obj1.cro as AddWinsSet<number>; | ||
|
||
for (let i = 0; i < samples; i++) { | ||
const result = obj1.hashGraph.areCausallyRelatedUsingBitsets( | ||
tests[i][0], | ||
tests[i][1], | ||
); | ||
} | ||
}); | ||
}); |