Fix string move constructor and assignment operator#10353
Conversation
|
The idea itself LGTM. Another idea: Change those I mean, just look how many purely hardcoded |
|
@yhabteab Can you please add some explanation to f15f99f why this is done (just like for the other commit)?
That's another idea in the sense that it would be an additional idea. If a |
79aade1 to
f15f99f
Compare
|
Oops, didn't want to push that here. That force push should just have restored the original state of the PR. |
|
I've written a test case that checks that the changes to the string operators have the desired effect: 79aade1 Feel free to have a look and include it in the PR (bonus points for something similar for the other change). |
The Icinga DB code performs intensive operations on certain STL containers, primarily on `std::vector<String>`. Specifically, it inserts 2-3 new elements at the beginning of a vector containing thousands of elements. Without this commit, all the existing elements would be unnecessarily copied just to accommodate the new elements at the front. By making this change, the compiler is able to optimize STL operations like `push_back`, `emplace_back`, and `insert`, enabling it to prefer the move constructor over copy operations, provided it is guaranteed that no exceptions will be thrown.
f15f99f to
f1b79f4
Compare
f1b79f4 to
8735e6f
Compare
8735e6f to
01a8bb9
Compare
The move `String(Value&&)` constructor tries to partially move `String` values from a `Value` type. However, since there was no an appropriate `Value::Get<T>()` implementation that binds to the requested move operation, the compiler will actually not move the value but copy it instead as the only available implementation of `Value::Get<T>()` returns a const reference `const T&`. This commit adds a new overload that returns a non-const reference and allows to optionally move the string value of a Value type.
01a8bb9 to
3e9292a
Compare
Hi @gbin2265, thanks for reporting! Yes, that is caused by this PR! I have created a new PR that fixes that. |

Warning
When backporting, also consider the following PR that fixes a problem in this PR:
The Icinga DB code performs heavyweight operations on certain STL containers,
primarily on
std::vector<String>. Specifically, it inserts 2-3 new elementsat the beginning of a vector containing thousands of elements. Without this commit,
all the existing elements would be unnecessarily copied just to accommodate the new
elements at the front. By making this change, the compiler is able to optimize STL
operations like
push_back,emplace_back, andinsert, enabling it to prefer themove constructor over copy operations, provided it is guaranteed that no exceptions
will be thrown.