On Monday, 30 May 2016 at 09:28:29 UTC, ag0aep6g wrote:
On 05/29/2016 10:40 PM, qznc wrote:
bool string_cmp_opt(immutable(ubyte)[] x, immutable(ubyte)[]
y) {
Having "string" in the function name may be a bit misleading.
This doesn't have any special functionality for
text/characters/Unicode, does it?
Should have const parameters, not immutable.
pragma(inline, false);
I think you have to put this pragma on the function signature,
not in the body. Also, why prevent inlining of the function?
if (x.length != y.length) return false;
int i=0;
int isn't large enough for array lengths.
// word-wise compare is faster than byte-wise
if (x.length > size_t.sizeof)
for (; i < x.length - size_t.sizeof;
i+=size_t.sizeof) {
size_t* xw = cast(size_t*) &x[i];
size_t* yw = cast(size_t*) &x[i];
Typo: Should be `&y[i]` here.
if (*xw != *yw) return false;
}
// last sub-word part
for (; i < x.length; i+=1) {
if (x[i] != y[i]) // byte compare
return false;
}
return true;
}
Any comments or recommendations?
Did you benchmark this against the built-in `==`, with ldc or
gdc?
If this is correct and faster than the built-in `==`, why isn't
it the built-in `==`?
I too expected it to compile to a memcmp call, but according to
asm.dlang.org DMD with -O and -release, DMD compiles a == b to a
byte-wise compare.
I suppose for very tiny strings this is the fastest, but for
slightly larger strings, calling memcmp() would be faster. I
think inlining a string comparison is also not great for code
size. In the general case, for element types with trivial
equality, a call to memcmp() will always be preferable, right?