Skip to content

Keep directly moved strings null-terminated - #616

Merged
nunoplopes merged 2 commits into
Cpp2Rust:masterfrom
perfloop-oss:perfloop-pr-open-r8s4ffc92b
Nov 15, 2026
Merged

nunoplopes merged 2 commits into
Cpp2Rust:masterfrom
perfloop-oss:perfloop-pr-open-r8s4ffc92b

Conversation

@perfloop-agent

@perfloop-agent perfloop-agent commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

String take expansions in both models now leave vec![0], reusing the default initializer from #478. Previously, std::mem::take left no terminator: size() wrapped and the unsafe substring length could underflow. C++ leaves a moved-from string valid with an unspecified value; choosing empty matches libstdc++ and libc++.

Implicit returns of locals and by-value string parameters keep take, avoiding a replacement allocation; destructors inspecting that returned local or parameter still see the old empty vector. Returns that move from reference parameters still reset the source. Explicit return std::move(x) still resets x and pays the allocation, because a local destructor can observe x after an explicit move.

Cost: each reset allocates a one-byte buffer. Generated memberwise move constructors for user records also pay this per moved string field, including when returning a record. The cost is not confined to move-only loops.

Elapsed-time medians:

Translated workload Before After Paired runs
2M string rotations, refcount 40.204 ms 87.55 ms 13
Same rotations, unsafe 5.1 ms 15.971 ms 13
1M string pushes, refcount 54.903 ms 64.818 ms 13
2M one-string record returns, refcount 51.3245 ms 66.199 ms 14

The unsafe record-return loop measured 7.0425 ms → 8.914 ms over 14 paired runs. These costs buy valid moved-from values on the repaired paths. Reusing destination buffers on assignment is not attempted.

Remaining: moves made by the runtime's generic move remain unfixed: std::pair<std::string,T> (including moved-pair push_back), std::array<std::string,N>, and std::map::operator[] with a moved key.

tests/unit/string_moved_from.cpp checks size, substring, terminators, moved-to payloads and return paths against native C++.

Testing and integration

lit ran 6 tests, all passed, covering tests/unit/string_moved_from.cpp, tests/unit/vector_deque_vector_string.cpp, tests/unit/string.cpp, tests/unit/string_substr.cpp, tests/unit/return_move.cpp and tests/unit/move_ctor.cpp; each test checks both models (Linux, Release, LLVM 23).

The new fixture's expected Rust distinguishes implicit local/by-value returns from moves through lvalue and rvalue reference parameters. Existing substring tests cover fresh strings and operand evaluation. The existing vector-of-strings expectations change from take to replace in both models.

clang-format reports no changes on the changed C++ files. macOS validation is left to target CI.

Combining this with #583 (argument-capture changes) requires regenerating their string-move expectations.

Case and evidence


Made by Perfloop. Human sponsor: Tomás Senart.

@nunoplopes

Copy link
Copy Markdown
Contributor

A value that is moved is left in an unspecified state according to the C++ standard. Please confirm, but if that's the case we don't need to recreate a [0] vector.

@perfloop-agent

Copy link
Copy Markdown
Contributor Author

Yes: the standard requires a valid but unspecified state, not an empty string or a new allocation. However, the current string rules assume a trailing zero: removing the reset makes size() underflow and leaves c_str() without a readable terminator, so avoiding allocation requires changing those rules rather than just dropping vec![0]. I suggest keeping the smaller invariant-restoring reset with its disclosed allocation cost rather than broadening the string representation in this PR; how does that sound?

@nunoplopes

Copy link
Copy Markdown
Contributor

Yes: the standard requires a valid but unspecified state, not an empty string or a new allocation. However, the current string rules assume a trailing zero: removing the reset makes size() underflow and leaves c_str() without a readable terminator, so avoiding allocation requires changing those rules rather than just dropping vec![0]. I suggest keeping the smaller invariant-restoring reset with its disclosed allocation cost rather than broadening the string representation in this PR; how does that sound?

What's the behavior of libstc++ and libc++?

@perfloop-agent

Copy link
Copy Markdown
Contributor Author

Both libstdc++ 14 and libc++ 19 leave the source empty and null-terminated after non-self std::string move construction and assignment with the default allocator, with the destination holding the source's original contents. They avoid a replacement allocation using inline storage (or reusing the destination's old buffer in libstdc++ assignment), whereas our Vec<c_char> needs storage for its terminator. Empty therefore matches both implementations, but allocating is a representation cost, not a C++ requirement; I still suggest keeping the scoped reset with its disclosed cost here rather than redesigning strings in this PR—how does that sound?

@nunoplopes

Copy link
Copy Markdown
Contributor

Both libstdc++ 14 and libc++ 19 leave the source empty and null-terminated after non-self std::string move construction and assignment with the default allocator, with the destination holding the source's original contents. They avoid a replacement allocation using inline storage (or reusing the destination's old buffer in libstdc++ assignment), whereas our Vec<c_char> needs storage for its terminator. Empty therefore matches both implementations, but allocating is a representation cost, not a C++ requirement; I still suggest keeping the scoped reset with its disclosed cost here rather than redesigning strings in this PR—how does that sound?

ok!

Signed-off-by: Tomás Senart <ts@perfloop.ai>
Signed-off-by: Tomás Senart <ts@perfloop.ai>
@perfloop
perfloop Bot force-pushed the perfloop-pr-open-r8s4ffc92b branch from 174df42 to 3b01548 Compare November 15, 2026 00:26
@nunoplopes
nunoplopes merged commit 69d7991 into Cpp2Rust:master Nov 15, 2026
9 checks passed
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