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

feat: optimize compute vertex state #275

Merged
merged 2 commits into from
Dec 4, 2024
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
1 change: 1 addition & 0 deletions packages/object/src/hashgraph/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ export class HashGraph {
throw new Error("Vertex dependencies are empty");
}
if (hashes.length === 1) {
visited.add(hashes[0]);
return hashes[0];
}
let lca: Hash | undefined = hashes[0];
Expand Down
34 changes: 14 additions & 20 deletions packages/object/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,17 @@ export class DRPObject implements IDRPObject {

private _setState(vertex: Vertex) {
const subgraph: Set<Hash> = new Set();
const lca = this.hashGraph.lowestCommonAncestorMultipleVertices(
vertex.dependencies,
subgraph,
);
const linearizedOperations = this.hashGraph.linearizeOperations(
lca,
subgraph,
);
const lca =
vertex.dependencies.length === 1
? vertex.dependencies[0]
: this.hashGraph.lowestCommonAncestorMultipleVertices(
vertex.dependencies,
subgraph,
);
const linearizedOperations =
vertex.dependencies.length === 1
? []
: this.hashGraph.linearizeOperations(lca, subgraph);

const drp = Object.create(
Object.getPrototypeOf(this.originalDRP),
Expand All @@ -196,22 +199,13 @@ export class DRPObject implements IDRPObject {
throw new Error("State is undefined");
}

const state = Object.create(
Object.getPrototypeOf(fetchedState),
Object.getOwnPropertyDescriptors(structuredClone(fetchedState)),
).state;
const state = structuredClone(fetchedState);

for (const [key, value] of state.entries()) {
for (const [key, value] of state.state) {
drp[key] = value;
}
d-roak marked this conversation as resolved.
Show resolved Hide resolved

let applyIdx = 1;
if (lca === HashGraph.rootHash) {
applyIdx = 0;
}

for (; applyIdx < linearizedOperations.length; applyIdx++) {
const op = linearizedOperations[applyIdx];
for (const op of linearizedOperations) {
drp[op.type](op.value);
}
if (vertex.operation) {
Expand Down
3 changes: 2 additions & 1 deletion packages/object/src/linearize/multipleSemantics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export function linearizeMultipleSemantics(
const dropped = new Array(order.length).fill(false);
const indices: Map<Hash, number> = new Map();
const result: Operation[] = [];
let i = 0;
// always remove the first operation
let i = 1;

while (i < order.length) {
if (dropped[i]) {
Expand Down
3 changes: 2 additions & 1 deletion packages/object/src/linearize/pairSemantics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export function linearizePairSemantics(
const order: Hash[] = hashGraph.topologicalSort(true, origin, subgraph);
const dropped = new Array(order.length).fill(false);
const result = [];
let i = 0;
// alway remove the first operation
d-roak marked this conversation as resolved.
Show resolved Hide resolved
let i = 1;

while (i < order.length) {
if (dropped[i]) {
Expand Down
Loading