Cache drawImage's loaded and scaled surfaces by file path - #27
Merged
Conversation
drawImage re-read and re-decoded the source file, and rescaled it, on every call -- a 60fps render loop redid both 60 times a second even though the bytes on disk never changed. Cache the loaded surface plus the most recent (size, scaled surface) pair per filePath, mirroring the existing _getFont cache. Unlike fonts, an unconverted surface survives a pygame.quit()/init() cycle, so no session-invalidation logic is needed here. Closes #23 Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Member
Author
|
Self-review rubric: Universal
Repo-specific
One judgment note (not a FAIL, just flagging): the cache is intentionally unbounded in the number of distinct |
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.
Summary
drawImagepreviously calledpygame.image.loadandpygame.transform.scaleon every single call, re-reading and re-decoding the same asset file from disk every frame (measured at ~1.04 ms/call, dominated by the ~0.57 ms decode).filePath, and separately caches the most recent(size, scaled surface)pair perfilePath— so a steady-state render loop (same size every frame) pays the load/scale cost exactly once, while an animated zoom cannot grow the cache unbounded._getFontcache's rationale, but does not need its display-session invalidation logic: an unconvertedpygame.image.loadsurface survives apygame.quit()/init()cycle (verified empirically), unlike aFontbuilt against SDL_ttf.test_draw_image_missing_file_raisesstill passes unmodified).drawImagetoday per a quick search, but the signature is unchanged regardless).drawImageentry, per the issue's suggestion to document rather than work around it.Test plan
python3 -m py_compile src/main/python/preponderous/graphik/graphik.pypython3 -m pytest— 24 passed (20 pre-existing + 4 new)test_draw_image_reuses_loaded_surface_across_callsandtest_draw_image_reuses_scaled_surface_for_the_same_sizeboth failed (5 loads/scales instead of 1) withgraphik.pyreverted, and passed once restored.test_draw_image_rescales_when_size_changesconfirms the per-path cache holds only the most recent size, not every size ever requested.test_draw_image_still_blits_correctly_after_cachingconfirms cached surfaces still blit correct pixels.Closes #23
This PR description was drafted during a Gardener session (Stephenson-Software/gardener).