Skip to content

Conversation

@dlech
Copy link
Member

@dlech dlech commented Jul 17, 2025

In pybricks/support#2154 (comment) (cc: @schodet), it came up that we were running out of MicroPython heap space on EV3 when trying to allocate full-screen sized images. We really don't want to use MicroPython heap for this because the image data will contain no pointers to MicroPython objects (other than false positives) and will make allocating and collecting memory in MicroPython much slower than it needs to be.

Instead, we can add a new memory allocator that doesn't use garbage collection. I've picked one created by our old MINDSTORMS pal @rhempel that seem well suited to the task. In addition to being designed for microcontrollers, it also has a nice debug print feature which I have wired up to hub.system.umm_info() that works similar to micropython.mem_info() that will help us debug memory issues if needed.

The block size and heap size was just an educated guess, but we can tune those as needed based on real-world usage.

@dlech dlech requested a review from laurensvalk July 17, 2025 03:57
@coveralls
Copy link

coveralls commented Jul 17, 2025

Coverage Status

coverage: 57.254% (+0.02%) from 57.239%
when pulling 7a850bd on dlech:image-malloc
into 4484552 on pybricks:master.

Copy link
Member

@laurensvalk laurensvalk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

(Nice one pulling @rhempel into this 😄)

@laurensvalk
Copy link
Member

  1. Could we perhaps also use this opportunity to set the following to sensible values? These are just pseudorandom values to get things to build initially. It's no surprise that this was quickly running our of MicroPython heap.
#define PBDRV_CONFIG_BLOCK_DEVICE_RAM_SIZE          (10 * 1024)
#define PBDRV_CONFIG_BLOCK_DEVICE_EV3_SIZE          (8 * 1024)
#define PBDRV_CONFIG_BLOCK_DEVICE_EV3_SIZE_USER     (512)
#define PBDRV_CONFIG_BLOCK_DEVICE_EV3_START_ADDRESS (10 * 1024 * 1024)
  1. While we are working on the Image class, it would be nice to also move it to its intended destination in pybricks.parameters.Image. This follows the pattern established by Icon and Color and it ensures we don't have to have a module for just one or two classes. See [EV3 - help wanted]: Implement display API support#2154 (comment) for rationale.

@rhempel
Copy link

rhempel commented Jul 17, 2025 via email

@schodet
Copy link
Contributor

schodet commented Jul 18, 2025

Looks good, thanks!

dlech added 5 commits July 18, 2025 16:47
Add support for a new memory allocator (umm_malloc) that is better
for allocating large blocks of memory. Since MicroPython's garbage
collector has to scan every single byte of memory it manages, to check
for pointers, it is not efficient for large blocks of memory that
are not expected to contain pointers. This is especially true for
image data.

So to avoid slow garbage collection/allocation times and filling up
the MicroPython heap, we can use a separate allocator with a separate
heap for image data.
Pull in a change to support %i in printf format strings. This is needed
by the umm_malloc library.
Add a umm_info() to print debug info about umm memory usage similar to
micropython.mem_info(). This is used on EV3 for allocating memory for
images.
This was left over from when we had the u-boot.bin binary committed to
the repo, so we don't need it anymore.
Mechanically sort lists of source files in makefiles using VS Code.
Some of these were starting to get out of order.
@dlech
Copy link
Member Author

dlech commented Jul 18, 2025

  1. Could we perhaps also use this opportunity to set the following to sensible values?

I'm not really sure what sensible values would be at this time. I think we should figure out how we plan to use all of the flash and RAM (make a complete map of both), then we can make a better educated guess.

2. it would be nice to also move it to its intended destination in pybricks.parameters.Image

Done.

dlech added 2 commits July 18, 2025 17:36
Move pb_type_image from media to parameters to be consistent with
Pybricks v3.x (media module was a 2.x thing for ev3dev).
Fix the alignment of the (x) in #defines in mpconfigvariant.h for
virtualhub. This was the only file that has a different alignment which
made copy/paste with other config files annoying.
@dlech dlech merged commit c14499c into pybricks:master Jul 18, 2025
17 checks passed
@dlech dlech deleted the image-malloc branch July 18, 2025 22:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants