4x faster strlen with 4 char sentinel

Ola Fosheim Grøstad via Digitalmars-d-announce digitalmars-d-announce at puremagic.com
Mon Jun 27 14:15:09 PDT 2016


On Monday, 27 June 2016 at 06:31:49 UTC, Ola Fosheim Grøstad 
wrote:
> On Monday, 27 June 2016 at 05:27:12 UTC, chmike wrote:
>> Ending strings with a single null byte/char is to save space. 
>> It was critical in the 70´s when C was created and memory 
>> space was very limited. That's not the case anymore and I 
>> guess the
>
> Not only to save space, some CPUs also had cheap incrementing 
> load/stores and branching on zero is faster than sacrificing 
> another register for a counter.

I incidentally just found my 1992 implementation for Motorola 
68K, to illustrate:

_mystrcpy		
		move.l	4(sp),a1	; pointer for destination
		move.l	8(sp),a0	; pointer for source

mystrcpy	move.l	a0,d0
1$		move.b	(a0)+,(a1)+ ; copy
		bne.s	1$		   ; jump back up if not zero
		rts

As you can see it is a tight loop. Other CPUs are even tighter, 
and have single-instruction loops (even 8086?)

So not only storage, also performance on specific CPUs. Which is 
a good reason for keeping datatypes in standard libraries 
abstract, different CPUs favour different representations. Even 
on very basic datatypes.





More information about the Digitalmars-d-announce mailing list