Skip to content

Commit

Permalink
[IMP] evaluation: improve perfs of getCorrespondingFormulaCell
Browse files Browse the repository at this point in the history
The getter `getCorrespondingFormulaCell` was tokenizing the cell's
content (via `compile`), which took a bit of time. But this is useless,
as we already have the tokens in the formula cell.

closes #3143

Task: 3584306
X-original-commit: 3d6f4f9
Signed-off-by: Lucas Lefèvre (lul) <[email protected]>
  • Loading branch information
hokolomopo authored and LucasLefevre committed Nov 13, 2023
1 parent 8f23f7b commit d7b4936
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/formulas/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ export const functionCache: { [key: string]: Omit<CompiledFormula, "dependencies

export function compile(formula: string): CompiledFormula {
const tokens = rangeTokenize(formula);
return compileTokens(tokens);
}

export function compileTokens(tokens: Token[]): CompiledFormula {
const { dependencies, constantValues } = formulaArguments(tokens);
const cacheKey = compilationCacheKey(tokens, dependencies, constantValues);
if (!functionCache[cacheKey]) {
Expand Down
10 changes: 8 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,14 @@ export const __info__ = {};
export { Revision } from "./collaborative/revisions";
export { Spreadsheet } from "./components/index";
export { setDefaultSheetViewSize } from "./constants";
export { compile, functionCache } from "./formulas/compiler";
export { astToFormula, convertAstNodes, iterateAstNodes, parse } from "./formulas/parser";
export { compile, compileTokens, functionCache } from "./formulas/compiler";
export {
astToFormula,
convertAstNodes,
iterateAstNodes,
parse,
parseTokens,
} from "./formulas/parser";
export { tokenize } from "./formulas/tokenizer";
export { AbstractChart } from "./helpers/figures/charts";
export { findCellInNewZone } from "./helpers/zones";
Expand Down
14 changes: 7 additions & 7 deletions src/plugins/ui_core_views/cell_evaluation/evaluation_plugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { compile, isExportableToExcel } from "../../../formulas/index";
import { compileTokens } from "../../../formulas/compiler";
import { Token, compile, isExportableToExcel } from "../../../formulas/index";
import { getItemId, positions, toXC } from "../../../helpers/index";
import {
CellPosition,
Expand Down Expand Up @@ -322,10 +323,9 @@ export class EvaluationPlugin extends UIPlugin {
getCorrespondingFormulaCell(position: CellPosition): FormulaCell | undefined {
const cell = this.getters.getCell(position);

if (cell && cell.content) {
if (cell.isFormula && !isBadExpression(cell.content)) {
return cell;
}
if (cell && cell.isFormula) {
return isBadExpression(cell.compiledFormula.tokens) ? undefined : cell;
} else if (cell && cell.content) {
return undefined;
}

Expand All @@ -350,9 +350,9 @@ export class EvaluationPlugin extends UIPlugin {
}
}

function isBadExpression(formula: string): boolean {
function isBadExpression(tokens: Token[]): boolean {
try {
compile(formula);
compileTokens(tokens);
return false;
} catch (error) {
return true;
Expand Down

0 comments on commit d7b4936

Please sign in to comment.