[swift-users] Problem with COW optimization
Adrian Zubarev
adrian.zubarev at devandartist.com
Sun Sep 18 03:16:22 CDT 2016
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:
<root>
<item/>
</root>
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")
root.add(item)
// The problematic behavior
root.add(root)
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:
<root>
<item/>
<root>
<item/>
</root>
</root>
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160918/0c9ac88a/attachment.html>
More information about the swift-users
mailing list