-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use mpy map and heap where possible, use root pointer #16
base: master
Are you sure you want to change the base?
Conversation
This replaces the use of std::map by micropython heap based maps. As a result, we need a root pointer for the root map such that the mpy garbage collector does not collect the micropython-wrap objects. This root pointer needs to be declared once outside of micropython-wrap such that it is generated by the micropython generators. All objects referenced by the map should also be on the micropython-heap. In this way, the garbage collector can properly track lifetimes. This is the first step into allowing us to use micropython-wrap on mcus, and also allows us to reset the micropython-wrap state by resetting the global state map.
Thanks for the interest, and yes, the overall idea is definitely sound and this would be a welcome addition. Very early on I considered making the whole allocation policy pluggable, exactly for the reasons you're doing it now and others, but IIRC back then there was no easy build support like
Preferably, because that would make it easier to switch implementations for testing than having to change branches. As for the actual shape of this:
Can you lay out other steps? Removing exception handling perhaps? Making some type conversions (strings etc) optional? |
Yes, I think so. For one of the future changes I'd like to introduce an STL compatible custom allocator. I'll describe below why that's needed. This could be used/aliased to either be the default allocator (as implicitly used right now), or the custom allocator that allocates on the mpy heap.
I think we'd need either an entirely seperate heap implementation here (which I would like to avoid) or this is probably not feasible. The micropython heap is quite global.
Yes, will do that. Wanted to post this PR to kind of show the direction this is going.
Next steps are:
Note: I'm not able to transform this into a full |
I've created a first PR here: #18 I'd continue to slowly pull out small steps as PRs to make the allocator and map implementations pluggable. |
Another one for ClassHash: #19 |
I also have another one introducing the root-pointer and |
Ok thanks for the explanation. All sounds pretty sensible. Practical issue for me is that the end of the year traditionally gets super busy so I don't know when I'll have time to review in detail. |
Makes sense, no hurry. If thats ok for you, I'd continue with separating out small nice PRs (and hopefully making the CI work), and we can discuss that whenever you have the time. My team and me are already using an internal fork of micropython-wrap that works with our target toolchain, and I'm just trying to contribute that back upstream, but from my side there is no urgency in this. |
At this point this is more an RFC than a ready PR, as it changes a lot about how micropython-wrap works at the core. Please let me know if you're generally open for merging something like this, maybe behind a `#ifdef' or similar if you want to maintain the old behaviour as well.
My motivation is: We're using micropython-wrap since a while now on a freestanding C++ implementation on a microcontroller. We have no STL (vector, map) available. Also, apart from the micropython garbage collected heap no global heap for C/C++ allocations.
Therefore we replaced the map uses by maps stored on the micropython heap. This of course requires consideration for the garbage collector, as to not accidentally clean up the micropython-wrap generated bindings.
As a nice side effect, we need to neither store the
<class>_globals
nor the_StaticPyObjectStore
in a user-available location.This replaces the use of std::map by micropython heap based maps. As a result, we need a root pointer for the root map such that the mpy garbage collector does not collect the micropython-wrap objects.
This root pointer needs to be declared once outside of micropython-wrap such that it is generated by the micropython generators.
All objects referenced by the map should also be on the micropython-heap. In this way, the garbage collector can properly track lifetimes.
This is the first step into allowing us to use micropython-wrap on mcus, and also allows us to reset the micropython-wrap state by resetting the global state map.