4x faster strlen with 4 char sentinel

Brad Roberts via Digitalmars-d-announce digitalmars-d-announce at puremagic.com
Mon Jun 27 10:33:50 PDT 2016


On 6/26/2016 11:47 AM, Jay Norwood via Digitalmars-d-announce wrote:
> On Sunday, 26 June 2016 at 16:59:54 UTC, David Nadlinger wrote:
>> Please keep general discussions like this off the announce list, which
>> would e.g. be suitable for announcing a fleshed out collection of
>> high-performance string handling routines.
>>
>> A couple of quick hints:
>>  - This is not a correct implementation of strlen, as it already
>> assumes that the array is terminated by four zero bytes. That
>> iterating memory with a stride of 4 instead of 1 will be faster is a
>> self-evident truth.
>>  - You should be benchmarking against a "proper" SIMD-optimised strlen
>> implementation.
>>
>>  — David
>
>
> This is more of just an observation that the choice of the single zero
> sentinel for C string termination comes at a cost of 4x strlen speed vs
> using four terminating zeros.
>
> I don't see a SIMD strlen implementation in the D libraries.
>
> The strlen2 function I posted works on any string that is terminated by
> four zeros, and returns the same len as strlen in that case, but much
> faster.
>
> How to get strings initialized with four terminating zeros at compile
> time is a separate issue.  I don't know the solution, else I might
> consider doing more with this.

Yup.. there's a reason that many many hours have been spent optimizing 
strlen and other memory related length and comparison routines.  They 
are used a lot and the number of ways of making them fast varies almost 
as much as the number of cpu's that exist.  This effort is embedded in 
the code gen of compilers (other than dmd) and libc runtimes.  Trying to 
re-invent it is noble, and very educational, but largely redundant.


More information about the Digitalmars-d-announce mailing list