Skip to content

Conversation

@paperbenni
Copy link
Member

@paperbenni paperbenni commented Feb 2, 2026

⚡ Bolt: Optimized drawstatusbar to use stack allocation.

💡 What: Replaced unconditional malloc with a stack buffer optimization (SSO) in drawstatusbar.
🎯 Why: drawstatusbar is called frequently (every second or more). Allocating heap memory for short strings (typically < 1024 bytes) is inefficient and causes unnecessary overhead/fragmentation.
📊 Impact: Eliminates heap allocation for >99% of status bar updates.
🔬 Measurement: Code analysis confirms removal of malloc in common path. Build verification passed.


PR created automatically by Jules for task 6717172985347198188 started by @paperbenni

Summary by Sourcery

Optimize status bar rendering by avoiding heap allocations in the hot path of drawstatusbar through use of a stack buffer for typical status text sizes.

Enhancements:

  • Use a fixed-size stack buffer in drawstatusbar and fall back to heap allocation only for unusually long status text.
  • Document the rationale and impact of optimizing drawstatusbar heap usage in a new .jules/bolt note.

Replaced heap allocation (malloc/free) with stack allocation for status text processing in `drawstatusbar`. This function is a hot path called on every status update (e.g., clock tick).

- Introduced a 1024-byte stack buffer (matching `stext` global size).
- Fallback to malloc only if text exceeds buffer size.
- Reduces allocator churn and improves efficiency.

Verification:
- `make clean && make` passes.
- Verified manual logic for buffer usage and cleanup.

Co-authored-by: paperbenni <15818888+paperbenni@users.noreply.github.com>
@google-labs-jules
Copy link
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.

@coderabbitai
Copy link

coderabbitai bot commented Feb 2, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

  • 🔍 Trigger a full review
✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch bolt-perf-drawstatusbar-stack-6717172985347198188

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.

@sourcery-ai
Copy link

sourcery-ai bot commented Feb 2, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Optimizes drawstatusbar to avoid heap allocations in the common case by introducing a stack-allocated buffer and only falling back to malloc for larger status strings, plus adds a Jules Bolt note documenting the optimization rationale.

File-Level Changes

Change Details Files
Optimize drawstatusbar memory allocation with a stack buffer and conditional heap fallback.
  • Introduce a fixed-size (1024-byte) stack buffer inside drawstatusbar for copying the status text.
  • Replace unconditional malloc with a conditional path: use the stack buffer when the required length is smaller than the buffer, otherwise allocate on the heap.
  • Adjust pointer initialization so both stack and heap paths share the same pointer variables for subsequent logic.
  • Ensure heap memory is freed only when heap allocation actually occurred by comparing the pointer to the stack buffer before calling free().
bar.c
Document the heap allocation optimization in the Jules Bolt metadata.
  • Add a bolt note describing the previous heap allocation behavior in drawstatusbar and the motivation for the stack buffer optimization.
  • Record that typical status text sizes are small and that the change targets allocator overhead in a hot rendering path.
.jules/bolt.md

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've found 1 issue, and left some high level feedback:

  • To maximize the benefit of the stack buffer, consider using len <= sizeof(buf) instead of len < sizeof(buf) so inputs that exactly fill the buffer also avoid heap allocation.
  • Since text and p always alias after text = p;, you can simplify the code by using a single pointer (e.g., reusing text for both stack and heap cases) to reduce cognitive overhead and potential confusion about which pointer should be freed.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- To maximize the benefit of the stack buffer, consider using `len <= sizeof(buf)` instead of `len < sizeof(buf)` so inputs that exactly fill the buffer also avoid heap allocation.
- Since `text` and `p` always alias after `text = p;`, you can simplify the code by using a single pointer (e.g., reusing `text` for both stack and heap cases) to reduce cognitive overhead and potential confusion about which pointer should be freed.

## Individual Comments

### Comment 1
<location> `.jules/bolt.md:3` </location>
<code_context>
+## 2026-02-02 - Heap Allocation in Hot Path (drawstatusbar)
+**Learning:** The `drawstatusbar` function allocates memory via `malloc` on every call to process the status text. Since the status text is typically small (capped at 1024 bytes by `stext`) and updated frequently (e.g., every second), this creates unnecessary heap churn.
+**Action:** Use a stack buffer (Small String Optimization) for typical sizes and fallback to heap only when necessary. This reduces allocator overhead in the rendering loop.
</code_context>

<issue_to_address>
**suggestion (typo):** Use "fall back" (verb) instead of "fallback" in this sentence.

Here it’s used as a verb phrase (“to fall back to heap”), so it should read: “fall back to heap only when necessary.”
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@@ -0,0 +1,3 @@
## 2026-02-02 - Heap Allocation in Hot Path (drawstatusbar)
**Learning:** The `drawstatusbar` function allocates memory via `malloc` on every call to process the status text. Since the status text is typically small (capped at 1024 bytes by `stext`) and updated frequently (e.g., every second), this creates unnecessary heap churn.
**Action:** Use a stack buffer (Small String Optimization) for typical sizes and fallback to heap only when necessary. This reduces allocator overhead in the rendering loop.
Copy link

Choose a reason for hiding this comment

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

suggestion (typo): Use "fall back" (verb) instead of "fallback" in this sentence.

Here it’s used as a verb phrase (“to fall back to heap”), so it should read: “fall back to heap only when necessary.”

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.

1 participant