Skip to content

Avoid unnecessary heap allocations for short-lived strings#41

Merged
nunoplopes merged 4 commits intomasterfrom
copilot/optimize-duplication-of-objects
Apr 29, 2026
Merged

Avoid unnecessary heap allocations for short-lived strings#41
nunoplopes merged 4 commits intomasterfrom
copilot/optimize-duplication-of-objects

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 29, 2026

Several hot paths were heap-allocating temporary std::string objects from StringRef/SmallVector sources only to immediately format or compare them.

Changes

  • converter_refcount.cppgetOpcodeStr().str()std::string_view for binary opcode in ConvertGenericBinaryOperator
  • mapper.cppbuild_rust_type — replace std::string(1, sign) + std::to_string(bits) (two allocations + concat) with std::format("{}{}", sign, bits)
  • mapper.cppToString unary opcode — replace std::string opcode = getOpcodeStr().str() + string concatenation with std::string_view + std::format
  • converter.cpp — enum emitter — eliminate intermediate std::string init_str(init.begin(), init.end()) by passing std::string_view views directly to std::format
  • converter.cppGetOverloadedFunctionNamestd::string name(decl->getNameAsString())auto name (removes redundant copy-construction)
  • converter.cpp — enum constant formattinggetName().str()std::string_view(getName())

Example before/after for the enum value emitter:

// Before
std::string init_str(init.begin(), init.end());
StrCat(std::format("{} = {},", e->getNameAsString(), init_str));

// After
StrCat(std::format("{} = {},", std::string_view(e->getName()),
                   std::string_view(init.data(), init.size())));

Copilot AI and others added 4 commits April 28, 2026 20:37
…tring copies

Agent-Logs-Url: https://github.com/Cpp2Rust/cpp2rust/sessions/67031e6a-ec2f-4f6c-a534-ac63f300898d

Co-authored-by: nunoplopes <2998477+nunoplopes@users.noreply.github.com>
Co-authored-by: nunoplopes <2998477+nunoplopes@users.noreply.github.com>
@nunoplopes nunoplopes marked this pull request as ready for review April 29, 2026 07:37
@nunoplopes nunoplopes merged commit 5bdf6a1 into master Apr 29, 2026
9 checks passed
@nunoplopes nunoplopes deleted the copilot/optimize-duplication-of-objects branch April 29, 2026 08:39
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