Collection wrapped in a Box that adds a second allocation
ID |
rust.performance.box_collection |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Inefficient Api Usage |
Language |
Rust |
Description
Vec, String and the standard maps and sets already keep their contents on the heap and are themselves only a few words wide, so wrapping one in a Box buys nothing and adds a second allocation plus an extra pointer hop on every access. Drop the Box and use the collection directly.
Rationale
Vec, String and the standard maps and sets already keep their contents on the heap and are themselves only a few words wide, so wrapping one in a Box buys nothing and adds a second allocation plus an extra pointer hop on every access. Drop the Box and use the collection directly.
The following code illustrates the pattern detected by this rule:
use std::collections::{BTreeMap, HashMap, HashSet};
// FLAGGED: Collection wrapped in a Box that adds a second allocation
fn build(items: Box<Vec<u32>>) -> u32 {
items.len() as u32
}