Skip to content

Fix memory leak#8

Open
ForeverWintr wants to merge 2 commits into
InvestmentSystems:mainfrom
ForeverWintr:fix-range-string-cache-leak
Open

Fix memory leak#8
ForeverWintr wants to merge 2 commits into
InvestmentSystems:mainfrom
ForeverWintr:fix-range-string-cache-leak

Conversation

@ForeverWintr

@ForeverWintr ForeverWintr commented Jun 11, 2026

Copy link
Copy Markdown
Member

_get_range_string was wrapped in functools.lru_cache(maxsize=None) and is called once per cell with the cell's coordinates as the key, so the cache grew by one permanent entry for every cell of every workbook written in the process. Measured in a long-running test suite: 3.57M entries (~1GB+) after writing one large report set, at a 14% hit rate.

The string formatting it avoided is cheap, and the call to get_column_letter is already cached by openpyxl.

ForeverWintr and others added 2 commits June 11, 2026 11:34
_get_range_string was wrapped in functools.lru_cache(maxsize=None) and is
called once per cell with the cell's coordinates as the key, so the cache
grew by one permanent entry for every cell of every workbook written in the
process. Measured in a long-running test suite: 3.57M entries (~1GB+) after
writing one large report set, at a 14% hit rate -- the cache barely
functioned while growing monotonically.

The string formatting it avoided is cheaper than the cache machinery, and
the genuinely cacheable part (_get_column_letter) keeps its own cache,
whose key space (column numbers) is naturally tiny.

Co-Authored-By: Claude Fable 5 <[email protected]>
openpyxl's is already cached (unbounded, unfortunately).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant