Repository navigation
perf: simplify render_text with one growable canvas per height - #19
Merged
Merged
Conversation
- Replace the fixed 1024px reusable surface plus a per-call surface for wider text with one canvas per block_size that grows when needed, so long texts also reuse their surface. - Set the black text source once per canvas and fill the white background through the numpy view (with mark_dirty/flush), dropping three cairo calls per render. - Replace the global measurement-context bookkeeping with a @cache'd layout getter. - Drop a redundant ascontiguousarray in render_signwriting and fix the block_size default in the docstring. Output is pixel-identical (4,420 renders compared against main, mixed sizes). Short words are ~12% faster; long text ~1% faster. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Fy1wuWdqcKuebgj3St8dME
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.
Simplifies
pixel_renderer/renderer.py(−79/+40 lines) and makes short-text rendering ~12% faster, with pixel-identical output.Changes
block_size. This replaces the fixed 1024px reusable surface plus a fresh surface/context for every wider text. Long texts now reuse their surface too, and the two-branch logic is gone.mark_dirty()before drawing,flush()before reading).@cache'd layout getter in place of the global_measurement_contexttuple.np.ascontiguousarrayinrender_signwritingand fixed the docstring'sblock_sizedefault (16, not 32).Benchmarks
Ubuntu 24.04 container matching CI (pango 1.52.1, cairo 1.18.0, Python 3.12), aarch64, best of several runs:
benchmark.pyword mixlong_text_benchmark.py(512 words)Correctness
render_text: regression samples, benchmark words, the long lorem text, empty / newline / 3,000-char / SignWriting inputs, at sizes (16,12), (32,24), (16,12), (24,16) interleaved so canvases get both reused and grown. 0 mismatches, same exceptions.Where the rest of the time goes (why this isn't faster)
Profiled per call:
I tried and rejected:
V3views, uint32 shifts and PIL'sBGRXunpacker were all slower than the existing three strided assignments, which run close to memory bandwidth.Pango.itemize/shape. Fragile through GI, and unlikely to stay pixel-identical (bidi reordering, etc.).🤖 Generated with Claude Code
https://claude.ai/code/session_01Fy1wuWdqcKuebgj3St8dME
Note
Low Risk
Performance refactor in rendering only; PR reports pixel-identical output across thousands of regression renders and full tests.
Overview
Refactors Pango/Cairo text rendering in
renderer.pyfor speed and simpler reuse: one@cache’d shared layout replaces the old measurement-context globals, and one growable canvas per line height (with a cached numpy pixel view) replaces the fixed 1024px surface plus a separate path for wide text.render_textnow clears the needed width viapixels.fill(255)andmark_dirty(), draws once, thenflush()beforebgra_to_rgb; long strings reuse the same surface after it grows.render_signwritingdrops redundantascontiguousarray, and theblock_sizedoc default is corrected to 16.Reviewed by Cursor Bugbot for commit c582f77. Bugbot is set up for automated code reviews on this repo. Configure here.