perf(agent): avoid preview allocations for non-truncated strings (fix #894) - #924
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a performance improvement by optimizing the Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a performance optimization by using Cow<'_ , str> in truncate_for_preview to avoid unnecessary string allocations. The changes are well-implemented and no security vulnerabilities were identified during the audit. However, an issue was found in src/worker/container.rs where a ToolError is passed to truncate_for_preview instead of a &str, which should be addressed for correctness.
446afbf to
d5a8947
Compare
zmanian
left a comment
There was a problem hiding this comment.
LGTM. Clean optimization that avoids allocating for non-truncated strings.
- Return type changes from
StringtoCow<'_, str>-- borrows the input when no truncation is needed, only allocates when the ellipsis suffix is appended. Cow<str>implementsSerialize,Display,Deref<Target=str>, andPartialEq<str>, so all existing call sites (includingserde_json::json!()macros,format!(), and string comparisons) continue to work.- Note: this changes the
truncate_for_previewinagentic_loop.rsonly (used bycontainer.rsandjob.rs). The separatetruncate_for_previewinagent_loop.rsis unchanged. Thejob.rscall sites were not modified in this diff but compile fine sinceCow<str>serializes identically toStringin JSON contexts. - The
container.rsfix correctly adds.into()on theformat!()result to produceCow::Owned. - Good test additions verifying
Cow::BorrowedvsCow::Ownedvariants.
Approved.
Head branch was pushed to by a user without write access
Head branch was modified
d5a8947 to
0a2bc38
Compare
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
- Remove redundant `.to_string()` on `&String` in container.rs error arm - Bind `format!()` result to a let in job.rs to avoid Cow borrowing from temporary - Merge borrowed/owned Cow assertions into existing tests, drop misleading comments Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Keep dedicated `test_truncate_short_string_borrows` and `test_truncate_long_string_owns` tests so the PR diff contains new `#[test]` functions, satisfying the regression test enforcement check. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…earai#894) (nearai#924) * perf(agent): avoid preview allocation on non-truncated strings * Update src/worker/container.rs Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * chore(ci): annotate test assertions for no-panics gate * fix: remove unnecessary allocation and consolidate tests - Remove redundant `.to_string()` on `&String` in container.rs error arm - Bind `format!()` result to a let in job.rs to avoid Cow borrowing from temporary - Merge borrowed/owned Cow assertions into existing tests, drop misleading comments Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: restore separate test functions for CI regression check Keep dedicated `test_truncate_short_string_borrows` and `test_truncate_long_string_owns` tests so the PR diff contains new `#[test]` functions, satisfying the regression test enforcement check. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: ilblackdragon@gmail.com <ilblackdragon@gmail.com> Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…earai#894) (nearai#924) * perf(agent): avoid preview allocation on non-truncated strings * Update src/worker/container.rs Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * chore(ci): annotate test assertions for no-panics gate * fix: remove unnecessary allocation and consolidate tests - Remove redundant `.to_string()` on `&String` in container.rs error arm - Bind `format!()` result to a let in job.rs to avoid Cow borrowing from temporary - Merge borrowed/owned Cow assertions into existing tests, drop misleading comments Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: restore separate test functions for CI regression check Keep dedicated `test_truncate_short_string_borrows` and `test_truncate_long_string_owns` tests so the PR diff contains new `#[test]` functions, satisfying the regression test enforcement check. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: ilblackdragon@gmail.com <ilblackdragon@gmail.com> Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Summary
Fixes #894 by eliminating unnecessary heap allocation in
truncate_for_previewwhen the input is already within the byte budget.What changed
src/agent/agentic_loop.rsStringtoCow<'_, str>.Cow::Borrowed(s)for non-truncated strings.Cow::Owned(...)only when truncation occurs.src/worker/container.rs.into()on onematcharm so both arms unify asCow<'_, str>.Issue
Before / After (explicit)
Before (
staging):After (this PR):
Behavioral verification added:
test_truncate_short_string_borrowed-> verifies non-truncated result is borrowedtest_truncate_long_string_owned-> verifies truncated result is ownedValidation
Targeted tests run locally:
All passed.
Scope
Localized performance fix; no user-facing semantic change in preview content.