Skip to content

Commit

Permalink
perf: improve incemental computation
Browse files Browse the repository at this point in the history
  • Loading branch information
gdorsi committed Dec 14, 2024
1 parent 4a52092 commit f5a3394
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
4 changes: 2 additions & 2 deletions packages/cojson/src/coValueCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -618,14 +618,14 @@ export class CoValueCore {

getValidTransactions(options?: {
ignorePrivateTransactions: boolean;
exclude?: Set<`${SessionID}-${number}`>;
knownTransactions?: CoValueKnownState["sessions"];
}): DecryptedTransaction[] {
const validTransactions = determineValidTransactions(this);

const allTransactions: DecryptedTransaction[] = [];

for (const { txID, tx } of validTransactions) {
if (options?.exclude?.has(`${txID.sessionID}-${txID.txIndex}`)) {
if (options?.knownTransactions?.[txID.sessionID]! >= txID.txIndex) {
continue;
}

Expand Down
16 changes: 10 additions & 6 deletions packages/cojson/src/coValues/coMap.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { CoID, RawCoValue } from "../coValue.js";
import { CoValueCore, DecryptedTransaction } from "../coValueCore.js";
import { AgentID, SessionID, TransactionID } from "../ids.js";
import { CoValueCore } from "../coValueCore.js";
import { AgentID, TransactionID } from "../ids.js";
import { JsonObject, JsonValue } from "../jsonValue.js";
import { CoValueKnownState } from "../sync.js";
import { accountOrAgentIDfromSessionID } from "../typeUtils/accountOrAgentIDfromSessionID.js";
import { isCoValue } from "../typeUtils/isCoValue.js";
import { RawAccountID } from "./account.js";
Expand Down Expand Up @@ -50,7 +51,7 @@ export class RawCoMapView<
[Key in keyof Shape & string]?: MapOp<Key, Shape[Key]>[];
};
/** @internal */
processedTransactionsSet: Set<`${SessionID}-${number}`>;
knownTransactions: CoValueKnownState["sessions"];

/** @internal */
ignorePrivateTransactions: boolean;
Expand All @@ -73,15 +74,15 @@ export class RawCoMapView<
options?.ignorePrivateTransactions ?? false;
this.ops = {};
this.latest = {};
this.processedTransactionsSet = new Set();
this.knownTransactions = {};

this.processLatestTransactions();
}

private getNextValidTransactions() {
const nextValidTransactions = this.core.getValidTransactions({
ignorePrivateTransactions: this.ignorePrivateTransactions,
exclude: this.processedTransactionsSet,
knownTransactions: this.knownTransactions,
});

return nextValidTransactions;
Expand Down Expand Up @@ -125,7 +126,10 @@ export class RawCoMapView<
entries.push(entry);
changedEntries.set(change.key, entries);
}
this.processedTransactionsSet.add(`${txID.sessionID}-${txID.txIndex}`);
this.knownTransactions[txID.sessionID] = Math.max(
this.knownTransactions[txID.sessionID] ?? 0,
txID.txIndex,
);
}
}

Expand Down
12 changes: 8 additions & 4 deletions packages/cojson/src/coValues/coStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CoID, RawCoValue } from "../coValue.js";
import { CoValueCore } from "../coValueCore.js";
import { AgentID, SessionID, TransactionID } from "../ids.js";
import { JsonObject, JsonValue } from "../jsonValue.js";
import { CoValueKnownState } from "../sync.js";
import { accountOrAgentIDfromSessionID } from "../typeUtils/accountOrAgentIDfromSessionID.js";
import { isAccountID } from "../typeUtils/isAccountID.js";
import { isCoValue } from "../typeUtils/isCoValue.js";
Expand Down Expand Up @@ -53,14 +54,14 @@ export class RawCoStreamView<
[key: SessionID]: CoStreamItem<Item>[];
};
/** @internal */
processedTransactionsSet: Set<`${SessionID}-${number}`>;
knownTransactions: CoValueKnownState["sessions"];
readonly _item!: Item;

constructor(core: CoValueCore) {
this.id = core.id as CoID<this>;
this.core = core;
this.items = {};
this.processedTransactionsSet = new Set();
this.knownTransactions = {};
this.fillFromCoValue();
}

Expand All @@ -83,7 +84,7 @@ export class RawCoStreamView<

for (const { txID, madeAt, changes } of this.core.getValidTransactions({
ignorePrivateTransactions: false,
exclude: this.processedTransactionsSet,
knownTransactions: this.knownTransactions,
})) {
for (const changeUntyped of changes) {
const change = changeUntyped as Item;
Expand All @@ -95,7 +96,10 @@ export class RawCoStreamView<
entries.push({ value: change, madeAt, tx: txID });
changeEntries.add(entries);
}
this.processedTransactionsSet.add(`${txID.sessionID}-${txID.txIndex}`);
this.knownTransactions[txID.sessionID] = Math.max(
this.knownTransactions[txID.sessionID] ?? 0,
txID.txIndex,
);
}

for (const entries of changeEntries) {
Expand Down

0 comments on commit f5a3394

Please sign in to comment.