Design Pattern For "Correct" Memory Management #2114
Replies: 2 comments
-
Shameless self-bump 🥲. Desperately hoping to catch @davidhewitt's attention... |
Beta Was this translation helpful? Give feedback.
-
Sorry for the slow reply; very busy recently and can only find so much time to respond to PyO3 things. Longer requests like this which require a bit of context switch kinda get buffered until I have time to sit down and think about them for a moment.
I'm not sure exactly what you're aiming for; the most "memory efficient" way would be to pack the memory into some kind of single Rust-owned buffer, which Python could read parts of. (e.g. think something like a
|
Beta Was this translation helpful? Give feedback.
-
Quick background:
Python developer
By way of accomplishing the above, I'm working on writing a PEP 249 compliant interface for reading (and eventually writing) a really niche type of embedded "database" file (DataFlex v2.3b/3.0) that's been effectively EOL'd for... years now.
Currently, I've got all of the necessary machinery in place to read the table files correctly with all of the helper functions and "core" objects implemented as
#[pyclass]
decorated structs.maturin
and poetry seem to play nice, and I can import my module and call the functions from Python as advertised, so on and so forth.What I'm... struggling with (for lack of a better word) is gathering information on implementing the best (or even a good) design pattern from a memory + interoperability standpoint. Like I said, I've got table reads working nicely, but I'm like 90% certain that my implementation isn't as memory-friendly as is could / should be.
For instance, all of the "core" helper functions I've implemented look something like:
and the various structures that make up the table file's format look something like:
My intuition is that it would be better to refactor everything such that all "new" data is allocated directly on the Python heap, and all "existing" data is only ever passed to the Rust "side" of things as something like
PyRef<DataType>
(or whatever the appropriate type would be) so as to minimize duplicate allocations / extra "work" on the part of the Rust functions.Is that even possible? If so, how? I keep running smack into the need to call
.clone()
on values in order for them to be returnable from Rust functions back to the Python "side" of things.If it's not possible, what's the recommended / idiomatic strategy or pattern I should shoot for?
Any guidance y'all could offer would be much appreciated!
Beta Was this translation helpful? Give feedback.
All reactions