From 8987494629d67acb7afabd52f3fccbff00de30bc Mon Sep 17 00:00:00 2001 From: Marcus Pousette Date: Fri, 27 Dec 2024 11:38:02 +0100 Subject: [PATCH] wip --- .../data/document/document/benchmark/index.ts | 51 ++++++------------- .../programs/data/shared-log/src/ranges.ts | 12 ++++- packages/utils/indexer/sqlite3/src/engine.ts | 1 - 3 files changed, 25 insertions(+), 39 deletions(-) diff --git a/packages/programs/data/document/document/benchmark/index.ts b/packages/programs/data/document/document/benchmark/index.ts index c6605c9c7..55dc234cd 100644 --- a/packages/programs/data/document/document/benchmark/index.ts +++ b/packages/programs/data/document/document/benchmark/index.ts @@ -1,8 +1,8 @@ import { field, option, variant } from "@dao-xyz/borsh"; import { Program, type ProgramClient } from "@peerbit/program"; import { TestSession } from "@peerbit/test-utils"; -import B from "benchmark"; import crypto from "crypto"; +import * as B from "tinybench"; import { v4 as uuid } from "uuid"; import { Documents, type SetupOptions } from "../src/program.js"; @@ -68,41 +68,20 @@ await client.open(store, { }, }); -const resolver: Map void> = new Map(); -store.docs.events.addEventListener("change", (change) => { - change.detail.added.forEach((doc) => { - resolver.get(doc.id)!(); - resolver.delete(doc.id); +const suite = new B.Bench({ name: "put", warmupIterations: 1000 }); +suite.add("put", async () => { + const doc = new Document({ + id: uuid(), + name: "hello", + number: 1n, + bytes: crypto.randomBytes(1200), + }); + await store.docs.put(doc, { + unique: true, }); }); -const suite = new B.Suite(); -suite - .add("put", { - fn: async (deferred: any) => { - const doc = new Document({ - id: uuid(), - name: "hello", - number: 1n, - bytes: crypto.randomBytes(1200), - }); - resolver.set(doc.id, () => { - deferred.resolve(); - }); - await store.docs.put(doc, { unique: true }); - }, - - minSamples: 300, - defer: true, - }) - .on("cycle", (event: any) => { - console.log(String(event.target)); - }) - .on("error", (err: any) => { - throw err; - }) - .on("complete", async function (this: any, ...args: any[]) { - await store.drop(); - await session.stop(); - }) - .run(); +await suite.run(); +console.table(suite.table()); +await store.drop(); +await session.stop(); diff --git a/packages/programs/data/shared-log/src/ranges.ts b/packages/programs/data/shared-log/src/ranges.ts index d51304042..1a26fcc97 100644 --- a/packages/programs/data/shared-log/src/ranges.ts +++ b/packages/programs/data/shared-log/src/ranges.ts @@ -1606,6 +1606,7 @@ export const getSamples = async ( let matured = 0; /* let missingForCursors: NumberFromType[] = [] */ + let uniqueVisited = new Set(); for (let i = 0; i < cursor.length; i++) { let point = cursor[i]; @@ -1615,6 +1616,7 @@ export const getSamples = async ( ); for (const rect of allContaining) { + uniqueVisited.add(rect.value.hash); let prev = leaders.get(rect.value.hash); if (!prev) { if (isMatured(rect.value, now, roleAge)) { @@ -1626,8 +1628,13 @@ export const getSamples = async ( } } - if (options?.uniqueReplicators?.size === leaders.size) { - break; // nothing ore to find + if (options?.uniqueReplicators) { + if ( + options.uniqueReplicators.size === leaders.size || + options.uniqueReplicators.size === uniqueVisited.size + ) { + break; // nothing more to find + } } if (options?.onlyIntersecting || matured > i) { @@ -1639,6 +1646,7 @@ export const getSamples = async ( roleAge, peers, (rect, m) => { + uniqueVisited.add(rect.hash); const prev = leaders.get(rect.hash); if (m) { if (!prev) { diff --git a/packages/utils/indexer/sqlite3/src/engine.ts b/packages/utils/indexer/sqlite3/src/engine.ts index 677267482..f1af9336a 100644 --- a/packages/utils/indexer/sqlite3/src/engine.ts +++ b/packages/utils/indexer/sqlite3/src/engine.ts @@ -468,7 +468,6 @@ export class SQLLiteIndex> /* console.log("----------------------") console.log(sqlFetch); */ - const allResults = await planningScope.perform(async () => { const allResults: Record[] = await stmt.all([ ...bindable,