optimize performance
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Kieran 2023-06-14 02:00:14 +01:00
parent cc943fed50
commit f0740cb6ca
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941
5 changed files with 56 additions and 54 deletions

View File

@ -26,8 +26,6 @@ export default function useTimelineFeed(subject: TimelineSubject, options: Timel
window: options.window, window: options.window,
now: options.now ?? unixNow(), now: options.now ?? unixNow(),
}); });
const [trackingEvents, setTrackingEvent] = useState<u256[]>([]);
const [trackingParentEvents, setTrackingParentEvents] = useState<u256[]>([]);
const pref = useLogin().preferences; const pref = useLogin().preferences;
const createBuilder = useCallback(() => { const createBuilder = useCallback(() => {
@ -132,34 +130,8 @@ export default function useTimelineFeed(subject: TimelineSubject, options: Timel
latest.clear(); latest.clear();
}, [options.relay]); }, [options.relay]);
const subNext = useMemo(() => { function getParentEvents() {
const rb = new RequestBuilder(`timeline-related:${subject.type}:${subject.discriminator}`); if (main.data) {
if (trackingEvents.length > 0) {
rb.withFilter()
.kinds(
pref.enableReactions
? [EventKind.Reaction, EventKind.Repost, EventKind.ZapReceipt]
: [EventKind.ZapReceipt, EventKind.Repost]
)
.tag("e", trackingEvents);
}
if (trackingParentEvents.length > 0) {
rb.withFilter().ids(trackingParentEvents);
}
return rb.numFilters > 0 ? rb : null;
}, [trackingEvents, pref, subject.type]);
const related = useRequestBuilder<FlatNoteStore>(FlatNoteStore, subNext);
useEffect(() => {
if (main.data && main.data.length > 0) {
setTrackingEvent(s => {
const ids = (main.data ?? []).map(a => a.id);
if (ids.some(a => !s.includes(a))) {
return Array.from(new Set([...s, ...ids]));
}
return s;
});
const repostsByKind6 = main.data const repostsByKind6 = main.data
.filter(a => a.kind === EventKind.Repost && a.content === "") .filter(a => a.kind === EventKind.Repost && a.content === "")
.map(a => a.tags.find(b => b[0] === "e")) .map(a => a.tags.find(b => b[0] === "e"))
@ -172,18 +144,31 @@ export default function useTimelineFeed(subject: TimelineSubject, options: Timel
.map(a => a.tags.find(tagFilterOfTextRepost(a))) .map(a => a.tags.find(tagFilterOfTextRepost(a)))
.filter(a => a) .filter(a => a)
.map(a => unwrap(a)[1]); .map(a => unwrap(a)[1]);
const reposts = [...repostsByKind6, ...repostsByKind1]; return [...repostsByKind6, ...repostsByKind1];
if (reposts.length > 0) {
setTrackingParentEvents(s => {
if (reposts.some(a => !s.includes(a))) {
const temp = new Set([...s, ...reposts]);
return Array.from(temp);
}
return s;
});
}
} }
}, [main]); return [];
}
const subNext = useMemo(() => {
const rb = new RequestBuilder(`timeline-related:${subject.type}:${subject.discriminator}`);
const trackingEvents = main.data?.map(a => a.id) ?? [];
if (trackingEvents.length > 0) {
rb.withFilter()
.kinds(
pref.enableReactions
? [EventKind.Reaction, EventKind.Repost, EventKind.ZapReceipt]
: [EventKind.ZapReceipt, EventKind.Repost]
)
.tag("e", trackingEvents);
}
const trackingParentEvents = getParentEvents();
if (trackingParentEvents.length > 0) {
rb.withFilter().ids(trackingParentEvents);
}
return rb.numFilters > 0 ? rb : null;
}, [main.data, pref, subject.type]);
const related = useRequestBuilder<FlatNoteStore>(FlatNoteStore, subNext);
return { return {
main: main.data, main: main.data,

View File

@ -1,7 +1,7 @@
import { v4 as uuid } from "uuid"; import { v4 as uuid } from "uuid";
import debug from "debug"; import debug from "debug";
import { Connection, ReqFilter, Nips, TaggedRawEvent } from "."; import { Connection, ReqFilter, Nips, TaggedRawEvent } from ".";
import { unixNowMs, unwrap } from "./Utils"; import { reqFilterEq, unixNowMs, unwrap } from "./Utils";
import { NoteStore } from "./NoteCollection"; import { NoteStore } from "./NoteCollection";
import { flatMerge } from "./RequestMerger"; import { flatMerge } from "./RequestMerger";
import { BuiltRawReqFilter } from "./RequestBuilder"; import { BuiltRawReqFilter } from "./RequestBuilder";
@ -46,7 +46,6 @@ class QueryTrace {
forceEose() { forceEose() {
this.eose = unixNowMs(); this.eose = unixNowMs();
this.#wasForceClosed = true; this.#wasForceClosed = true;
this.#fnProgress();
this.sendClose(); this.sendClose();
} }
@ -163,7 +162,13 @@ export class Query implements QueryBase {
} }
get flatFilters() { get flatFilters() {
return this.#tracing.flatMap(a => a.filters).flatMap(expandFilter); const f: Array<ReqFilter> = [];
for (const x of this.#tracing.flatMap(a => a.filters)) {
if (!f.some(a => reqFilterEq(a, x))) {
f.push(x);
}
}
return f.flatMap(expandFilter);
} }
get feed() { get feed() {

View File

@ -96,11 +96,11 @@ export class RequestBuilder {
/** /**
* Detects a change in request from a previous set of filters * Detects a change in request from a previous set of filters
*/ */
buildDiff(relays: RelayCache, filters: Array<FlatReqFilter>): Array<BuiltRawReqFilter> { buildDiff(relays: RelayCache, prev: Array<FlatReqFilter>): Array<BuiltRawReqFilter> {
const start = unixNowMs(); const start = unixNowMs();
const next = this.#builders.flatMap(f => expandFilter(f.filter)) const next = this.#builders.flatMap(f => expandFilter(f.filter));
const diff = diffFilters(filters, next); const diff = diffFilters(prev, next);
const ts = (unixNowMs() - start); const ts = (unixNowMs() - start);
this.#log("buildDiff %s %d ms", this.id, ts); this.#log("buildDiff %s %d ms", this.id, ts);
if (diff.changed) { if (diff.changed) {
@ -205,7 +205,7 @@ export class RequestFilterBuilder {
tag(key: "e" | "p" | "d" | "t" | "r", value?: Array<string>) { tag(key: "e" | "p" | "d" | "t" | "r", value?: Array<string>) {
if (!value) return this; if (!value) return this;
this.#filter[`#${key}`] = value; this.#filter[`#${key}`] = appendDedupe(this.#filter[`#${key}`], value);
return this; return this;
} }

View File

@ -6,21 +6,23 @@ export function diffFilters(prev: Array<FlatReqFilter>, next: Array<FlatReqFilte
const added = []; const added = [];
const removed = []; const removed = [];
for (let x = 0; x < next.length; x++) { prev = [...prev];
const px = prev.findIndex(a => flatFilterEq(a, next[x])); next = [...next];
for (const n of next) {
const px = prev.findIndex(a => flatFilterEq(a, n));
if (px !== -1) { if (px !== -1) {
prev.splice(px, 1); prev.splice(px, 1);
} else { } else {
added.push(next[x]); added.push(n);
} }
} }
if (calcRemoved) { if (calcRemoved) {
for (let x = 0; x < prev.length; x++) { for (const p of prev) {
const px = next.findIndex(a => flatFilterEq(a, prev[x])); const px = next.findIndex(a => flatFilterEq(a, p));
if (px !== -1) { if (px !== -1) {
next.splice(px, 1); next.splice(px, 1);
} else { } else {
removed.push(prev[x]); removed.push(p);
} }
} }
} }

View File

@ -86,6 +86,16 @@ export function flatFilterEq(a: FlatReqFilter, b: FlatReqFilter): boolean {
&& a["#r"] === b["#r"]; && a["#r"] === b["#r"];
} }
export function countMembers(a: any) {
let ret = 0;
for (const [k, v] of Object.entries(a)) {
if (Array.isArray(v)) {
ret += v.length;
}
}
return ret;
}
export function equalProp(a: string | number | Array<string | number> | undefined, b: string | number | Array<string | number> | undefined) { export function equalProp(a: string | number | Array<string | number> | undefined, b: string | number | Array<string | number> | undefined) {
if ((a !== undefined && b === undefined) || (a === undefined && b !== undefined)) { if ((a !== undefined && b === undefined) || (a === undefined && b !== undefined)) {
return false; return false;