Pointer type wrapping another pointer type
ID |
rust.performance.redundant_allocation |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Inefficient Api Usage |
Language |
Rust |
Description
Box, Rc and Arc each own a heap allocation on their own, so nesting one inside another allocates twice and adds a pointer hop to every dereference without changing the ownership semantics. Keep the outer pointer and drop the inner one.
Rationale
Box, Rc and Arc each own a heap allocation on their own, so nesting one inside another allocates twice and adds a pointer hop to every dereference without changing the ownership semantics. Keep the outer pointer and drop the inner one.
The following code illustrates the pattern detected by this rule:
use std::cell::RefCell;
use std::rc::Rc;
use std::sync::{Arc, Mutex};
struct Node {
v: u32,
}
// FLAGGED: Pointer type wrapping another pointer type
fn share(node: Rc<Box<Node>>) -> u32 {
node.v
}