-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
I've had a try at implementing the unsafe `tree_arena` in a separate lib (but in the same workspace for now) - haven't thought of a good way to make non root access not O(depth), without slowing down insertion or using more memory, though have improved accessing nodes from the root to O(1) from O(depth) and accessing direct children to O(1) from O(children)
- Loading branch information
Showing
19 changed files
with
979 additions
and
134 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
[package] | ||
name = "tree_arena" | ||
version = "0.1.0" | ||
description = "An arena allocated tree designed for Linebender" | ||
keywords = ["arena", "tree"] | ||
categories = ["gui"] | ||
edition.workspace = true | ||
rust-version.workspace = true | ||
license.workspace = true | ||
repository.workspace = true | ||
homepage.workspace = true | ||
|
||
|
||
[package.metadata.docs.rs] | ||
all-features = true # all features are enabled - this means that the generated docs are for the safe version | ||
# There are no platform specific docs. | ||
default-target = "x86_64-unknown-linux-gnu" | ||
targets = [] | ||
# rustdoc-scrape-examples tracking issue https://github.com/rust-lang/rust/issues/88791 | ||
# cargo-args = ["-Zunstable-options", "-Zrustdoc-scrape-examples"] # no examples yet | ||
|
||
|
||
[dependencies] | ||
|
||
[features] | ||
# This crate contains two implementations of a tree for use in masonry, one safe and the other unsafe. | ||
# The safe tree is known to work, and serves as the baseline implementation and is used by default. | ||
# The unsafe tree leverages a hashmap as an arena and is designed for higher performance: it leverages unsafe code to achieve this. | ||
# The unsafe tree is not yet fully tested, and is not used by default. | ||
default = ["safe_tree"] | ||
safe_tree = [] | ||
|
||
[lints] | ||
workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Tree Arena | ||
|
||
This crate contains two implementations of a tree for use in masonry, one safe and the other unsafe. The safe tree is known to work, and serves as the baseline implementation and is used by default. The unsafe tree leverages a hashmap as an arena and is designed for higher performance: it leverages unsafe code to achieve this. The unsafe tree is not yet fully tested, and is not used by default. | ||
|
||
The safe tree is the priority. This means: | ||
|
||
* The safe version may have features / APIs that the unsafe version doesn't yet have. | ||
|
||
* If both versions are at feature parity, Masonry can switch on the unsafe version for best performance. | ||
|
||
* Otherwise, Masonry uses the safe version. | ||
|
||
## Architecture | ||
|
||
### Safe Tree | ||
|
||
The safe tree contains a root `TreeArena` which owns the root nodes as `Vec<TreeNode<T>>`, and a`parents_map` tracking the parent of every node. Each `TreeNode` subsequently owns its own children as `Vec<TreeNode<T>>`. This model of owneship is thus checked by the rust compiler, but has the downside of requiring passing through every ancestor node to access the descendant - this requires an O(depth) determination of whether the node is a descendant, followed by O(children) time at each level to traverse the path to the child. | ||
|
||
### Unsafe Tree | ||
|
||
The unsafe tree arena contains a `DataMap` which **owns** all nodes. The `DataMap` contains: | ||
|
||
* A `HashMap` associating `NodeId` with `Box<UnsafeCell<TreeNode<T>>>`, owning the node data, (boxed to prevent movement of the node when the `HashMap` is resized and `UnsafeCell` to express the interior mutability) | ||
|
||
* A `HashMap` associating `NodeId` with `Option<NodeId>`, containing the parent information for the nodes | ||
|
||
* `Box<UnsafeCell<Vec<NodeId>>>` containing the roots of the tree | ||
|
||
It is possible to get shared (immutable) access or exclusive (mutable) access to the tree. These return `ArenaRef<'arena, T>` or `ArenaMut<'arena, T>` respectively. We do this by leveraging a hash map to store the nodes: from this we can obtain either shared or exclusive access to nodes. To ensure that only one item is allowed to create new exclusive access to nodes, this action requires mutable access to the arena as a whole (and so is checked by the compiler) - what the compiler cannot check is that the nodes accessed mutably are distinct from one another - this is done by only allowing access to descendants of the node being accessed mutably. The aim of this is to reduce the time needed to access node, as given a node, we only need to determine whether it is a descendant of the node being accessed mutably, and do not need to iterate over the children and to flatten the overall tree graph into a hash map. | ||
|
||
#### Shared References | ||
|
||
`ArenaRef<'arena, T>` contains the identity of the parent node, a reference to the node data, and `ArenaRefChildren<'arena, T>`. The `ArenaRefChildren<'arena, T>` contains the ids of the children of the node, the id of the node, and a reference to the arena. From this `ArenaRefChildren<'arena, T>` it is possible to get shared access to children of the node. | ||
|
||
#### Exclusive References | ||
|
||
`ArenaMut<'arena, T>` contains the identity of the parent node, a mutable reference to the node data, and `ArenaMutChildren<'arena, T>`. The `ArenaMutChildren<'arena, T>` contains the ids of the children of the node, the id of the node, and a mutable reference to the arena. From this `ArenaMutChildren<'arena, T>` it is possible to get exclusive access to children of the node. | ||
|
||
#### Safety | ||
|
||
From the `ArenaMutChildren<'arena, T>`, it is important that we can only access descendants of that node, such that we can only ever have exclusive mutable access to the contents of a node, and never have multiple mutable references. This invariant is not checked by the compiler and thus relies on the logic to determine whether a node is a descendant being correct. | ||
|
||
### Complexity | ||
|
||
|Operation | Safe | Unsafe | | ||
| --- | --- | --- | | ||
|Find child | O(Children) | O(1) | | ||
|Descendant | O(Depth) | O(Depth) | | ||
|From root | O(Depth) | O(1) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright 2024 the Xilem Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
//! This crate implements a tree data structure for use in Masonry | ||
//! It contains both a safe implementation (that is used by default) | ||
//! and an unsafe implementation that can be used to improve performance | ||
//! | ||
//! The safe version is the first class citizen | ||
//! | ||
//! * The safe version may have features / APIs that the unsafe version doesn't yet have. | ||
//! * If both versions are at feature parity, Masonry can switch on the unsafe version for best performance. | ||
//! * Otherwise, Masonry uses the safe version. | ||
type NodeId = u64; | ||
|
||
#[cfg(not(feature = "safe_tree"))] | ||
mod tree_arena_unsafe; | ||
#[cfg(not(feature = "safe_tree"))] | ||
pub use tree_arena_unsafe::*; | ||
|
||
#[cfg(feature = "safe_tree")] | ||
mod tree_arena_safe; | ||
#[cfg(feature = "safe_tree")] | ||
pub use tree_arena_safe::*; |
Oops, something went wrong.