Add formula token cache to reduce repeated parsing overhead#4829
Add formula token cache to reduce repeated parsing overhead#4829oleibman merged 9 commits intoPHPOffice:masterfrom
Conversation
Benchmark Results (formula-ast-parsing)Targeted benchmark: parse repeated formula tokens (500 distinct patterns).
Enormous parsing speedup (15–195x). Calculation time unchanged — the win is entirely in token parsing, not evaluation. Memory neutral. |
|
I strive to avoid the introduction of new static properties. Is it possible to make |
|
Makes sense — I'll refactor both to instance properties on the FormulaParser and default the cache max size to 0 so it's opt-in. Users who want the parsing speedup can explicitly enable it. |
|
Thank you for your contribution. |
Summary
parseFormula()checks the cache — on hit, returns cached tokens immediately, skipping all regex operationsclearFormulaTokenCache()andgetFormulaTokenCacheSize()static methods for testing and memory managementChanges
src/PhpSpreadsheet/Calculation/Calculation.php: Static$formulaTokenCachearray, cache lookup inparseFormula(), clear/size methodstests/PhpSpreadsheetTests/Calculation/FormulaTokenCacheTest.php: 8 tests covering correctness, cache reuse, clearing, complex formulas, evictiontests/PhpSpreadsheetTests/Benchmark/FormulaTokenCacheBenchmark.php: Benchmark comparing cache hit vs miss for 1K formula cells and 10K parseFormula callsTest plan
vendor/bin/phpunit --group benchmark --filter FormulaTokenCacheBenchmark --stderr