我正在尝试使用Node
表示形式构建树状数据结构:
use std::cmp::Ordering;
use std::fmt::Debug;
use std::sync::Arc;
const BRANCH_FACTOR: usize = 32;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
enum Node<T> {
Branch {
children: [Option<Arc<Node<T>>>; BRANCH_FACTOR],
len: usize,
},
RelaxedBranch {
children: [Option<Arc<Node<T>>>; BRANCH_FACTOR],
sizes: [Option<usize>; BRANCH_FACTOR],
len: usize,
},
Leaf {
elements: [Option<T>; BRANCH_FACTOR],
len: usize,
},
}
枚举的RelaxedBranch
变体将很少使用,有时甚至根本不使用。由于Rust中枚举的大小由最大变体的大小定义,因此RelaxedBranch
总体上大大增加了Node
的内存占用量。此枚举的较大大小会导致20%的性能下降,在我的情况下这是不可接受的。
作为枚举的替代方法,我决定尝试特征对象:
use std::cmp::Ordering;
use std::fmt::Debug;
use std::sync::Arc;
const BRANCH_FACTOR: usize = 32;
trait Node<T>: Debug + Clone + PartialEq + Eq + PartialOrd + Ord {}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
struct Branch<T> {
children: [Option<Arc<Node<T>>>; BRANCH_FACTOR],
len: usize,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
struct RelaxedBranch<T> {
children: [Option<Arc<Node<T>>>; BRANCH_FACTOR],
len: usize,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
struct Leaf<T> {
elements: [Option<T>; BRANCH_FACTOR],
len: usize,
}
impl<T> Node<T> for Branch<T> {}
impl<T> Node<T> for RelaxedBranch<T> {}
impl<T> Node<T> for Leaf<T> {}
我很快发现了一种称为特征对象安全性的东西:)这不允许用于特征对象的特征返回Self
类型的对象,这是我的情况,原因是继承自Clone
。
如何在没有枚举开销的情况下表示树节点?
答案 0 :(得分:3)
这里不需要特质对象,因为您不需要它们提供的无限多态性。
Clippy告诉您该怎么做:
warning: large size difference between variants
--> src/main.rs:13:5
|
13 | / RelaxedBranch {
14 | | children: [Option<Arc<Node<T>>>; BRANCH_FACTOR],
15 | | sizes: [Option<usize>; BRANCH_FACTOR],
16 | | len: usize,
17 | | },
| |_____^
|
= note: #[warn(large_enum_variant)] on by default
help: consider boxing the large fields to reduce the total size of the enum
--> src/main.rs:13:5
|
13 | / RelaxedBranch {
14 | | children: [Option<Arc<Node<T>>>; BRANCH_FACTOR],
15 | | sizes: [Option<usize>; BRANCH_FACTOR],
16 | | len: usize,
17 | | },
| |_____^
= help: for further information visit https://rust-lang-nursery.github.io/rust-clippy/v0.0.211/index.html#large_enum_variant
切换到
RelaxedBranch {
children: Box<[Option<Arc<Node<T>>>; BRANCH_FACTOR]>,
sizes: Box<[Option<usize>; BRANCH_FACTOR]>,
len: usize,
},
将Node<()>
的大小从784减少到272。您可以通过将所有字段组合成一个新的结构并将其装箱来进行进一步操作。