apple / swift-nio

Event-driven network application framework for high performance protocol servers & clients, non-blocking.
https://swiftpackageindex.com/apple/swift-nio/documentation
Apache License 2.0
7.85k stars 633 forks source link

ByteBuffer: Investigate optimal allocation sizes (probably just under a power of two) #2729

Open weissi opened 1 month ago

weissi commented 1 month ago

Currently, ByteBuffer always allocates in powers of 2 which makes sense. But many allocators have a small amount of overhead which means the actual space reservation might be even bigger, possibly another power of two, often unnecessarily.

For example the following could happen:

This could yield to an extra page being mapped for nothing (we only needed 9000 bytes after all). NIO's ByteBuffer should probably allocate just under the next power of two to leave some slack for the allocator in case it needs it. Needs investigation.

This becomes even more important if/when ByteBuffer is able to use tail allocations.

hassila commented 1 month ago

See some note to consider in the related case:

https://github.com/apple/swift-nio/issues/2723#issuecomment-2135885509