D needs emplacement new
Namespace via Digitalmars-d
digitalmars-d at puremagic.com
Sat Apr 25 11:29:14 PDT 2015
Consider both of these scripts: test_gc_new.d
(http://dpaste.dzfl.pl/4f36b165c502) and test_gc_emplace_new.d
(http://dpaste.dzfl.pl/ff4e3c35479f)
and these results:
test_gc_new:
>test_gc_new.exe "--DRT-gcopt=profile:1"
Number of collections: 140
Total GC prep time: 0 milliseconds
Total mark time: 5 milliseconds
Total sweep time: 9 milliseconds
Total page recovery time: 2 milliseconds
Max Pause Time: 0 milliseconds
Grand total GC time: 18 milliseconds
GC summary: 5 MB, 140 GC 18 ms, Pauses 6 ms < 0 ms
test_gc_emplace_new:
>test_gc_emplace_new.exe "--DRT-gcopt=profile:1"
Number of collections: 2
Total GC prep time: 0 milliseconds
Total mark time: 0 milliseconds
Total sweep time: 0 milliseconds
Total page recovery time: 0 milliseconds
Max Pause Time: 0 milliseconds
Grand total GC time: 0 milliseconds
GC summary: 5 MB, 2 GC 0 ms, Pauses 0 ms < 0 ms
As you can see, thanks to the emplacement, I have 138 less GC
collections. In C++ you can do
new (f) Foo(...) instead of my New!(Foo)(f, ...)
The C++ version is a lot more readable, nicer and safer.
Therefore D should (since it says it is a system language like
C++) add an equal syntax or, at least, implement a library
version like mine to allow people a nice and easy way to reuse
their object. We already have emplace (which my implementation
uses) but to write emplace((cast(void*) obj)[0 ..
__traits(classInstanceSize, Foo)], ...) is not nice, not
intuitive and not safe and can be improved.
More information about the Digitalmars-d
mailing list