5 ms·
Yeah, I like Apple’s (Next’s) approach of pool allocation for each run through the event loop. Defer dealloc, drop pool at the end.
by projektfu 6y ago
Yeah, I like Apple’s (Next’s) approach of pool allocation for each run through the event loop. Defer dealloc, drop pool at the end.
- saurik 6y agoApple's pools are for helping manage reference counts of returned objects (via autorelease) but aren't doing pool allocation (as any of those objects could escape, so you can't do the fast thing of just deallocating the pool). The normal memory allocator is used while in the scope of an autorelease pool.
- projektfu 6y agoTrue, I was thinking the same after I posted. Maybe I’m thinking more of Apache’s approach and how it gets some of the benefits of a fork-per-request server without the overhead.
- andrekandre 6y agoright, so if you do @autoreleasePool { [[[SomeObject alloc] init] autorelease]; }//someobject is release after exiting this scope //inside main runloop, not inside @autorelease{} [[[AnotherObject alloc] init] autorelease]; //AnotherObject will be release at end of main runloop [[NotAutoreleased alloc] init]; //will leak past runloop iteration is that understanding correct?
- mcguire 6y agoUnless your destructors do more than deallocation, in which case you will leak whatever other resource you're managing.
- MaxBarraclough 6y agoA pool can invoke destructors when it is cleared. Might take a bit of overhead (if the pool is to support arbitrary classes), but you could retain the fast pointer-bump allocation.
- ShroudedNight 6y agoNot Rust, C++, but in case anybody wants an example: https://github.com/eclipse/omr/commit/fd99ca42fdbed76cc00e68a5931fcf5dc23a1d09 https://github.com/eclipse/omr/commit/fd99ca42fdbed76cc00e68... TR::Region is the slab allocator used by the JIT in OpenJ9/OMR. The linked commit adds functionality for calling destructors of arbitrary types allocated in the Region.
- MaxBarraclough 6y agoInteresting, thanks. Seems to require use of a common base class though, i.e. to use a class with this pool, you have to inherit from Destructable, or else create a subclass that does. Seems ugly. Perhaps that's the only way it can be done in C++ though.
- projektfu 6y agoIt does create an object with that base class for the allocator but returns an object of the type you want from the allocator, using a template (Region::Instance<T>). So the API user only sees the types it's interested in.