Skip to content

fix: eliminate unnecessary string copies in EmitDeref and GetOrMaterialize#75

Closed
Copilot wants to merge 1 commit into
masterfrom
copilot/fix-unnecessary-string-copies
Closed

fix: eliminate unnecessary string copies in EmitDeref and GetOrMaterialize#75
Copilot wants to merge 1 commit into
masterfrom
copilot/fix-unnecessary-string-copies

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 11, 2026

Found and fixed two cases where unnecessary string copies were created in the C++ converter, identified via performance-unnecessary-value-param clang-tidy checks.

Changes

Converter::EmitDerefstd::stringstd::string_view

inner was passed by value, but StrCat only reads it via const T& (its variadic template parameter). No ownership was needed, so every call site was paying for a heap allocation/copy that was immediately discarded. Changed to std::string_view and updated the three call sites (two of which had redundant std::move calls removed).

TempMaterializationCtx::GetOrMaterializestd::function by value → const &

The callable parameter materialize_fn was taken by value, causing the lambda (and the std::function wrapping it) to be copied on every call. Since the function only calls it once and never stores it, passing by const reference avoids the copy entirely.

…alize

- Change EmitDeref's `inner` parameter from `std::string` to `std::string_view`
  to avoid copying a string that is only ever read via StrCat.
  Update the three call sites accordingly (two std::move calls become plain refs).
- Change GetOrMaterialize's `materialize_fn` parameter from by-value to
  `const std::function<...>&` to avoid copying the callable object on each call.

Agent-Logs-Url: https://github.com/Cpp2Rust/cpp2rust/sessions/a4a77a6f-20a8-49a8-98d1-507851373647

Co-authored-by: nunoplopes <2998477+nunoplopes@users.noreply.github.com>
Copilot AI requested a review from nunoplopes May 11, 2026 17:02
@nunoplopes nunoplopes closed this May 11, 2026
@nunoplopes nunoplopes deleted the copilot/fix-unnecessary-string-copies branch May 11, 2026 17:57
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