www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Custom String vs D String performance

reply Patric <patric.dexheimer gmail.com> writes:
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
Sep 05 2016
next sibling parent Daniel Kozak via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
Dne 5.9.2016 v 13:11 Patric via Digitalmars-d-learn napsal(a):

 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
One of reason could be GC, because I belive in D string it reuse memory, but in your case you always do malloc
Sep 05 2016
prev sibling next sibling parent Daniel Kozak via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
Dne 5.9.2016 v 13:17 Daniel Kozak napsal(a):

 Dne 5.9.2016 v 13:11 Patric via Digitalmars-d-learn napsal(a):

 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
One of reason could be GC, because I belive in D string it reuse memory, but in your case you always do malloc
BTW on my pc your Custom String is faster than D string
Sep 05 2016
prev sibling next sibling parent reply rikki cattermole <rikki cattermole.co.nz> writes:
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
Sep 05 2016
next sibling parent Daniel Kozak via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
Dne 5.9.2016 v 13:20 rikki cattermole via Digitalmars-d-learn napsal(a):
 ...
 Don't forget to disable the GC as well so it doesn't try to collect 
 during the tests.
This is not true, in many cases it will hurt performance a lot
Sep 05 2016
prev sibling parent reply Patric <patric.dexheimer gmail.com> writes:
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 (!!!)
Sep 05 2016
next sibling parent reply Daniel Kozak via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
Dne 5.9.2016 v 13:45 Patric via Digitalmars-d-learn napsal(a):
 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 (!!!)
Do you have windows or linux? Which version of dmd you have?
Sep 05 2016
parent reply Patric <patric.dexheimer gmail.com> writes:
On Monday, 5 September 2016 at 11:53:12 UTC, Daniel Kozak wrote:
 Do you have windows or linux? Which version of dmd you have?
Now on my job pc: DMD32 D Compiler v2.071.0-b2 In home i have 64 last version, and with my first version it shows the DString faster than my custom.
Sep 05 2016
parent Patric <patric.dexheimer gmail.com> writes:
On Monday, 5 September 2016 at 12:03:49 UTC, Patric wrote:
 On Monday, 5 September 2016 at 11:53:12 UTC, Daniel Kozak wrote:
 Do you have windows or linux? Which version of dmd you have?
Now on my job pc: DMD32 D Compiler v2.071.0-b2 In home i have 64 last version, and with my first version it shows the DString faster than my custom.
Well, if my opBinary implementation is right (in the performance sense also), so is a matter of Platform not of implementation, and i´m ok with that :) But I find strange this kind of discrepancy.
Sep 05 2016
prev sibling parent reply rikki cattermole <rikki cattermole.co.nz> writes:
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];``.
Sep 05 2016
parent Patric <patric.dexheimer gmail.com> writes:
On Monday, 5 September 2016 at 11:55:09 UTC, rikki cattermole 
wrote:
 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.
Iep, the full code have the postblit operator to correct this :)
Sep 05 2016
prev sibling parent Guillaume Piolat <first.last gmail.com> writes:
On Monday, 5 September 2016 at 11:11:23 UTC, 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
string s; string a = "testing"; string b = "another"; foreach(_ ; 0..max){ s = a ~ b; } Potentially this makes no allocation at all. "testing" and "another" will be in readonly memory, "a ~ b" will be constant-folded, and s will get assigned the same slice every-time. Now replace it with std::string in C++ and count the number of malloc ;)
Sep 05 2016