perf: convert_alpha() cached drawImage surfaces for faster blits - #28
Merged
Merged
Conversation
An unconverted pygame Surface is reformatted to match the display on every single blit; converting once when the surface enters the drawImage cache keeps that cost out of the per-frame render path the cache already exists to protect. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Member
Author
|
Self-review rubric:
Summary: internal perf-only fix to drafted by Claude on behalf of Daniel Stephenson |
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
drawImagealready caches the loaded and scaled surface byfilePathto avoid repeating the expensive load/scale work every frame, but the cached surface itself was never converted to the display's pixel format.pygame.Surfaceis reformatted on every singleblit()call (pygame's documented perf guidance is toconvert()/convert_alpha()once after loading). Doing that conversion when the surface first enters the cache keeps that reformat cost out of the per-frame render path the cache exists to protect.convert_alpha()(rather thanconvert()) also preserves per-pixel alpha for images that have it, so transparent PNGs continue to render correctly.No public API surface changed —
Graphik.drawImage's signature and observable caching behavior (per-path cache, rescale-on-size-change) are unchanged; this is purely an internal perf improvement.Test plan
python3 -m py_compile src/main/python/preponderous/graphik/graphik.pypython3 -m pytest— 25 passed (was 24; addedtest_draw_image_converts_loaded_surface_for_faster_blits)SRCALPHAflag absent), restored the fix, confirmed it passes.drawImagecorrectness/caching tests (pixel-color, load-count, scale-count, rescale-on-size-change) all still pass unchanged.No tracking issue — gap found during triage; the backlog (issues/PRs) was empty at the start of this cycle.
This PR description was drafted during a Gardener session (Stephenson-Software/gardener).