GC vs. Manual Memory Management Real World Comparison

Benjamin Thaut code at benjamin-thaut.de
Fri Sep 7 14:06:50 PDT 2012


Am 07.09.2012 18:36, schrieb Andrei Alexandrescu:
>
> You mentioned some issues in Phobos with memory allocation, that you had
> to replace with your own code. It would be awesome if you could post
> more about that, and possibly post a few pull requests where directly
> applicable.
>
> Thanks,
>
> Andrei

Let me give a bit more details about what I did and why.

Druntime:

I added a reference counting mechanism. core.refcounted in my druntime 
branch.
I created a reference counted array which is as close to the native D 
array as currently possible (compiler bugs, type system issues, etc). 
also in core.refcounted. It however does not replace the default string 
or array type in all cases because it would lead to reference counting 
in uneccessary places. The focus is to get only reference couting where 
absolutly neccessary. I'm still using the standard string type as a 
"only valid for current scope" kind of string.
I created a allocator base interface which is used by everything that 
allocates, also I created replacement templates for new and delete.
Located in core.allocator
I created a new hashmap container wich is cache friendly and does not 
leak memory. Located in core.hashmap
I created a memory tracking allocator in core.allocator which can be 
turned on and off with a version statement (as it has to run before and 
after module ctors dtors etc)

I changed all parts of druntime that do string processing to use the 
reference counted array, so it no longer leaks. I made the Thread class 
reference counted so it no longer leaks. I fixed the type info 
comparsion and numerous other issues. Of all these changes only the type 
info fix will be easily convertible into the default druntime because it 
does not depend on any of my other stuff. I will do a merge request for 
this fix as soon as I find some time.

Phobos:
I threw away most of phobos because it didn't match my requirements.
The only modules I kept are
std.traits, std.random, std.math, std.typetuple, std.uni

The parts of these modules that I use have been changed so they don't 
leak memory. Mostly this comes down to use reference counted strings for 
exception error message generation.

I did require the option to specify a allocator for any function that 
allocates. Either by template argument, by function parameter or both, 
depending on the case. As custom allocators can not be pure this is a 
major issue with phobos, because adding allocators to the functions 
would make them unpure instantly. I know about the C-Linkage pure hack 
but its really a hack and it does not work for templates.

So I think most of my changes are not directly applicable because:

- You most likely won't like the way I implemented reference counting
- You might won't like my allocator design
- My standard library goes more into the C++ direction and is not as 
easly usable as phobos (as performance comes first for me, and usability 
is second)
- All my changes heavily depend on some of the functionality I added to 
druntime.
- The neccessary changes to phobos would break a lot of code because 
some of the function properties like pure couldn't be used any more, as 
a result of language limitations.

Kind Regards
Benjamin Thaut


More information about the Digitalmars-d-announce mailing list