Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

perf: add cache support to evaluation in reearth/core #573

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
"leaflet": "1.9.3",
"localforage": "1.10.0",
"lodash-es": "4.17.21",
"lru-cache": "^8.0.4",
"mini-svg-data-uri": "1.4.4",
"parse-domain": "7.0.1",
"quickjs-emscripten": "0.21.1",
Expand Down
45 changes: 42 additions & 3 deletions src/core/mantle/evaluator/simple/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { pick } from "lodash-es";
import LRUCache from "lru-cache";

import type { EvalContext, EvalResult } from "..";
import {
Expand All @@ -15,6 +16,9 @@ import {
import { ConditionalExpression } from "./conditionalExpression";
import { clearExpressionCaches, Expression } from "./expression";
import { evalTimeInterval } from "./interval";
import { getCacheableProperties } from "./utils";

const EVAL_EXPRESSION_CACHES = new LRUCache({ max: 10000 });

export async function evalSimpleLayer(
layer: LayerSimple,
Expand All @@ -23,6 +27,11 @@ export async function evalSimpleLayer(
const features = layer.data ? await ctx.getAllFeatures(layer.data) : undefined;
const appearances: Partial<LayerAppearanceTypes> = pick(layer, appearanceKeys);
const timeIntervals = evalTimeInterval(features, layer.data?.time);

if (!features) {
return undefined;
}

return {
layer: evalLayerAppearances(appearances, layer),
features: features?.map((f, i) => evalSimpleLayerFeature(layer, f, timeIntervals?.[i])),
Expand Down Expand Up @@ -111,11 +120,41 @@ function evalExpression(
if (typeof styleExpression === "undefined") {
return undefined;
} else if (typeof styleExpression === "object" && styleExpression.conditions) {
return new ConditionalExpression(styleExpression, feature, layer.defines).evaluate();
const cacheKey = JSON.stringify([
styleExpression,
getCacheableProperties(styleExpression, feature),
layer.defines,
]);

if (EVAL_EXPRESSION_CACHES.has(cacheKey)) {
return EVAL_EXPRESSION_CACHES.get(cacheKey);
}

const result = new ConditionalExpression(
styleExpression,
feature,
layer.defines,
).evaluate();
EVAL_EXPRESSION_CACHES.set(cacheKey, result);

return result;
} else if (typeof styleExpression === "boolean" || typeof styleExpression === "number") {
return new Expression(String(styleExpression), feature, layer.defines).evaluate();
return styleExpression;
} else if (typeof styleExpression === "string") {
return new Expression(styleExpression, feature, layer.defines).evaluate();
const cacheKey = JSON.stringify([
styleExpression,
getCacheableProperties(styleExpression, feature),
layer.defines,
]);

if (EVAL_EXPRESSION_CACHES.has(cacheKey)) {
return EVAL_EXPRESSION_CACHES.get(cacheKey);
}

const result = new Expression(styleExpression, feature, layer.defines).evaluate();
EVAL_EXPRESSION_CACHES.set(cacheKey, result);

return result;
}
return styleExpression;
}
Expand Down
76 changes: 76 additions & 0 deletions src/core/mantle/evaluator/simple/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { expect, test, describe } from "vitest";

import { ConditionsExpression } from "../../types";

import { getReferences, getCombinedReferences } from "./utils";

describe("getReferences", () => {
test("should return an empty array when the input is an empty string", () => {
expect(getReferences("")).toEqual([]);
});

test("should return the input string as a single element array when it contains no placeholders", () => {
expect(getReferences("hello world")).toEqual([]);
});

test("should return the input string with placeholders replaced by variable references", () => {
expect(getReferences("a${b}c${d}e${f}g")).toEqual(["b", "d", "f"]);
});

test("should return the input string with placeholders inside quotes left unchanged", () => {
expect(getReferences("a'${b}'c\"${d}\"e${f}g")).toEqual(["b", "d", "f"]);
});

test("should throw an error if a placeholder is not closed", () => {
expect(() => getReferences("a${b")).toThrowError("Unmatched {.");
});
});

describe("getCombinedReferences", () => {
test("should return an empty array when the input is an empty string", () => {
expect(getCombinedReferences("")).toEqual([]);
});

test("should return the variable references from a single string", () => {
expect(getCombinedReferences("a${b}c${d}e${f}g")).toEqual(["b", "d", "f"]);
});

test("should return the combined variable references from all conditions", () => {
const expression: ConditionsExpression = {
conditions: [
["${a} > 1", "red"],
["${b} === 'foo'", "blue"],
["${c} !== true", "green"],
],
};

expect(getCombinedReferences(expression)).toEqual(["a", "b", "c"]);
});

test("should return an empty array when the input is an empty condition", () => {
const expression: ConditionsExpression = {
conditions: [],
};

expect(getCombinedReferences(expression)).toEqual([]);
});

test("should return the variable references from a single condition", () => {
const expression: ConditionsExpression = {
conditions: [["${a} > 1", "red"]],
};

expect(getCombinedReferences(expression)).toEqual(["a"]);
});

test("should handle quotes in conditions and values", () => {
const expression: ConditionsExpression = {
conditions: [
["${a} > 1 && '${b}' === \"bar\"", "red"],
["'${c}' === '${d}'", "blue"],
],
};

expect(getCombinedReferences(expression)).toEqual(["a", "b", "c", "d"]);
});
});
67 changes: 67 additions & 0 deletions src/core/mantle/evaluator/simple/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { pick } from "lodash-es";

import { Feature, StyleExpression } from "../../types";

const JSONPATH_REGEX =
/\$[a-zA-Z_][a-zA-Z0-9_]*(\[[^\]]+\])?(\.[a-zA-Z_][a-zA-Z0-9_]*(\[[^\]]+\])?)*$/g;
pyshx marked this conversation as resolved.
Show resolved Hide resolved

export function getCombinedReferences(expression: StyleExpression): string[] {
if (typeof expression === "string") {
return getReferences(expression);
} else {
const references: string[] = [];
for (const [condition, value] of expression.conditions) {
references.push(...getReferences(condition), ...getReferences(value));
}
return references;
}
}

export function getReferences(expression: string): string[] {
rot1024 marked this conversation as resolved.
Show resolved Hide resolved
if (hasJsonPath(expression)) return ["REEARTH_JSONPATH"];
pyshx marked this conversation as resolved.
Show resolved Hide resolved
const result: string[] = [];
let exp = expression;
let i = exp.indexOf("${");

while (i >= 0) {
const openSingleQuote = exp.indexOf("'", i);
const openDoubleQuote = exp.indexOf('"', i);

if (openSingleQuote >= 0 && openSingleQuote < i) {
const closeQuote = exp.indexOf("'", openSingleQuote + 1);
result.push(exp.substring(0, closeQuote + 1));
exp = exp.substring(closeQuote + 1);
} else if (openDoubleQuote >= 0 && openDoubleQuote < i) {
const closeQuote = exp.indexOf('"', openDoubleQuote + 1);
result.push(exp.substring(0, closeQuote + 1));
exp = exp.substring(closeQuote + 1);
} else {
const j = exp.indexOf("}", i);

if (j < 0) {
throw new Error("Unmatched {.");
}

result.push(exp.substring(i + 2, j));
exp = exp.substring(j + 1);
}

i = exp.indexOf("${");
}

return result;
}

const hasJsonPath = (str: string): boolean => {
return JSONPATH_REGEX.test(str);
};

export function getCacheableProperties(styleExpression: StyleExpression, feature?: Feature) {
const ref = getCombinedReferences(styleExpression);
const properties = pick(
feature?.properties,
ref.includes("REEARTH_JSONPATH") ? Object.keys(feature?.properties) : ref,
);

return properties;
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12945,6 +12945,11 @@ lru-cache@^6.0.0:
dependencies:
yallist "^4.0.0"

lru-cache@^8.0.4:
version "8.0.4"
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-8.0.4.tgz#49fbbc46c0b4cedc36258885247f93dba341e7ec"
integrity sha512-E9FF6+Oc/uFLqZCuZwRKUzgFt5Raih6LfxknOSAVTjNkrCZkBf7DQCwJxZQgd9l4eHjIJDGR+E+1QKD1RhThPw==

lz-string@^1.4.4:
version "1.4.4"
resolved "https://registry.yarnpkg.com/lz-string/-/lz-string-1.4.4.tgz#c0d8eaf36059f705796e1e344811cf4c498d3a26"
Expand Down