This patch optimizes basic_string::compare to use strcmp when the default char_traits has been given.
See PR19900 for more information. https://llvm.org/bugs/show_bug.cgi?id=19900
Details
Diff Detail
Event Timeline
Won't this do the wrong thing for embedded '\0' in a std::string?
std::string("hello\0world", 11).compare("hello")
should not return 0.
Woops. Yep that seems correct but it will sure hamper the possibility for optimization.
Instead of optimizing away the length calculation, this patch still performs it but uses the calculated length to return early if the two strings differ in length.
include/string | ||
---|---|---|
3802 | I actually don't want to invoke compare because it has different semantics and does unneeded work. compare first does a comparison using traits::compare and then does a comparison using length. We have already compared based on length and we don't need to do any of the extra work that compare(...) does. I think calling __rhs.compare(0, npos, __lhs, __lhs_len) would be a pessimization. |
The first change LGTM. The second one needs to match it.
include/string | ||
---|---|---|
3814 | You'll want to do the same as above here. |
If the lengths are the same (and they are), you don't need the min of them