www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - H1 2015 - memory management

reply "AndyC" <andy squeakycode.net> writes:
The vision says "We aim to make the standard library usable in 
its entirety without a garbage collector. Safe code should not 
require the presence of a garbage collector"

I was just playing with std.json, and this part got me to 
thinking.  How will we parse a string into a json struct w/out 
allocating memory?

And if I allocate memory w/out the GC, then is the caller 
responsible to free it?

Things like string format and file read are gonna have to alloc 
memory, yes?  How will we make std.* usable w/out the GC?

-Andy
Feb 04 2015
next sibling parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 2/4/15 1:51 PM, AndyC wrote:
 How will we parse a string into a json struct w/out allocating memory?
It will allocate, but without creating garbage. Reference counted strings are the answer. -- Andrei
Feb 04 2015
prev sibling parent "Jonathan Marler" <johnnymarler gmail.com> writes:
On Wednesday, 4 February 2015 at 21:51:55 UTC, AndyC wrote:
 The vision says "We aim to make the standard library usable in 
 its entirety without a garbage collector. Safe code should not 
 require the presence of a garbage collector"

 I was just playing with std.json, and this part got me to 
 thinking.  How will we parse a string into a json struct w/out 
 allocating memory?

 And if I allocate memory w/out the GC, then is the caller 
 responsible to free it?

 Things like string format and file read are gonna have to alloc 
 memory, yes?  How will we make std.* usable w/out the GC?

 -Andy
It depends on the implementation, but it should be possible to parse an entire JSON file without allocating memory. Instead of creating new memory for every string in the JSON, you can just save a slice to the JSON itself. However, this will not work if the user intends on freeing the JSON memory later. Either way, this is an option I believe the library should provide. In some cases it's more efficient to read the entire JSON file into memory and just save slices into that memory. Note: If you need to escape strings you will have to modify the original JSON in memory, which may or may not be ok. Again, the library should provide a way to specify this. You could also allow the user to pass an allocator to the JSON library. This allows memory management to be implemented by the user instead of forcing everyone to use whatever memory management the JSON library uses. One last tip. If you don't care about preserving the original JSON, you can re-use the JSON memory by "moving" all the strings you need for your in-memory JSON at the begining of the JSON buffer. Original JSON memory: { "myid" : "hello" } "in-memory" JSON myidhello ^ ^ All the strings are moved to the beginning of the JSON buffer. Then you can decide if it's worth it to free the original JSON buffer and copy what you need to a new buffer or just keep the original buffer and ignore the wasted space left over.
Feb 04 2015