Add calculation cache to improve CalcCellValue performance #2144
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR Details
Add calculation cache to improve CalcCellValue performance
Description
Implements a calculation cache mechanism for
CalcCellValue
to significantly improve performance when calculating formulas repeatedly. The cache stores calculated results and automatically invalidates when cells are modified.Related Issue
Fixes #2057 - Performance enhancement - cache calculated values
Motivation and Context
As described in #2057, Excelize doesn't cache calculated values from
CalcCellValue
, causing expensive formulas to be recalculated every time they are referenced by other cells. This leads to poor performance when dealing with complex formula dependencies.Example scenario:
Solution implemented:
sync.Map
SetCellX()
function is called"sheet!cell"
Performance improvement:
How Has This Been Tested
Test coverage includes:
Types of changes
☑️ New feature (non-breaking change which adds functionality)
☐ Docs change / refactoring / dependency upgrade
☐ Bug fix (non-breaking change which fixes an issue)
☐ Breaking change (fix or feature that would cause existing functionality to change)
Checklist
☑️ My code follows the code style of this project.
☐ My change requires a change to the documentation.
☐ I have updated the documentation accordingly.
☑️ I have read the CONTRIBUTING document.
☑️ I have added tests to cover my changes.
☑️ All new and existing tests passed.
Implementation Details
Modified Files:
excelize.go
: Added cache fields (calcCache sync.Map
,calcCacheMu sync.RWMutex
) to File structcalc.go
: Implemented caching logic inCalcCellValue()
+ addedclearCalcCache()
helpercell.go
: Added cache clearing to all 12 Set functionscalc_test.go
: Added 3 comprehensive cache test functions