perf: eliminate spurious string copies in cpp2rust converter#35
Merged
nunoplopes merged 4 commits intomasterfrom Apr 28, 2026
Merged
perf: eliminate spurious string copies in cpp2rust converter#35nunoplopes merged 4 commits intomasterfrom
nunoplopes merged 4 commits intomasterfrom
Conversation
Copilot created this pull request from a session on behalf of
nunoplopes
April 28, 2026 14:15
View session
- 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>
b9dda07 to
4c1beff
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Several hot-path functions in the converter were performing redundant or avoidable
std::stringallocations during type-to-string conversion and struct/record processing.Changes
mapper.cpp—ToString(): Addedstd::move()when passing local strings tonormalizeTranslationRule()(by-value param). Sinceraw_string_ostreamis unbuffered, the string is fully populated before the call; move avoids a copy on every type serialization.converter.cpp—recordDerivesCopy():Mapper::Map(f->getType())was called twice per field for two separatestarts_with()checks. Result is now cached in a local.converter.cpp—GetDefaultAsString():Mapper::ToString(qual_type)was called twice in successiveelse ifbranches. Result is now cached before the checks.converter.cpp—EmitRustStruct()/VisitCXXRecordDecl(): Replaced multi-step temporaries:converter_lib.cpp—GetFileName(): Eliminated an intermediatefile_name_as_stringcopy by constructingstd::filesystem::pathdirectly fromStringRefiterators and usingfile_path.string()as the fallback return value.