Introducing second-stack
second-stack is an allocator for slices which do not need to outlive the stack, but may not fit on the stack itself. Internally it is a growing stack.
second-stack was originally developed for writing dynamic buffers in WebGL (eg: procedurally generate some triangles/colors, write them to a buffer, and hand them off to the graphics card many times per frame without incurring the cost of many heap allocations). But, over time I found that needing a short-lived slice was common and using second-stack all over the place allowed for the best memory re-use and performance.
Potential further additions:
- Placement for large values that are not slices
- API to free the threadlocal (right now it's freed when the thread drops)
- Feature flag to globally enable/disable so libraries can use it without fear but have the application decide whether it makes sense for their threading model. (When disabled, a Vec<T> would be allocated for each call but use the same external API)
Links: