String compare in words?

ag0aep6g via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon May 30 02:28:29 PDT 2016


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 `==`?


More information about the Digitalmars-d-learn mailing list