Repository navigation
Keep directly moved strings null-terminated - #616
Conversation
|
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. |
|
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 |
What's the behavior of libstc++ and libc++? |
|
Both libstdc++ 14 and libc++ 19 leave the source empty and null-terminated after non-self |
ok! |
Signed-off-by: Tomás Senart <ts@perfloop.ai>
Signed-off-by: Tomás Senart <ts@perfloop.ai>
174df42 to
3b01548
Compare
String take expansions in both models now leave
vec![0], reusing the default initializer from #478. Previously,std::mem::takeleft 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. Explicitreturn 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:
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-pairpush_back),std::array<std::string,N>, andstd::map::operator[]with a moved key.tests/unit/string_moved_from.cppchecks 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.cppandtests/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
taketoreplacein 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.