On 2017-04-14 16:04, Sönke Ludwig wrote:

If the allocator can be designed to be initialized lazily, using a TaskLocal!Allocator global (TLS) variable should work. The destructor will be run at the end of each task invocation. If creating/destroying the allocator per task is likely to be too inefficient, I'd probably use a thread local free list of allocators where each task just takes one and puts it back after use. This could also be wrapped within a lazy initialized task local variable to make it transparent to use.

A free list sounds like a good idea. The example below looks like a
simpler solution than a task local variable.

A HTTP specific solution could also inject a middleware by simply wrapping the request handler:

auto router = new URLRouter;
// ...

void setupAllocator(HTTPServerRequest req, HTTPServerResponse res) {
    scope (exit) allocator.teardown();
    router.handleRequest(req, res);

listenHTTP(httpsettings, &setupAllocator);

Aha, I didn't know that, thanks.

I guess I need to do some performance testing to really see which
solution works best.

/Jacob Carlborg