#reference-counting #tree #atomic #implemented #dom-like

arctree

A 'DOM-like' tree implemented using atomic reference counting

1 unstable release

0.1.0 Apr 6, 2024

#47 in #reference-counting

Download history 1/week @ 2024-04-29 23/week @ 2024-05-20 6/week @ 2024-05-27 14/week @ 2024-06-03 13/week @ 2024-06-10 6/week @ 2024-06-17 7/week @ 2024-06-24 5/week @ 2024-07-01 6/week @ 2024-07-08 7/week @ 2024-07-15 5/week @ 2024-07-22 36/week @ 2024-07-29 5/week @ 2024-08-05

53 downloads per month
Used in 4 crates (via lotus-lib)

MIT license

33KB
474 lines

arctree

Build Status Crates.io Documentation Rust 1.49.0

arctree is a "DOM-like" tree implemented using atomic reference counting.

Origin

This is a fork of rctree.

Details

"DOM-like" here means that data structures can be used to represent the parsed content of an HTML or XML document, like the DOM does, but don't necessarily have the exact same API as the DOM. That is:

  • A tree is made up of nodes.
  • Each node has zero or more child nodes, which are ordered.
  • Each node has a no more than one parent, the node that it is a child of.
  • A node without a parent is called a root.
  • As a consequence, each node may also have siblings: its parent's other children, if any.
  • From any given node, access to its parent, previous sibling, next sibling, first child, and last child (if any) can take no more than O(1) time.
  • Each node also has data associated to it, which for the purpose of this project is purely generic. For an HTML document, the data would be either the text of a text node, or the name and attributes of an element node.
  • The tree is mutable: nodes (with their sub-trees) can be inserted or removed anywhere in the tree.

The lifetime of nodes is managed through atomic reference counting. To avoid reference cycles which would cause memory leaks, the tree is asymmetric: each node holds optional strong references to its next sibling and first child, but only optional weak references to its parent, previous sibling, and last child.

Nodes are destroyed as soon as there is no strong reference left to them. The structure is such that holding a reference to the root is sufficient to keep the entire tree alive. However, if for example the only reference that exists from outside the tree is one that you use to traverse it, you will not be able to go back "up" the tree to ancestors and previous siblings after going "down", as those nodes will have been destroyed.

Weak references to destroyed nodes are treated as if they were not set at all. (E.g. a node can become a root when its parent is destroyed.)

Since nodes are aliased (have multiple references to them), RwLock is used for interior mutability.

Advantages:

  • A single Node user-visible type to manipulate the tree, with methods.
  • Memory is freed as soon as it becomes unused (if parts of the tree are removed).

Disadvantages:

  • Any tree manipulation, including read-only traversals, requires incrementing and decrementing reference counts, which causes run-time overhead.
  • Nodes are allocated individually, which may cause memory fragmentation and hurt performance.

Minimum Supported Rust Version (MSRV)

The current MSRV is 1.49.0 due to the parking_lot dependency.

License

arctree is licensed under the MIT License.

Dependencies

~0.4–5.5MB
~11K SLoC