From 1f4a36e056e996a648e0c8896ce6e905e7e6d851 Mon Sep 17 00:00:00 2001 From: Jonathan <395137+Grafikart@users.noreply.github.com> Date: Fri, 8 Mar 2024 10:49:57 +0100 Subject: [PATCH] Cache all calculated variables (#902) --- .../variables/lunatic-variables-store.ts | 33 +++++++++++++++++-- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/src/use-lunatic/commons/variables/lunatic-variables-store.ts b/src/use-lunatic/commons/variables/lunatic-variables-store.ts index e3a814d1c..a33bde486 100644 --- a/src/use-lunatic/commons/variables/lunatic-variables-store.ts +++ b/src/use-lunatic/commons/variables/lunatic-variables-store.ts @@ -53,13 +53,14 @@ export class LunaticVariablesStore { if (name === 'yAxis') return 1; return undefined; }; + store.set('1', 1); // Fake variable to use on the shapeFrom, we will use "variableDimension" in the future for (const variable of source.variables) { switch (variable.variableType) { case 'CALCULATED': store.setCalculated(variable.name, variable.expression.value, { dependencies: variable.bindingDependencies, iterationDepth: getIterationDepth(variable.name), - shapeFrom: variable.shapeFrom, + shapeFrom: variable.shapeFrom ?? '1', }); break; case 'COLLECTED': @@ -193,6 +194,28 @@ export class LunaticVariablesStore { get interpretCount() { return interpretCount; } + + // Displays a table of the most calculated variable (useful for debug) + debug() { + console.table( + Array.from(this.dictionary.values()) + .sort((a, b) => b.calculatedCount - a.calculatedCount) + .slice(0, 25) + .map((v) => ({ + name: v.name, + calculations: v.calculatedCount, + expression: v.expression, + })) + ); + console.log( + 'Total calculations : ' + + Array.from(this.dictionary.values()).reduce( + (acc, v) => acc + v.calculatedCount, + 0 + ) + ); + Array.from(this.dictionary.values()).map((v) => (v.calculatedCount = 0)); + } } class LunaticVariable { @@ -205,7 +228,7 @@ class LunaticVariable { // List of dependencies, ex: ['FIRSTNAME', 'LASTNAME'] private dependencies?: string[]; // Expression for calculated variable - private readonly expression?: string; + public readonly expression?: string; // Dictionary holding all the available variables private readonly dictionary?: Map; // Specific iteration depth to get value from dependencies (used for yAxis for instance) @@ -213,7 +236,9 @@ class LunaticVariable { // For calculated variable, shape is copied from another variable private readonly shapeFrom?: string; // Keep a record of variable name (optional, used for debug) - private readonly name?: string; + public readonly name?: string; + // Count the number of calculation + public calculatedCount = 0; constructor( args: { @@ -274,6 +299,8 @@ class LunaticVariable { if (isTestEnv()) { interpretCount++; } + // Uncomment this if you want to track the number of calculation + // this.calculatedCount++; // Remember the value try { this.setValue(interpretVTL(this.expression, bindings), iteration);