KWARC / rust-libxml

Rust wrapper for libxml2
https://crates.io/crates/libxml
MIT License
76 stars 38 forks source link

Downgrade Node, Context and Object document references to Weak #42

Closed dginev closed 6 years ago

dginev commented 6 years ago

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 into Weak<> 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:

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 ...

triptec commented 6 years ago

Could you add a test that when running without those changes would exhibit the memory leak? I think your changes makes sense and that you should merge

dginev commented 6 years ago

I'll work on the tests in the coming weeks, it's "awkward" to write leakage tests... But I agree we should have them. I also want to release the version with the fix to make it a dependency "officially", so merging here - thanks for reviewing!