Clear the textmap cache in Page.flush_cache() - #1397
Open
siddharthgaur1 wants to merge 1 commit into
Open
siddharthgaur1 wants to merge 1 commit into
siddharthgaur1 wants to merge 1 commit into
Conversation
Page.flush_cache() iterates Page.cached_properties, which covers _layout
and the object/edge caches but not the lru_cache wrapping _get_textmap.
That textmap cache is the largest of a page's caches, so flushing left
most of the memory in place.
Measured on tests/pdfs/chelsea_pdta.pdf (65 pages), extracting text from
every page and tracing with tracemalloc:
no flush 184.61 MB
flush_cache() 105.19 MB <- before
close() 4.80 MB
flush_cache() 4.80 MB <- after
Page.close() already cleared it explicitly, so it now simply delegates to
flush_cache(). DerivedPage.__init__ builds its textmap cache before
calling flush_cache(), rather than after, since flush_cache() now touches
that attribute.
Fixes jsvine#1395.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #1395.
Problem
Page.flush_cache()iteratesPage.cached_properties, which covers_layoutand the object/edge caches — but not thelru_cachewrapping_get_textmap, which is created as an instance attribute inPage.__init__and is the largest of a page's caches. So a flush left most of the memory in place.Measured on
tests/pdfs/chelsea_pdta.pdf(65 pages), callingextract_text()on every page undertracemalloc:flush_cache()— before this PRclose()flush_cache()— after this PRNote on the issue's diagnosis
The report attributes the retention to
_layoutsurviving the flush and topage_obj, and proposespage.page_obj = Noneas the fix. Neither holds up:_layoutis cleared already.flush_cachereadsself.cached_properties, which on aPageresolves toContainer.cached_properties + ["_layout"], so the default flush includes it.page.page_obj = Nonechanges nothing measurable (105.19 MB → 105.19 MB). The entire difference is the textmap cache.The retention the reporter measured is real; the cause is the textmap cache.
Change
Page.flush_cache()now clears the textmap cache after delegating toContainer.flush_cache.Page.close()did this explicitly already, so it now just delegates toflush_cache().DerivedPage.__init__builds its textmap cache before callingflush_cache()rather than after, sinceflush_cache()now touches that attribute. Without the reorder, constructing any cropped page raisesAttributeError.Users who want the old surgical behaviour can still pass an explicit list, e.g.
page.flush_cache(["_objects"])— the textmap cache is cleared either way, which matches what the method's name promises.Tests
Adds
test_flush_cache_clears_textmap, covering both an original page and a cropped one (theDerivedPagepath that the reorder protects). It fails on the parent commit (assert 1 == 0) and passes with the change.Full suite: 164 passed, 10 failed — all 10 failures are pre-existing on a clean
developin this environment (test_repair.pyneeds Ghostscript,test_convert.pyhits Windows line-ending differences) and are unrelated to this change.black,isort --profile black,flake8andmypyall pass on the touched files.