www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Learning resources for std.experimental.allocator

reply xtreak <tir.karthi gmail.com> writes:
I am newbie to D learning it for sometime using Ali's book. I 
came across std.experimental.allocator and read through 
http://dlang.org/library/std/experimental/allocator/building_blocks.html . Can
someone explain me the actual benefits of using this and if so any benchmarks
explaining the advantage. Maybe its too advanced for me as a beginner now its
just I am curious over the usage and applications with a simple hello world
like example explaining what this library will accomplish. I asked it
previously at https://www.reddit.com/r/programming/comments/4hti33/dconf_2016_is_streamin
_right_now/d2satic/ but still I don't understand the exact applications of the
same.

A simple hello world like example over its advantage will be very 
much helpful.

PS : Sorry for the crosspost. I posted it to learn group but 
didn't receive any suggestions on the same at 
http://forum.dlang.org/post/bhnwudqeanbxjgooouxg forum.dlang.org
Jan 05 2017
next sibling parent reply Edwin van Leeuwen <edder tkwsping.nl> writes:
On Thursday, 5 January 2017 at 11:09:01 UTC, xtreak wrote:
 Can someone explain me the actual benefits of using this and if 
 so any benchmarks explaining the advantage.
Benefits compared to what? Compared to using the GC?
Jan 05 2017
parent xtreak <tir.karthi gmail.com> writes:
On Thursday, 5 January 2017 at 13:16:44 UTC, Edwin van Leeuwen 
wrote:
 On Thursday, 5 January 2017 at 11:09:01 UTC, xtreak wrote:
 Can someone explain me the actual benefits of using this and 
 if so any benchmarks explaining the advantage.
Benefits compared to what? Compared to using the GC?
Yes, I want to know the benefits with respect to GC and if its going to reduce the usage of GC across the language implementation.
Jan 05 2017
prev sibling next sibling parent jmh530 <john.michael.hall gmail.com> writes:
On Thursday, 5 January 2017 at 11:09:01 UTC, xtreak wrote:
 I am newbie to D learning it for sometime using Ali's book. I 
 came across std.experimental.allocator and read through 
 http://dlang.org/library/std/experimental/allocator/building_blocks.html . Can
someone explain me the actual benefits of using this and if so any benchmarks
explaining the advantage. Maybe its too advanced for me as a beginner now its
just I am curious over the usage and applications with a simple hello world
like example explaining what this library will accomplish. I asked it
previously at https://www.reddit.com/r/programming/comments/4hti33/dconf_2016_is_streamin
_right_now/d2satic/ but still I don't understand the exact applications of the
same.

 A simple hello world like example over its advantage will be 
 very much helpful.

 PS : Sorry for the crosspost. I posted it to learn group but 
 didn't receive any suggestions on the same at 
 http://forum.dlang.org/post/bhnwudqeanbxjgooouxg forum.dlang.org
I also have found std.experimental.allocator's documentation is written at quite the advanced level. There is definitely scope for someone to improve it (for instance, the main page presupposes that the reader already knows what allocators are) or write a nice tutorial. Nevertheless, the building blocks page is actually about how to build your own allocator. You can use std.experimental.allocator without building your own. The page https://dlang.org/phobos/std_experimental_allocator.html actually has some very simple examples on how to allocate an int or an array and dispose of them. You might also consider reviewing the following threads http://forum.dlang.org/post/vfipmwojmvseqxoiwndr forum.dlang.org http://forum.dlang.org/post/zbzjtssagejcxpxittij forum.dlang.org
Jan 05 2017
prev sibling parent reply Chris Wright <dhasenan gmail.com> writes:
On Thu, 05 Jan 2017 11:09:01 +0000, xtreak wrote:
 Can someone explain me the actual benefits of using this
The target audience is people writing libraries that need to allocate memory, where the libraries' target audiences care about performance more than most. GC allocation and scans can be expensive. Far more so when you have to deal with interior pointers, which are common in D (thanks to how arrays are designed). Real-time programming especially isn't allowed to touch the GC in general because it could cause the program to pause for an unpredictable amount of time. There are a lot of other approaches to memory management. You might, for instance, have an allocator that just holds a large buffer of memory and a pointer to the first free segment of it. You can't free up arbitrary segments of memory from it and expect them to be reused. This is convenient when you are dealing with a task that allocates its own memory and then finishes without sharing that memory anywhere else. Or you could use malloc, which is better for minimizing memory use and amortizes the cost of memory management better than a GC. If you write your code to use std.experimental.allocator, then other people who depend on it can decide what strategy works for them. The downside is that you must write your code in the least capable way available. You can't rely on resources being automatically cleaned up at any point because the person might be using an allocator that just calls malloc and free. As for benchmarks, an allocator is just a thin wrapper around malloc/ free, or a thin wrapper around the GC, or the like. The differences should be relatively obvious. There are some other interesting things you can do with this, though, regarding tracking how you use memory. Or you could use it to better benchmark malloc vs GC for your application.
Jan 05 2017
parent reply Ryan <clumsycodemonkey gmail.com> writes:
On Friday, 6 January 2017 at 03:28:43 UTC, Chris Wright wrote:

 There are some other interesting things you can do with this, 
 though, regarding tracking how you use memory. Or you could use 
 it to better benchmark malloc vs GC for your application.
I did this just to see what the difference was and I was surprised by the results. Just using the allocator interface with the GC caused a speed up, then the additional speed up from using malloc/free was very small in comparison. https://forum.dlang.org/post/rurbbpuqnfepllecqzla forum.dlang.org
Jan 06 2017
parent xtreak <tir.karthi gmail.com> writes:
On Friday, 6 January 2017 at 23:39:09 UTC, Ryan wrote:
 On Friday, 6 January 2017 at 03:28:43 UTC, Chris Wright wrote:

 There are some other interesting things you can do with this, 
 though, regarding tracking how you use memory. Or you could 
 use it to better benchmark malloc vs GC for your application.
I did this just to see what the difference was and I was surprised by the results. Just using the allocator interface with the GC caused a speed up, then the additional speed up from using malloc/free was very small in comparison. https://forum.dlang.org/post/rurbbpuqnfepllecqzla forum.dlang.org
Thanks a lot for sharing this.
Jan 07 2017