Performance: Redundant heap allocations in truncation logic in core/src/truncate.rs

Resolved 💬 2 comments Opened Jan 22, 2026 by oopscompiled Closed Jan 23, 2026

What version of Codex is running?

v0.88.0

What subscription do you have?

Plus

Which model were you using?

-

What platform is your computer?

Darwin 24.6.0 arm64 arm

What terminal emulator and version are you using (if applicable)?

VSCode

What issue are you seeing?

The current string truncation logic in core/src/truncate.rs is inefficient. Mainly, the functions truncate_text, truncate_with_byte_estimate, and truncate_with_token_budget always return a String, which forces a heap allocation and a full copy of the content even when the text is within the budget and no truncation is required.

In high-throughput scenarios or when handling large tool outputs, this creates unnecessary memory pressure , thus slowing down the core execution loop.

What steps can reproduce the bug?

This is a performance-related bug. You can observe redundant memory allocation by calling the truncate_text function and setting a truncation policy with a budget greater than the length of the input text.

the following code snippet shows the issue more clearly (truncate.rs)

pub(crate) fn formatted_truncate_text(content: &str, policy: TruncationPolicy) -> String {
    if content.len() <= policy.byte_budget() {
        return content.to_string(); // <--- Redundant allocation/copy
    }
    // ...

}

What is the expected behavior?

The truncation logic should follow a "zero-copy" strategy to ensure the accuracy of the fast path. If the input text length is within the set budget, the system should return a reference to the original string (e.g., using Cow<'a, str>) instead of allocating a new String object.

Additional information

I have prepared a pull request to refactor these utilities to use Cow<'a, str>.
This change ensures that:

  1. No memory allocation occurs if truncation is not required.
  1. Owned variants of Cow are created only when an actual intermediate truncation operation (prefix + tag + suffix) is performed.
  1. Performance is improved without requiring changes to existing output formatting or logic.

View original on GitHub ↗

This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗