Downgrade Node, Context and Object document references to Weak #42
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
CC @triptec , would be good to get a review if you have some a bit of time on your hands.
The story here is as follows. I hit memory leak problems in one of the projects I have using rust-libxml, where I have a
corpus-level iterator
that traverses multiple documents, and parses them into libxml as you.next()
through the iterator.Before the big Node refactor, that code worked very elegantly, in almost constant memory, and gracefully deallocated each libxml2 Document (and its sub-objects) as the iterator proceeded to the next one. After, I observed memory leaking, as (a portion of) the allocated memory for each document remained present for the full run.
I had some suspicions and indeed - it turned out that the
Rc<>
wrappers ended up impossible to deallocate in my setup due to having references to a document in multiple levels of a deep data structure. It was also extremely confusing to 1) localize and 2) understand the details of how this leakage occurred. It is both silent and hard to grasp, and it isn't helping that the particular project can't run under valgrind for separate and unrelated reasons.So, anyhow, there is an obvious way to relax our design to avoid unneeded "memory hogging", which is to downgrade the
Rc<>
wrappers intoWeak<>
wrappers, that do not enforce ownership.This PR takes a stab at that, and indeed I can report my project is back to constant memory use, and is leak-free. Things I am not fully happy about:
.unwrap()
, especially since if the main document Rc is no longer in scope, it will panic the entire process. Thinking how to improve without introducing endless result types...unlinked
and all rust code should be guarding using that flag, against directly accessing the Weak reference.That's about all... I am quite happy to have solved the memory leak, so I am quite confident we need a solution in this vein ...