Skip to content

perf: eliminate spurious string copies in cpp2rust converter#35

Merged
nunoplopes merged 4 commits intomasterfrom
copilot/optimize-spurious-string-copies
Apr 28, 2026
Merged

perf: eliminate spurious string copies in cpp2rust converter#35
nunoplopes merged 4 commits intomasterfrom
copilot/optimize-spurious-string-copies

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 28, 2026

Several hot-path functions in the converter were performing redundant or avoidable std::string allocations during type-to-string conversion and struct/record processing.

Changes

  • mapper.cppToString(): Added std::move() when passing local strings to normalizeTranslationRule() (by-value param). Since raw_string_ostream is unbuffered, the string is fully populated before the call; move avoids a copy on every type serialization.

  • converter.cpprecordDerivesCopy(): Mapper::Map(f->getType()) was called twice per field for two separate starts_with() checks. Result is now cached in a local.

  • converter.cppGetDefaultAsString(): Mapper::ToString(qual_type) was called twice in successive else if branches. Result is now cached before the checks.

  • converter.cppEmitRustStruct() / VisitCXXRecordDecl(): Replaced multi-step temporaries:

    // Before: constructs 2-3 temporary std::string objects
    std::string(keyword::kImpl) + ' ' + struct_name
    
    // After: single allocation
    std::format("{} {}", keyword::kImpl, struct_name)
  • converter_lib.cppGetFileName(): Eliminated an intermediate file_name_as_string copy by constructing std::filesystem::path directly from StringRef iterators and using file_path.string() as the fallback return value.

Copilot AI and others added 2 commits April 28, 2026 15:21
- mapper.cpp: use std::move() when passing local strings to
  normalizeTranslationRule() to avoid unnecessary copies in ToString()
- converter.cpp recordDerivesCopy: cache Mapper::Map(f->getType())
  result to avoid calling it twice per field
- converter.cpp GetDefaultAsString: cache Mapper::ToString(qual_type)
  result to avoid calling it twice in successive else-if branches
- converter.cpp EmitRustStruct/VisitCXXRecordDecl: replace
  std::string(keyword::kImpl) + ' ' + name with std::format to avoid
  constructing multiple temporary strings
- converter_lib.cpp GetFileName: construct std::filesystem::path
  directly from StringRef iterators, eliminating the intermediate
  file_name_as_string copy and reusing file_path.string() as fallback

Agent-Logs-Url: https://github.com/Cpp2Rust/cpp2rust/sessions/b538d15a-35a7-4c43-93f3-6621eabaf105

Co-authored-by: nunoplopes <2998477+nunoplopes@users.noreply.github.com>
@nunoplopes nunoplopes force-pushed the copilot/optimize-spurious-string-copies branch from b9dda07 to 4c1beff Compare April 28, 2026 14:21
@nunoplopes nunoplopes marked this pull request as ready for review April 28, 2026 14:42
@nunoplopes nunoplopes merged commit 9a3bdee into master Apr 28, 2026
13 of 16 checks passed
@nunoplopes nunoplopes deleted the copilot/optimize-spurious-string-copies branch April 28, 2026 15:30
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