Skip to content

FrameBuffer: areas=False draws without making an Area per call - #105

Merged
bdbarnett merged 2 commits into
mainfrom
area-switch-104
Nov 20, 2026
Merged

bdbarnett merged 2 commits into
mainfrom
area-switch-104

Conversation

@bdbarnett

Copy link
Copy Markdown
Contributor

A program that draws every frame and never reads the returned areas can now make its framebuffer with areas=False. Every drawing call on it returns None, whether through the framebuffer's own methods, a Draw on it, or a module function such as pygraphics.text8(fb, ...). On MicroPython with the compiled module, that drawing allocates nothing; before, each call made a 32-byte Area, and a few hundred calls a frame made kilobytes of garbage. fb.areas reads the setting. Fixes #104. Refs PyDevices/apps#189.

The default is unchanged, and the switch is opt-in on purpose. Code in the wild holds areas across later draws on the same framebuffer: pdwidgets' Console keeps a list of them and adds them up, and the gallery's blackjack holds one for a whole hand. A single reused Area would have changed their results without an error. It is a constructor keyword rather than a settable attribute because on MicroPython a Python subclass's attribute store never reaches the native object, so setting it there would silently do nothing. Subclasses pass the keyword through super().__init__.

On unix MicroPython the issue's reproduction gives 32.0 bytes a call by default and 0.0 with areas=False. The new tests/test_areas_off.py checks every drawing call both ways, that both draw the same pixels, subclasses and clips, and on MicroPython that nine kinds of call (fill_rect, text, text16, blit_alpha, blit, circle, Draw.circle, Draw.text, text8) allocate 0 bytes. The suite passes on pure Python (160) and the CPython extension (147), and tools/compare_graphics_run.py passes all 423 checks. Speed with the default is unchanged within the noise of a shared machine: best of five, in ns per call on unix MicroPython, fill_rect went from 401 to 388, pixel from 266 to 289, text from 1013 to 890, Draw.circle from 639 to 576, and with areas=False fill_rect takes 304. I haven't built it for CircuitPython.

FrameBuffer(buf, w, h, fmt, areas=False) makes every drawing call on that
framebuffer return None instead of a new Area: its own methods, a Draw on
it, and the module functions given it. On MicroPython with the compiled
module that drawing allocates nothing, where each call made a 32-byte Area
before. The default is unchanged, because callers keep areas past later
draws (lists of dirty rectangles, areas added together), so a shared,
reused Area would have changed their results.

tests/test_areas_off.py checks every drawing call both ways, that both
draw the same pixels, subclasses, clips, and on unix MicroPython that nine
kinds of call allocate 0 bytes. Pure Python and the CPython extension pass
the suite; tools/compare_graphics_run.py passes 423 checks.

Fixes #104
@bdbarnett
bdbarnett merged commit 5c3b363 into main Nov 20, 2026
7 checks passed
@bdbarnett
bdbarnett deleted the area-switch-104 branch November 20, 2026 17:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Every drawing call allocates an Area, so per-frame drawing makes garbage on MicroPython

1 participant