digitalmars.D.learn - Custom String vs D String performance
- Patric (6/6) Sep 05 2016 I´m playing remaking D functionalities with nogc structs, and to
- Daniel Kozak via Digitalmars-d-learn (3/9) Sep 05 2016 One of reason could be GC, because I belive in D string it reuse memory,...
- Daniel Kozak via Digitalmars-d-learn (2/12) Sep 05 2016 BTW on my pc your Custom String is faster than D string
- rikki cattermole (23/29) Sep 05 2016 Ok lots of bad assumptions in there so lets declare what they should be:
- Daniel Kozak via Digitalmars-d-learn (2/5) Sep 05 2016 This is not true, in many cases it will hurt performance a lot
- Patric (19/55) Sep 05 2016 I´m aware of 1, and 2.
- Daniel Kozak via Digitalmars-d-learn (2/55) Sep 05 2016 Do you have windows or linux? Which version of dmd you have?
- Patric (5/6) Sep 05 2016 Now on my job pc:
- Patric (5/11) Sep 05 2016 Well, if my opBinary implementation is right (in the performance
- rikki cattermole (8/61) Sep 05 2016 Be a bit careful there, when a struct is moved around its destructor
- Patric (3/8) Sep 05 2016 Iep, the full code have the postblit operator to correct this :)
- Guillaume Piolat (13/20) Sep 05 2016 string s;
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
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/1c981fdc71acOne 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
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):BTW on my pc your Custom String is faster than D stringI´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/1c981fdc71acOne 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
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/1c981fdc71acOk 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
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
On Monday, 5 September 2016 at 11:20:08 UTC, rikki cattermole wrote:On 05/09/2016 11:11 PM, Patric wrote: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 (!!!)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/1c981fdc71acOk 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
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:Do you have windows or linux? Which version of dmd you have?On 05/09/2016 11:11 PM, Patric wrote: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 (!!!)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/1c981fdc71acOk 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
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
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: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.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
On 05/09/2016 11:45 PM, Patric wrote:On Monday, 5 September 2016 at 11:20:08 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. Oh and you'll want to turn that into a slice at some point ``char[] str = ptr[0 .. length];``.On 05/09/2016 11:11 PM, Patric wrote: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 (!!!)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/1c981fdc71acOk 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
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
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/1c981fdc71acstring 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