⚡ Bolt: optimize format! allocation in procfs - #18
Conversation
Replaced `format!` macros inside `procfs` with pre-allocated `String::with_capacity` and `write!` macros for complex formatted strings, and `to_string()` for simple integers. This prevents the default dynamic allocation overhead incurred by `format!` inside hot paths, specifically when parsing PIDs and FDs. Co-authored-by: muou000 <77525792+muou000@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughThis PR simplifies string construction in the procfs module by eliminating the ChangesProcfs String Formatting Refactoring
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Code Review
This pull request optimizes string formatting in procfs.rs by replacing format! macros with .to_string() and pre-allocated strings using write!. A review comment points out that calling to_string() followed by pushing a newline character on PID_MAX triggers an unnecessary reallocation, and suggests using write! with a pre-allocated string of capacity 12 to avoid this.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| let mut s = PID_MAX.load(core::sync::atomic::Ordering::Acquire).to_string(); | ||
| s.push('\n'); | ||
| s |
There was a problem hiding this comment.
Calling to_string() on an integer typically allocates a String with a capacity exactly matching the length of the formatted number. Consequently, calling s.push('\n') immediately after will trigger a reallocation and copy the string data, which defeats the optimization goal of this PR.
Using write! with a pre-allocated String of capacity 12 (sufficient for any u32 plus a newline) avoids this reallocation entirely.
use core::fmt::Write;
let mut s = String::with_capacity(12);
let _ = write!(&mut s, "{}\n", PID_MAX.load(core::sync::atomic::Ordering::Acquire));
sThere was a problem hiding this comment.
Pull request overview
Optimizes string construction in the procfs implementation to reduce transient heap allocations when serving common /proc reads, replacing several format! usages with more allocation-aware alternatives.
Changes:
- Replaced integer
format!calls (e.g., for PIDs/FDs) withto_string(). - Reworked
/proc/meminforendering to preallocate aStringand populate it viawrite!. - Avoided
format!("{}\n", ...)by appending\nonto an existing string.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
💡 What: Replaced inefficient
format!macro calls withinarceos/modules/axfs/src/fs/procfs.rswith more optimal alternatives.to_string()for integer formatting which natively relies on fastitoaoperations instead of macro-generated structures.String::with_capacity(128)and thewrite!macro for complex layout strings (e.g.MemInfo).🎯 Why: To improve performance and minimize unnecessary memory heap allocations when serving standard Linux process/system file accesses.
format!dynamically allocates on the heap without knowing its maximum capacity beforehand, whereas statically sizing strings minimizes reallocations.📊 Impact: Reduces transient dynamic memory allocation calls when fetching process file descriptors, meminfo, and enumerating processes. Expected to save CPU cycles and reduce fragmentation on system operations reading
/proc.🔬 Measurement: Verify tests run cleanly on
riscv64andloongarch64builds usingmake test.PR created automatically by Jules for task 5448303405494560997 started by @muou000
Summary by CodeRabbit