Custom String vs D String performance

rikki cattermole via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Sep 5 04:20:08 PDT 2016


On 05/09/2016 11:11 PM, Patric wrote:
> I´m playing remaking D functionalities with nogc structs, and to at
> least match D performance.
> But in this particular case i´m unable to get near D performance.  Can
> someone point me out what i´m doing wrong, or if there is some magic
> behind the curtains on D strings?
>
> https://dpaste.dzfl.pl/1c981fdc71ac

Ok lots of bad assumptions in there so lets declare what they should be:

1. D supports three string types, string, wstring and dstring with the 
character types of char, wchar and dchar.
    Strings themselves have no special behavior in the compiler as they 
are arrays.
2. Types such as char are fixed in size, no point multiplying when its a 
constant 1.
3. A D string is length then pointer.

Ok, now on to implementation do not use StopWatch for benchmarking. Use 
benchmark[0]. This will execute the benchmark many times which removes 
one off errors.

Don't directly call malloc, it will never be free'd in this case.
``new char(length)`` would be better as it will automatically be handled 
by the GC.
You'll also want to reserve a block of memory to remove allocation from 
the cost as much as possible (after all you're not measuring that are 
you?). Don't forget to disable the GC as well so it doesn't try to 
collect during the tests.

[0] https://dlang.org/phobos/std_datetime.html#.benchmark
[1] http://dlang.org/spec/type.html
[2] http://dlang.org/spec/abi.html
[3] https://github.com/dlang/druntime/blob/master/src/object.d#L41


More information about the Digitalmars-d-learn mailing list