Skip to content

Commit 4e57cff

Browse files
committed
Remove CachedPredicate
1 parent f54c93a commit 4e57cff

File tree

2 files changed

+19
-41
lines changed

2 files changed

+19
-41
lines changed

src/module/apps/compendium-browser/tabs/base.svelte.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { CompendiumIndexData } from "@client/documents/collections/compendium-collection.d.mts";
22
import type { TableResultSource } from "@common/documents/table-result.d.mts";
33
import { CompendiumDirectoryPF2e } from "@module/apps/sidebar/compendium-directory.ts";
4-
import { CachedPredicate, type PredicateStatement } from "@system/predication.ts";
4+
import { Predicate, type PredicateStatement } from "@system/predication.ts";
55
import { ErrorPF2e, htmlQuery, sluggify } from "@util";
66
import MiniSearch from "minisearch";
77
import * as R from "remeda";
@@ -141,7 +141,7 @@ export abstract class CompendiumBrowserTab {
141141
protected abstract prepareFilterData(): this["filterData"];
142142

143143
/** Build a `CachedPredicate` from the applied filters */
144-
protected buildPredicate(): CachedPredicate {
144+
protected buildPredicate(): Predicate {
145145
if (!this.filterData) {
146146
throw ErrorPF2e(`Tab "${this.tabLabel}" is not initialized!`);
147147
}
@@ -203,7 +203,7 @@ export abstract class CompendiumBrowserTab {
203203
}
204204
}
205205

206-
return new CachedPredicate([{ and: statements }]);
206+
return new Predicate([{ and: statements }]);
207207
}
208208

209209
/** Sort result array by name, level or price */

src/module/system/predication.ts

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class Predicate extends Array<PredicateStatement> {
4141
}
4242

4343
const domain = options instanceof Set ? options : new Set(options);
44-
return this.every((s) => this.isTrue(s, domain));
44+
return this.every((s) => this.#isTrue(s, domain));
4545
}
4646

4747
toObject(): RawPredicate {
@@ -53,15 +53,15 @@ class Predicate extends Array<PredicateStatement> {
5353
}
5454

5555
/** Is the provided statement true? */
56-
protected isTrue(statement: PredicateStatement, domain: Set<string>): boolean {
56+
#isTrue(statement: PredicateStatement, domain: Set<string>): boolean {
5757
return (
5858
(typeof statement === "string" && domain.has(statement)) ||
59-
(StatementValidator.isBinaryOp(statement) && this.testBinaryOp(statement, domain)) ||
60-
(StatementValidator.isCompound(statement) && this.testCompound(statement, domain))
59+
(StatementValidator.isBinaryOp(statement) && this.#testBinaryOp(statement, domain)) ||
60+
(StatementValidator.isCompound(statement) && this.#testCompound(statement, domain))
6161
);
6262
}
6363

64-
protected testBinaryOp(statement: BinaryOperation, domain: Set<string>): boolean {
64+
#testBinaryOp(statement: BinaryOperation, domain: Set<string>): boolean {
6565
if ("eq" in statement) {
6666
return typeof statement.eq[1] === "string"
6767
? statement.eq[0] === statement.eq[1]
@@ -102,44 +102,22 @@ class Predicate extends Array<PredicateStatement> {
102102
}
103103

104104
/** Is the provided compound statement true? */
105-
protected testCompound(statement: Exclude<PredicateStatement, Atom>, domain: Set<string>): boolean {
105+
#testCompound(statement: Exclude<PredicateStatement, Atom>, domain: Set<string>): boolean {
106106
return (
107-
("and" in statement && statement.and.every((s) => this.isTrue(s, domain))) ||
108-
("nand" in statement && !statement.nand.every((s) => this.isTrue(s, domain))) ||
109-
("or" in statement && statement.or.some((s) => this.isTrue(s, domain))) ||
110-
("xor" in statement && statement.xor.filter((s) => this.isTrue(s, domain)).length === 1) ||
111-
("nor" in statement && !statement.nor.some((s) => this.isTrue(s, domain))) ||
112-
("not" in statement && !this.isTrue(statement.not, domain)) ||
113-
("if" in statement && !(this.isTrue(statement.if, domain) && !this.isTrue(statement.then, domain))) ||
107+
("and" in statement && statement.and.every((s) => this.#isTrue(s, domain))) ||
108+
("nand" in statement && !statement.nand.every((s) => this.#isTrue(s, domain))) ||
109+
("or" in statement && statement.or.some((s) => this.#isTrue(s, domain))) ||
110+
("xor" in statement && statement.xor.filter((s) => this.#isTrue(s, domain)).length === 1) ||
111+
("nor" in statement && !statement.nor.some((s) => this.#isTrue(s, domain))) ||
112+
("not" in statement && !this.#isTrue(statement.not, domain)) ||
113+
("if" in statement && !(this.#isTrue(statement.if, domain) && !this.#isTrue(statement.then, domain))) ||
114114
("iff" in statement &&
115-
(statement.iff.every((s) => this.isTrue(s, domain)) ||
116-
statement.iff.every((s) => !this.isTrue(s, domain))))
115+
(statement.iff.every((s) => this.#isTrue(s, domain)) ||
116+
statement.iff.every((s) => !this.#isTrue(s, domain))))
117117
);
118118
}
119119
}
120120

121-
/** A `Predicate` that caches the test method for all contained `PredicateStatements`.
122-
* This is a 4-5x speed increase for cases where the same predicate is tested against 1000+
123-
* different domains.
124-
*/
125-
class CachedPredicate extends Predicate {
126-
#statementCache = new Map<PredicateStatement, (d: Set<string>) => boolean>();
127-
128-
protected override isTrue(statement: PredicateStatement, domain: Set<string>): boolean {
129-
if (typeof statement === "string") return domain.has(statement);
130-
131-
if (!this.#statementCache.has(statement)) {
132-
if (StatementValidator.isBinaryOp(statement)) {
133-
this.#statementCache.set(statement, (d) => this.testBinaryOp(statement, d));
134-
} else if (StatementValidator.isCompound(statement)) {
135-
this.#statementCache.set(statement, (d) => this.testCompound(statement, d));
136-
}
137-
}
138-
139-
return !!this.#statementCache.get(statement)?.(domain);
140-
}
141-
}
142-
143121
class StatementValidator {
144122
static isStatement(statement: unknown): statement is PredicateStatement {
145123
return R.isPlainObject(statement)
@@ -272,5 +250,5 @@ type PredicateStatement = Atom | CompoundStatement;
272250

273251
type RawPredicate = PredicateStatement[];
274252

275-
export { CachedPredicate, Predicate, StatementValidator };
253+
export { Predicate, StatementValidator };
276254
export type { PredicateStatement, RawPredicate };

0 commit comments

Comments
 (0)