Replies: 2 comments 2 replies
-
Hi @tannewt - this is interesting work, especially as someone that does a bunch of work on C modules etc. I do not have any opinion on the API design at the moment, I would like to understand the intended usage a bit better first. What would be the typical cases where one would use this functionality, and how to decide if to use it or not. I guess any large allocation where the code that allocates knows there will not be any MP objects inside? And I guess the default behavior would have to be "do collection?", to maintain compatibility? |
Beta Was this translation helpful? Give feedback.
-
This sounds like a useful feature! IMO this should probably be made the default for the python-facing I think it would be better to call this
I'm not quite familiar enough with the internals of how the GC's tables work to say for sure how easy this would be from a technical standpoint, but could this be implemented as its own entirely separate call? E.g: ptr = mp_malloc(len);
mp_gc_weakdata(ptr, len); Or even a more generic ptr = mp_malloc(len);
mp_madvise(ptr, len, MP_MADV_WEAKDATA); That would also give an obvious place to put an implementation of
There are supposed to be code size advantages to having so many different allocation functions --- though I agree we should avoid proliferating even more versions of |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
In CircuitPython we're working on supporting our new Fruit Jam board. It uses HSTX to generate DVI video and PSRAM for a larger VM heap (via split heaps). One issue with our demos is that GC collect time gets long enough to chirp audio and stutter video. The heap grows significantly because we store bitmaps and audio buffers in the VM heap.
An idea I had to speed this up was to annotate which VM allocations need to be "collected" (aka scanning for heap pointers.) I've implemented this by adding another table like the one for finalisers. GC times dropped from 80+ms to ~25ms in our test example.
One question I struggled with is how to update the dozen or so memory allocation APIs. Some vary to allocate a struct or a variable length struct. Others vary depending on whether they should raise an error on failure, have a finaliser or zero memory. Now I'm adding a fourth boolean of whether the memory should be collected. What should the APIs be? Can we simplify things by splitting the size computation from the allocation policy?
Existing APIs
micropython/py/misc.h
Lines 70 to 115 in f498a16
micropython/py/obj.h
Lines 922 to 936 in f498a16
Another approach would be to focus on the type of allocations being made.
mp_obj_malloc
is a good example of this. It makes it clear that the allocated memory is a Python object. Therefore it could have finaliser always set and always be collected.How could we unify these allocation APIs to make them easier to maintain? Any ideas?
Beta Was this translation helpful? Give feedback.
All reactions