Skip to content

audioeffects: each effect loads the first time it's used - #10

Merged
bdbarnett merged 1 commit into
mainfrom
lazy-effect-imports
Nov 9, 2026
Merged

bdbarnett merged 1 commit into
mainfrom
lazy-effect-imports

Conversation

@bdbarnett

Copy link
Copy Markdown
Contributor

import audioeffects imported all 45 effect modules. A program that uses one reverb paid for every effect's code in RAM, and a firmware that freezes the package had to freeze all of it, about 400 KB of bytecode on an ESP32.

Each class is now imported on first use, through the package's __getattr__. from audioeffects import Reverb, audioeffects.Reverb, create() and ALL behave as before. A firmware can freeze just the effects it plays, plus __init__ and _component.

One behaviour changes: on MicroPython, from audioeffects import * now brings in only the effects already loaded, because MicroPython's star import reads the module's names rather than __all__. CPython's star import still loads everything.

I ran tools/scoped_tests.py contract, tools/validate_api.py (45 effects), tests/parity/effects_library_smoke.py (45 classes, 325 patches, 0 failures) and flake8. On CPython and the unix MicroPython port, importing the package loads only _component, and importing Reverb and Chorus loads just those two. The full suite is left to CI. I haven't run it on a board yet.

`import audioeffects` imported all 45 effect modules, so a program that
uses a reverb paid for every effect's code in RAM, and a firmware that
freezes the package had to freeze all of it, about 400 KB of bytecode on
an ESP32.

Each class is now imported on first use, through the package's
__getattr__. `from audioeffects import Reverb`, `audioeffects.Reverb`,
`create()` and `ALL` work as before. A firmware can freeze only the
effects it plays plus `__init__` and `_component`.

One difference: on MicroPython, `from audioeffects import *` now brings
in only the effects already used, since MicroPython's star import reads
the module's names rather than `__all__`.

Checked: tools/scoped_tests.py contract, tools/validate_api.py (45
effects), tests/parity/effects_library_smoke.py (45 classes, 325
patches, 0 failures) and flake8. On CPython and the unix MicroPython
port, importing the package loads only _component, and importing Reverb
and Chorus loads just those two.
@bdbarnett
bdbarnett merged commit 5c87316 into main Nov 9, 2026
8 checks passed
@bdbarnett
bdbarnett deleted the lazy-effect-imports branch November 9, 2026 08:51
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.

1 participant