vmm: reduce memory copy when BFT device tree

The current implementation of breadth first traversal for device tree
uses a temporary vector, therefore causes unnecessary memory copy.
Remove it and do it within vector nodes.

Signed-off-by: Hao Xu <howeyxu@tencent.com>
This commit is contained in:
Hao Xu 2023-05-09 15:26:06 +08:00 committed by Rob Bradford
parent 18f7a37b48
commit c56a3ce59a

View File

@ -109,7 +109,8 @@ pub struct BftIter<'a> {
impl<'a> BftIter<'a> {
fn new(hash_map: &'a HashMap<String, DeviceNode>) -> Self {
let mut nodes = Vec::new();
let mut nodes = Vec::with_capacity(hash_map.len());
let mut i = 0;
for (_, node) in hash_map.iter() {
if node.parent.is_none() {
@ -117,26 +118,13 @@ impl<'a> BftIter<'a> {
}
}
let mut node_layer = nodes.as_slice();
loop {
let mut next_node_layer = Vec::new();
for node in node_layer.iter() {
for child_node_id in node.children.iter() {
if let Some(child_node) = hash_map.get(child_node_id) {
next_node_layer.push(child_node);
}
while i < nodes.len() {
for child_node_id in nodes[i].children.iter() {
if let Some(child_node) = hash_map.get(child_node_id) {
nodes.push(child_node);
}
}
if next_node_layer.is_empty() {
break;
}
let pos = nodes.len();
nodes.extend(next_node_layer);
node_layer = &nodes[pos..];
i += 1;
}
BftIter { nodes }