adafruit / circuitpython

CircuitPython - a Python implementation for teaching coding with microcontrollers
https://circuitpython.org
Other
4.09k stars 1.21k forks source link

rgbmatrix: decrease RAM usage over soft resets #3499

Closed jepler closed 3 years ago

jepler commented 4 years ago

.. implement #3498 but for rgbmatrix. will require the same infrastructure commits to be merged in first.

tannewt commented 4 years ago

@jepler Do we need to fix this for 6.0.0?

jepler commented 4 years ago

No.

jepler commented 3 years ago

@cwalther was this addressed by the enhancements to supervisor allocations or does it still need to be done?

cwalther commented 3 years ago

As far as I understand it is addressed. Looking at a4b84cf, the previous implementation would allocate preferentially on the GC heap, while the new one should reuse previously freed supervisor allocations when possible.

Empirical result: In a quick test I’m seeing considerably less GC memory use after initializing an RGBMatrix display for the second time after a soft reset, so it seems to be effective.

Before:

Adafruit CircuitPython 6.1.0-beta.1-143-g299b6efd8-dirty on 2020-12-05; PewPew M4 CW with samd51G19
>>> import rgbmat_display
158464 880 157584
158464 27776 130688
>>> 
soft reboot

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
Hello World!

Press any key to enter the REPL. Use CTRL-D to reload.
Adafruit CircuitPython 6.1.0-beta.1-143-g299b6efd8-dirty on 2020-12-05; PewPew M4 CW with samd51G19
>>> import displayio; displayio.release_displays()
>>> import rgbmat_display
132352 960 131392
132352 27840 104512
>>> 
soft reboot

After:

Adafruit CircuitPython 6.1.0-beta.2-5-ga95c972b5-dirty on 2020-12-05; PewPew M4 CW with samd51G19
>>> import rgbmat_display
158464 880 157584
158464 27776 130688
>>> 
soft reboot

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
Hello World!

Press any key to enter the REPL. Use CTRL-D to reload.
Adafruit CircuitPython 6.1.0-beta.2-5-ga95c972b5-dirty on 2020-12-05; PewPew M4 CW with samd51G19
>>> import displayio; displayio.release_displays()
>>> import rgbmat_display
132288 960 131328
132288 3136 129152
>>> 
soft reboot

rgbmat_display.py:

import displayio
import framebufferio
import rgbmatrix
import gc
import microcontroller.pin as pin

gc.collect();
a, f = gc.mem_alloc(), gc.mem_free()
print(a+f, a, f)

#displayio.release_displays()
rgbm = rgbmatrix.RGBMatrix(width=32, bit_depth=6, rgb_pins=[pin.PB02, pin.PB03, pin.PB08, pin.PB09, pin.PB10, pin.PB11], addr_pins=[pin.PA20, pin.PA21, pin.PA22, pin.PA23], clock_pin=pin.PB22, latch_pin=pin.PB23, output_enable_pin=pin.PA27)
display = framebufferio.FramebufferDisplay(rgbm, auto_refresh=True)

gc.collect()
a, f = gc.mem_alloc(), gc.mem_free()
print(a+f, a, f)
jepler commented 3 years ago

excellent, thank you again