Dear Swift community,

currently I’m building a value type XML library which is baked behind the scene 
with a reference type to manage graph traversing between nodes. I also like to 
COW optimize the xml graph, but I run into one single problem atm.

Image this xml tree:

It’s just a root element with one single child. As for value types it should be 
totally fine to do something like this:

// The given xml tree
var root = XML.Element(name: "root")
let item = XML.Element(name: "item")

// The problematic behavior
If this would be a simple value type without any references behind the scenes 
you could imagine that the result of the last code line will look like this:

Basically we copied the whole tree and added it as the second child into the 
original root element.

As for COW optimization this is a problem, just because the passed root is a 
copy of a struct that contains the exact same reference as the original root 
element. isKnownUniquelyReferenced(&self.reference) will result in false inside 
the add method.

Is there any chance I could force my program to decrease the reference counter 
of that last item after I’m sure I don’t need it?!

A few more details: inside the add method I’m always cloning the passed 
reference just because graphs aren’t that trivial and otherwise I could 
possibly end up with a cycle graph, which would be really bad. After that job 
I’m sure that I don’t need the passed reference anymore and I need a way to 
escape from it.

I’d appreciate any suggestions and help. :)

Adrian Zubarev
Sent with Airmail
swift-users mailing list

Reply via email to