Skip to content

Commit

Permalink
chore: cache compressed filters
Browse files Browse the repository at this point in the history
  • Loading branch information
v0l committed Jun 17, 2024
1 parent 240c3ba commit a31c054
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
12 changes: 10 additions & 2 deletions packages/system/src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ export class Query extends EventEmitter<QueryEvents> {

#log = debug("Query");

/**
* Compressed cached trace filters
*/
#cachedFilters?: Array<ReqFilter>;

constructor(system: SystemInterface, req: RequestBuilder) {
super();
this.request = req;
Expand All @@ -193,7 +198,6 @@ export class Query extends EventEmitter<QueryEvents> {
addRequest(req: RequestBuilder) {
if (req.instance === this.request.instance) {
// same requst, do nothing
this.#log("Same query %O === %O", req, this.request);
return;
}
this.#log("Add query %O to %s", req, this.id);
Expand All @@ -214,7 +218,10 @@ export class Query extends EventEmitter<QueryEvents> {
* Recompute the complete set of compressed filters from all query traces
*/
get filters() {
return this.#tracing.flatMap(a => a.filters);
if (this.#system && !this.#cachedFilters) {
this.#cachedFilters = this.#system.optimizer.compress(this.#tracing.flatMap(a => a.filters));
}
return this.#cachedFilters ?? this.#tracing.flatMap(a => a.filters);
}

get feed() {
Expand Down Expand Up @@ -432,6 +439,7 @@ export class Query extends EventEmitter<QueryEvents> {
c.off("closed", eoseHandler);
});
this.#tracing.push(qt);
this.#cachedFilters = undefined;

if (q.syncFrom !== undefined) {
c.request(["SYNC", qt.id, q.syncFrom, ...qt.filters], () => qt.sentToRelay());
Expand Down
17 changes: 12 additions & 5 deletions packages/system/src/request-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export class RequestBuilder {
#builders: Array<RequestFilterBuilder>;
#options?: RequestBuilderOptions;
#log = debug("RequestBuilder");
#rawCached?: Array<ReqFilter>;

constructor(id: string) {
this.instance = uuid();
Expand All @@ -83,17 +84,20 @@ export class RequestBuilder {
*/
add(other: RequestBuilder) {
this.#builders.push(...other.#builders);
this.#rawCached = undefined;
}

withFilter() {
const ret = new RequestFilterBuilder();
this.#builders.push(ret);
this.#rawCached = undefined;
return ret;
}

withBareFilter(f: ReqFilter) {
const ret = new RequestFilterBuilder(f);
this.#builders.push(ret);
this.#rawCached = undefined;
return ret;
}

Expand All @@ -105,12 +109,15 @@ export class RequestBuilder {
return this;
}

buildRaw(): Array<ReqFilter> {
return this.#builders.map(f => f.filter);
buildRaw(system?: SystemInterface): Array<ReqFilter> {
if (!this.#rawCached && system) {
this.#rawCached = system.optimizer.compress(this.#builders.map(f => f.filter));
}
return this.#rawCached ?? [];
}

build(system: SystemInterface): Array<BuiltRawReqFilter> {
let rawFilters = this.buildRaw();
let rawFilters = this.buildRaw(system);
if (system.requestRouter) {
rawFilters = system.requestRouter.forAllRequest(rawFilters);
}
Expand All @@ -124,7 +131,7 @@ export class RequestBuilder {
buildDiff(system: SystemInterface, prev: Array<ReqFilter>): Array<BuiltRawReqFilter> {
const start = unixNowMs();

let rawFilters = this.buildRaw();
let rawFilters = this.buildRaw(system);
if (system.requestRouter) {
rawFilters = system.requestRouter.forAllRequest(rawFilters);
}
Expand All @@ -133,7 +140,7 @@ export class RequestBuilder {
const ret = this.#groupFlatByRelay(system, diff);
const ts = unixNowMs() - start;
if (ts >= 100) {
this.#log("slow diff %s %d ms, consider separate query ids, or use skipDiff: %O", this.id, ts, ret);
this.#log("slow diff %s %d ms, consider separate query ids, or use skipDiff: %O", this.id, ts, prev);
}
return ret;
}
Expand Down

0 comments on commit a31c054

Please sign in to comment.