try improve performance of diffFilters
This commit is contained in:
parent
3ffc9bc81f
commit
cc943fed50
@ -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",
|
||||
|
@ -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<FlatReqFilter>): Array<BuiltRawReqFilter> {
|
||||
const start = unixNowMs();
|
||||
@ -114,6 +112,8 @@ export class RequestBuilder {
|
||||
relay: a.relay,
|
||||
};
|
||||
});
|
||||
} else {
|
||||
this.#log(`Wasted ${ts} ms detecting no changes!`);
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
@ -3,9 +3,27 @@ import { FlatReqFilter } from "./RequestExpander";
|
||||
import { flatMerge } from "./RequestMerger";
|
||||
|
||||
export function diffFilters(prev: Array<FlatReqFilter>, next: Array<FlatReqFilter>, 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) : [],
|
||||
|
@ -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);
|
||||
|
||||
})
|
Loading…
x
Reference in New Issue
Block a user