Skip to content

⚡ Bolt: optimize format! allocation in procfs - #18

Closed
muou000 wants to merge 1 commit into
mainfrom
bolt-perf-procfs-format-5448303405494560997
Closed

⚡ Bolt: optimize format! allocation in procfs#18
muou000 wants to merge 1 commit into
mainfrom
bolt-perf-procfs-format-5448303405494560997

Conversation

@muou000

@muou000 muou000 commented Jun 4, 2026

Copy link
Copy Markdown
Owner

💡 What: Replaced inefficient format! macro calls within arceos/modules/axfs/src/fs/procfs.rs with more optimal alternatives.

  • Used to_string() for integer formatting which natively relies on fast itoa operations instead of macro-generated structures.
  • Implemented String::with_capacity(128) and the write! 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 riscv64 and loongarch64 builds using make test.


PR created automatically by Jules for task 5448303405494560997 started by @muou000

Summary by CodeRabbit

  • Chores
    • Optimized internal string construction mechanisms for improved code efficiency and maintainability in system information processing.

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>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 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 @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Copilot AI review requested due to automatic review settings June 4, 2026 20:03
@coderabbitai

coderabbitai Bot commented Jun 4, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 2870fc9b-2797-452c-8440-2992ac1a9f7a

📥 Commits

Reviewing files that changed from the base of the PR and between 804887e and cd06413.

📒 Files selected for processing (1)
  • arceos/modules/axfs/src/fs/procfs.rs

📝 Walkthrough

Walkthrough

This PR simplifies string construction in the procfs module by eliminating the format macro import and replacing format!("{}", ...) calls with .to_string() for numeric conversions and refactoring render_meminfo() to use the Write trait for more efficient string building.

Changes

Procfs String Formatting Refactoring

Layer / File(s) Summary
Import cleanup and numeric to_string() conversions
arceos/modules/axfs/src/fs/procfs.rs
Removes the format import from the module prelude. Numeric IDs (pid, fd) throughout the file are converted to strings using .to_string() instead of format!("{}", ...) at six call sites: fd directory inode construction, self/init symlink paths, PidMax live file generation, and fd/pid entries in read_dir().
render_meminfo() Write trait refactoring
arceos/modules/axfs/src/fs/procfs.rs
render_meminfo() is refactored to use core::fmt::Write and the write! macro for building the output string into a preallocated buffer, replacing the previous format! approach.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 Strings once wrapped in format! calls so deep,
Now .to_string() keeps them light and neat,
The Write trait dances, memory's a treat,
With care refactored, the code's pristine and clean! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: optimizing format! allocations in procfs. While it includes an emoji/prefix, it accurately captures the core optimization objective.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch bolt-perf-procfs-format-5448303405494560997

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +697 to +699
let mut s = PID_MAX.load(core::sync::atomic::Ordering::Acquire).to_string();
s.push('\n');
s

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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));
            s

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) with to_string().
  • Reworked /proc/meminfo rendering to preallocate a String and populate it via write!.
  • Avoided format!("{}\n", ...) by appending \n onto an existing string.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@muou000 muou000 closed this Jun 9, 2026
@muou000
muou000 deleted the bolt-perf-procfs-format-5448303405494560997 branch June 9, 2026 11:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants