Avoid over allocations & reallocations

As we know, vectors amortize the cost of adding new elements by reserving
space for multiple elements when full. This is useful but may lead to
allocating more memory than necessary.

`relibc` generally avoids over allocations by reserving the exact amount
of space when possible. I fixed a few areas that still over allocated or
reallocated unnecessarily by leveraging iterators that are more likely
to know sizes.
This commit is contained in:
Josh Megnauth
2024-11-12 23:53:45 -05:00
parent a45c7b26d9
commit d6ac5f8947
6 changed files with 74 additions and 89 deletions
+1 -4
View File
@@ -219,10 +219,7 @@ impl Pal for Sys {
}
fn fpath(fildes: c_int, out: &mut [u8]) -> Result<usize> {
let mut proc_path = b"/proc/self/fd/".to_vec();
write!(proc_path, "{}", fildes).unwrap();
proc_path.push(0);
let proc_path = format!("/proc/self/fd/{}\0", fildes).into_bytes();
Self::readlink(CStr::from_bytes_with_nul(&proc_path).unwrap(), out)
}