Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: better topo dfs perf #482

Merged
merged 2 commits into from
Feb 19, 2025
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
21 changes: 13 additions & 8 deletions packages/object/src/hashgraph/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,16 +166,20 @@ export class HashGraph {

dfsTopologicalSortIterative(origin: Hash, subgraph: ObjectSet<Hash>): Hash[] {
const visited = new ObjectSet<Hash>();
const result: Hash[] = [];
const stack: Hash[] = [origin];
const result: Hash[] = Array(subgraph.size);
const stack: Hash[] = Array(subgraph.size);
const processing = new ObjectSet<Hash>();
let resultIndex = subgraph.size - 1;
let stackIndex = 0;
stack[stackIndex] = origin;

while (stack.length > 0) {
const node = stack[stack.length - 1];
while (resultIndex >= 0) {
const node = stack[stackIndex];

if (visited.has(node)) {
stack.pop();
result.push(node);
result[resultIndex] = node;
stackIndex--;
resultIndex--;
processing.delete(node);
continue;
}
Expand All @@ -188,13 +192,14 @@ export class HashGraph {
for (const neighbor of neighbors.sort()) {
if (processing.has(neighbor)) throw new Error("Graph contains a cycle!");
if (subgraph.has(neighbor) && !visited.has(neighbor)) {
stack.push(neighbor);
stackIndex++;
stack[stackIndex] = neighbor;
}
}
}
}

return result.reverse();
return result;
}

/* Topologically sort the vertices in the whole hashgraph or the past of a given vertex. */
Expand Down
1 change: 0 additions & 1 deletion packages/object/src/linearize/pairSemantics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,5 @@ export function linearizePairSemantics(
}
}
}

return result;
}
9 changes: 9 additions & 0 deletions packages/object/src/utils/objectSet.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
export class ObjectSet<T extends string | number | symbol> {
set: { [key in T]: boolean };
size: number;

constructor(iterable: Iterable<T> = []) {
this.set = {} as { [key in T]: boolean };
this.size = 0;
for (const item of iterable) {
this.set[item] = true;
this.size++;
}
}

add(item: T): void {
if (this.has(item)) return;

this.set[item] = true;
this.size++;
}

delete(item: T): void {
if (!this.has(item)) return;

delete this.set[item];
this.size--;
}

has(item: T): boolean {
Expand Down
4 changes: 3 additions & 1 deletion packages/object/tests/actiontypes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ describe("Test: ActionTypes (Nop and Swap)", () => {
addMul.add(5);
drp.merge(drp2.vertices);
drp2.merge(drp.vertices);

addMul.mul(5);
addMul.add(5);
addMul2.add(5);
Expand All @@ -99,8 +98,11 @@ describe("Test: ActionTypes (Nop and Swap)", () => {
expect(addMul2.query_value()).toBe(75);

addMul2.mul(2);
vi.setSystemTime(new Date(Date.UTC(1998, 11, 24)));
addMul2.add(2);
vi.setSystemTime(new Date(Date.UTC(1998, 11, 25)));
addMul.add(3);
vi.setSystemTime(new Date(Date.UTC(1998, 11, 26)));
addMul.mul(3);
drp.merge(drp2.vertices);
drp2.merge(drp.vertices);
Expand Down
Loading