mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2025-01-03 11:25:20 +00:00
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:
parent
18f7a37b48
commit
c56a3ce59a
@ -109,7 +109,8 @@ pub struct BftIter<'a> {
|
|||||||
|
|
||||||
impl<'a> BftIter<'a> {
|
impl<'a> BftIter<'a> {
|
||||||
fn new(hash_map: &'a HashMap<String, DeviceNode>) -> Self {
|
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() {
|
for (_, node) in hash_map.iter() {
|
||||||
if node.parent.is_none() {
|
if node.parent.is_none() {
|
||||||
@ -117,26 +118,13 @@ impl<'a> BftIter<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut node_layer = nodes.as_slice();
|
while i < nodes.len() {
|
||||||
loop {
|
for child_node_id in nodes[i].children.iter() {
|
||||||
let mut next_node_layer = Vec::new();
|
if let Some(child_node) = hash_map.get(child_node_id) {
|
||||||
|
nodes.push(child_node);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
i += 1;
|
||||||
if next_node_layer.is_empty() {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
let pos = nodes.len();
|
|
||||||
nodes.extend(next_node_layer);
|
|
||||||
|
|
||||||
node_layer = &nodes[pos..];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
BftIter { nodes }
|
BftIter { nodes }
|
||||||
|
Loading…
Reference in New Issue
Block a user