New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
std.algorithm.cmp with default ordering can use memcmp for all size 1 unsigned types (instead of just char) #6776
Conversation
Thanks for your pull request, @n8sh! Bugzilla references
Testing this PR locallyIf you don't have a local development environment setup, you can use Digger to test this PR: dub fetch digger
dub run digger -- build "master + phobos#6776" |
ae8fa67
to
281bc3a
Compare
return threeWay(r1.length, r2.length); | ||
}(); | ||
} | ||
if (*p1 != *p2) return cast(int) *p1 - cast(int) *p2; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you should cache *p1
and *p2
the additional dereference may be expensive
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@stefan-koch-sociomantic I've verified that the compiler performs this optimization without needing any hint from the programmer. Both cmp1
and cmp1
in the example below are compiled to the same machine code by ldc -O2
:
https://run.dlang.io/is/cz5hay
import std.algorithm : min;
int cmp1(const dchar[] r1, const dchar[] r2) @trusted
{
auto p1 = r1.ptr, p2 = r2.ptr, pEnd = p1 + min(r1.length, r2.length);
for (; p1 != pEnd; ++p1, ++p2)
{
if (*p1 != *p2)
return cast(int)*p1 - cast(int)*p2;
}
return int(r1.length > r2.length) - int(r1.length < r2.length);
}
int cmp2(const dchar[] r1, const dchar[] r2) @trusted
{
auto p1 = r1.ptr, p2 = r2.ptr, pEnd = p1 + min(r1.length, r2.length);
for (; p1 != pEnd; ++p1, ++p2)
{
const v1 = *p1, v2 = *p2; // manually saving result of *p1 and *p2
if (v1 != v2)
return cast(int) v1 - cast(int) v2;
}
return int(r1.length > r2.length) - int(r1.length < r2.length);
}
void main()
{
}
(Blocked on dscanner - so I stop relooking at this PR) |
I'm against this change. We should be removing C dependencies from Phobos, not finding more ways to plug them in. The language should be capable enough without them. |
|
@wilzbach As I said, "removing, not finding more ways" ;) |
Benchmark of this PR's change:
Note: in this benchmark ldc2 produced a 64-bit binary and dmd produced a 32-bit binary. For wider types whether this was an improvement depended on length, so I've restricted it to just byte-width types. @radcapricorn instead of using |
…cmp for all size 1 unsigned types ... instead of just char.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just address Stefan's comment and I think this is good.
No description provided.