From cc943fed50ab67ff1ed26cf82efe803a22f3f010 Mon Sep 17 00:00:00 2001 From: Kieran Date: Tue, 13 Jun 2023 17:06:47 +0100 Subject: [PATCH] try improve performance of diffFilters --- package.json | 2 +- packages/system/src/RequestBuilder.ts | 4 ++-- packages/system/src/RequestSplitter.ts | 22 ++++++++++++++++++-- packages/system/tests/RequestBuilder.test.ts | 4 ++-- 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index d05a6602..286263aa 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "scripts": { "build": "yarn workspace @snort/system build && yarn workspace @snort/app build", "start": "yarn workspace @snort/system build && yarn workspace @snort/app start", - "test": "yarn workspace @snort/app test" + "test": "yarn workspace @snort/system build && yarn workspace @snort/app test && yarn workspace @snort/system test" }, "devDependencies": { "@tauri-apps/cli": "^1.2.3", diff --git a/packages/system/src/RequestBuilder.ts b/packages/system/src/RequestBuilder.ts index b93dc648..0bc23a53 100644 --- a/packages/system/src/RequestBuilder.ts +++ b/packages/system/src/RequestBuilder.ts @@ -95,8 +95,6 @@ export class RequestBuilder { /** * Detects a change in request from a previous set of filters - * @param q All previous filters merged - * @returns */ buildDiff(relays: RelayCache, filters: Array): Array { const start = unixNowMs(); @@ -114,6 +112,8 @@ export class RequestBuilder { relay: a.relay, }; }); + } else { + this.#log(`Wasted ${ts} ms detecting no changes!`); } return []; } diff --git a/packages/system/src/RequestSplitter.ts b/packages/system/src/RequestSplitter.ts index 7f430895..8863776b 100644 --- a/packages/system/src/RequestSplitter.ts +++ b/packages/system/src/RequestSplitter.ts @@ -3,9 +3,27 @@ import { FlatReqFilter } from "./RequestExpander"; import { flatMerge } from "./RequestMerger"; export function diffFilters(prev: Array, next: Array, calcRemoved?: boolean) { - const added = next.filter(a => !prev.some(b => flatFilterEq(a, b))); - const removed = calcRemoved ? prev.filter(a => !next.some(b => flatFilterEq(a, b))) : []; + const added = []; + const removed = []; + for (let x = 0; x < next.length; x++) { + const px = prev.findIndex(a => flatFilterEq(a, next[x])); + if (px !== -1) { + prev.splice(px, 1); + } else { + added.push(next[x]); + } + } + if (calcRemoved) { + for (let x = 0; x < prev.length; x++) { + const px = next.findIndex(a => flatFilterEq(a, prev[x])); + if (px !== -1) { + next.splice(px, 1); + } else { + removed.push(prev[x]); + } + } + } const changed = added.length > 0 || removed.length > 0; return { added: changed ? flatMerge(added) : [], diff --git a/packages/system/tests/RequestBuilder.test.ts b/packages/system/tests/RequestBuilder.test.ts index 62919c36..530d7e0b 100644 --- a/packages/system/tests/RequestBuilder.test.ts +++ b/packages/system/tests/RequestBuilder.test.ts @@ -167,7 +167,7 @@ describe("RequestBuilder", () => { describe("build diff, large follow list", () => { const f = []; - for (let x = 0; x < 500; x++) { + for (let x = 0; x < 2500; x++) { const bytes = crypto.getRandomValues(new Uint8Array(32)); f.push(bytesToHex(bytes)); } @@ -194,6 +194,6 @@ describe("build diff, large follow list", () => { const start2 = unixNowMs(); const b = rb.buildDiff(DummyCache, rb.buildRaw().flatMap(expandFilter)); expect(b).toEqual([]); - expect(unixNowMs() - start2).toBeLessThan(200); + expect(unixNowMs() - start2).toBeLessThan(100); }) \ No newline at end of file