Skip to content

Commit

Permalink
optimised cache usage for dynamic expressions
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Deubler <[email protected]>
  • Loading branch information
TerminalTim committed Jul 12, 2024
1 parent bd960bf commit 51c8796
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 49 deletions.
7 changes: 2 additions & 5 deletions packages/common/src/Expressions/Expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export abstract class Expression implements IExpression {

json: JSONExpression;

supportsPartialEval: boolean = false;
supportsPartialEval: boolean;
constructor(json: JSONExpression, env: ExpressionParser) {
// this.id = expId++;
this.json = json;
Expand Down Expand Up @@ -83,9 +83,6 @@ export abstract class Expression implements IExpression {

resolve(context=this.env.context, mode?: ExpressionMode) {
const {env} = this;
env.setMode(mode);
const result = env.evaluate(this, context);
env.setMode(ExpressionMode.static);
return result;
return env.evaluate(this, context, mode);
}
}
21 changes: 16 additions & 5 deletions packages/common/src/Expressions/ExpressionParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
* License-Filename: LICENSE
*/

import {Expression, ExpressionMode, IExpression, JSONExpression} from './Expression';
import {Expression, ExpressionMode, JSONExpression} from './Expression';
import * as Expressions from './Expressions';
import * as InterpolateExpressions from './InterpolateExpression';
import {JSUtils} from '@here/xyz-maps-common';

type ResultCache = Map<Expression, any> & { hits?: number };

Expand Down Expand Up @@ -69,7 +68,15 @@ export class ExpressionParser {

// this.cache.get = this.cache.set =()=>undefined;
// this.defaultResultCache.get = this.defaultResultCache.set =()=>undefined;
// this.dynamicResultCache.get = this.defaultResultCache.set =()=>undefined;
// this.dynamicResultCache.get = this.dynamicResultCache.set =()=>undefined;
// this.dynamicResultCache.get = (exp)=>{
// console.log('dynamicResultCache get');
// };
// this.dynamicResultCache._clear = this.dynamicResultCache.clear;
// this.dynamicResultCache.clear =()=>{
// console.log('dynamicResultCache cleared');
// this.dynamicResultCache._clear();
// };
}

init(def, mapContext) {
Expand All @@ -87,10 +94,11 @@ export class ExpressionParser {
this.cache.clear();
}

evaluate(exp, context) {
exp = this.parseJSON(exp);
evaluate(exp, context, mode: ExpressionMode = ExpressionMode.static) {
let result;
exp = this.parseJSON(exp);
try {
this.setMode(mode);
result = this.evaluateParsed(exp, context);
} catch (e) {
if (e.message === 'DynamicExpressionInterrupt') {
Expand Down Expand Up @@ -189,6 +197,9 @@ export class ExpressionParser {

clearResultCache() {
this.defaultResultCache.clear();
}

clearDynamicResultCache() {
this.dynamicResultCache.clear();
}

Expand Down
46 changes: 8 additions & 38 deletions packages/core/src/styles/XYZLayerStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@

import {Feature} from '../features/Feature';
import {Color, LayerStyle, Style, StyleGroup, StyleGroupMap, StyleValueFunction, StyleZoomRange} from '../styles/LayerStyle';
import {ExpressionParser, JSONExpression} from '@here/xyz-maps-common';
import {Expression, ExpressionParser, JSONExpression} from '@here/xyz-maps-common';
import {TileLayer} from '../layers/TileLayer';
// import {JsonExpression} from '@here/xyz-maps-common';

const isTypedArray = (() => {
const TypedArray = Object.getPrototypeOf(Uint8Array);
Expand Down Expand Up @@ -94,9 +93,8 @@ export class XYZLayerStyle implements LayerStyle {
const property = styleJSON[p];
this[p] = p == 'styleGroups' ? deepCopy(property) : property;
}
// layerStyle._l = layer;
this.definitions ||= {};

this.definitions ||= {};
this.expParser = new ExpressionParser(this.definitions, this.exprContext);

if (!styleJSON.assign) {
Expand All @@ -107,14 +105,10 @@ export class XYZLayerStyle implements LayerStyle {
styleGrp = [styleGrp];
}
for (let style of styleGrp) {
// for (let prop in style) {
// if ( ExpressionParser.isJSONExp(style[prop])) {
// style[prop] = this.createExpEvaluator(style[prop]);
// }
// }
if (style.filter) {
if (ExpressionParser.isJSONExp(style.filter)) {
style.filter = this.createExpEvaluator(style.filter);
// style.filter = this.createExpEvaluator(style.filter);
style.filter = this.expParser.parseJSON(style.filter);
}
flatStyles.push(style);
}
Expand All @@ -128,18 +122,6 @@ export class XYZLayerStyle implements LayerStyle {
this._style = styleJSON;
}

private createExpEvaluator(expr: JSONExpression) {
return (feature) => {
return this.expParser.evaluate(expr, feature.properties);
// window._measure.count('filter-calls');
// window._measure.start('filter-eval');
// let filtered = this.expParser.evaluate(expr, feature.properties);
// window._measure.stop('filter-eval');
// return filtered;
};
};

// default: simple assignment based on geometryType.
assign(feature: Feature, level?: number): string | null | undefined {
// return <string><unknown> this.flatStyles;
// let filteredStyleGroups = this.flatStyles.filter((style) => {
Expand All @@ -156,9 +138,10 @@ export class XYZLayerStyle implements LayerStyle {
for (let style of this.flatStyles) {
const {filter} = style;

const filtered =
typeof filter === 'function' ? (style.filter as StyleValueFunction<boolean>)(feature, level)
// : ExpressionParser.isJSONExp(filter) ? this.expParser.evaluate(filter, feature.properties)
const filtered = filter instanceof Expression
? filter.resolve(feature.properties)
: typeof filter === 'function'
? (style.filter as StyleValueFunction<boolean>)(feature, level)
: filter;

if (filtered) {
Expand All @@ -168,19 +151,11 @@ export class XYZLayerStyle implements LayerStyle {

if (i > 0) {
filteredStyleGroups.length = i;
// filteredStyleGroups.__p = false;
return <string><unknown>filteredStyleGroups;
}
// return feature.geometry.type;
};

// get : function( feature, level )
// {
// return this.styleGroups[
// this.assign( feature, level )
// ];
// },

getDefinitions() {
return this.definitions;
}
Expand Down Expand Up @@ -256,11 +231,6 @@ export class XYZLayerStyle implements LayerStyle {
}

return group;

// if( layer = this._l )
// {
// layer._l.trigger( 'style', [ feature, group, layer ], true );
// }
};

merge(grp1: readonly Style[], grp2: StyleGroup | false) {
Expand Down
2 changes: 1 addition & 1 deletion packages/display/src/displays/Layers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ class Layers extends Array<Layer> {
if (expContext && expContext.zoom != zoomlevel) {
expContext.zoom = zoomlevel;
expContext.$zoom = zoomlevel^0;
// expParser.dynamicResultCache.clear();
expParser.clearDynamicResultCache();
}
}
}
Expand Down

0 comments on commit 51c8796

Please sign in to comment.