Quotes of the week
Posted Apr 22, 2012 3:26 UTC (Sun) by Cyberax
(✭ supporter ✭
In reply to: Quotes of the week
Parent article: Quotes of the week
Yup. And what if your objects are MOSTLY short-lived, but sometimes they are not?
Then you're in a world of hurt. For example, consider a generic library code:
json_value parse_value(const char *begin, const char *end)
if (value_is_int(begin, end))
res.as_int = parse_as_int(begin, end);
else if (value_is_string(begin, end))
//Hmm. What should I do here?
res.as_string = malloc(end-begin+1);
//strcpy, unquoting, whatever skipped
What allocator should I use here?
Hm. That means I need to pass "allocator_ptr *" into all my functions working with dynamic RAM! And all allocations go through at least one additional level of indirection (remember, in Java ALL small-object allocations are roughly simple pointer increments).
Then there's a question of API. For example, STL in C++ doesn't really support statefull allocators (speaking from experience). Plain C neatly sidesteps this problem by not having a standard library worth its name.
But that's not all.
Suppose that we have a network server with usual request/response cycle. It's natural to use thread-local arenas linked to the request cycle. That way you can delete everything at once at the end of the request.
However, what if I want to add some asynchronousity later and offload some requests (that I know to be long running) to a separate thread?
Whoops. Arenas stop to be a good idea because either you have to pass the whole arena to another thread or make a deep copy of arena-local objects before passing them to another thread. Both solutions are not nice.
With GC I can simply pass an object reference to another thread and forget about it. No messing around with arenas, no nothing. And I get all the speed benefits of thread-local arenas with only some additional overhead.
That's analogous to comparing writing code in high-level language with writing code in assembly. Yeah, it's possible to do everything high-level language can do in assembly (duh). But is it worth it?
to post comments)