Small Buffer Optimization for string and friends

Jakob Ovrum jakobovrum at gmail.com
Mon Apr 9 08:04:23 PDT 2012


On Monday, 9 April 2012 at 14:55:16 UTC, Andrei Alexandrescu 
wrote:
> 3. There are patterns that attempt to optimize by e.g. using 
> .ptr, but end up pessimizing code because they trigger multiple 
> memory allocations.
>
>
> Andrei

It's important to note that this pattern is probably most common 
in glue code to C libraries, not bounds-checking related 
optimizations. There are countless of C library functions which 
receive the equivalent of an array by taking a pointer and a 
length, and implicit allocation on `foo.ptr` is completely 
unacceptable in these cases.

It's also common to avoid the `toStringz` function for strings 
you know are zero-terminated, using `.ptr` directly instead, as 
the toStringz function unconditionally appends a zero these days 
(and for good reasons, its previous optimization was extremely 
optimistic about its input).


More information about the Digitalmars-d mailing list