Skip to content

Latest commit

 

History

History
6 lines (6 loc) · 1.72 KB

File metadata and controls

6 lines (6 loc) · 1.72 KB

2024-06-07 - Avoid format! for single integer to string conversions

Learning: format!("{var}") invokes alloc::format! which requires parsing the format string and comes with formatting overhead. For simply converting an integer to a string, calling var.to_string() (from alloc::string::ToString trait) is significantly faster in #![no_std] as it relies on optimized itoa under the hood and exactly allocates the capacity. Action: Replace format!("{}", fd) with alloc::string::ToString::to_string(&fd) (or just import the trait) to avoid runtime format string parsing and potential over-allocations when converting single integers to strings in procfs.rs.

2024-06-07 - Avoid vec![0; N] when the buffer is immediately overwritten

Learning: Initializing large buffers with vec![0; N] in #![no_std] allocates and zeroes out the memory. For buffers like block device reads (raw array in ext4) that are immediately overwritten by a read_block system call, this zeroing is unnecessary overhead. However, safe Rust requires read_block to write to a valid initialized buffer (or an &mut [u8]). Using vec![0; N] is often fine for small arrays, but can be a bottleneck for large ones. In Ext4 write/read block functions, there are allocations for vec![0u8; self.sector_size] which is 512 bytes or 4KB, where zero initialization happens every time. Given safe Rust limitations without MaybeUninit, keeping vec![0; ...] might be necessary, but we can potentially optimize buffer usage or caching. Another optimization is String::to_string instead of format!. Action: Replace format!("{}", fd) with fd.to_string() in procfs iteration as it is an easy and significant performance improvement within hot loop.