Custom String vs D String performance

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


On 05/09/2016 11:45 PM, Patric wrote:
> On Monday, 5 September 2016 at 11:20:08 UTC, rikki cattermole wrote:
>> 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
>
> I´m aware of 1, and 2.
>
> I look one million times on datetime and did´nt see the benchmark,
> thanks xD
>
> My intention is to not use gc at all, so no "new" for me.
> Yes the optimal case will be reserve the memory beforehand, but i´m
> benchmarking the opBinary.
>
> So now a bit better example :)
> https://dpaste.dzfl.pl/b9356f57a8c8
>
>
> Daniel Kozak:
> Ok, now it gets a bit weird.
>
> On DPaste it shows:
> 14 ms and 343 μs - DString
> 3 ms and 928 μs - CustomString
>
> And on my PC (with dub release mode) :
>
> 7 ms, 885 μs, and 9 hnsecs - DString
> 18 ms, 740 μs, and 8 hnsecs - CustomString
>
> (!!!)

Be a bit careful there, when a struct is moved around its destructor 
will be called. This is why I suggested you to use the GC to allocate 
memory or else ouch.

So you'll find very quickly that you will be having segfaults since you 
will be doing use after free.

Oh and you'll want to turn that into a slice at some point ``char[] str 
= ptr[0 .. length];``.


More information about the Digitalmars-d-learn mailing list