Why and How to Roll Your Own std::function Implementation
summary
Reimplements std::function inside JUCE with a vtable-style type eraser, benchmarks showing parity with libstdc++ and up to ~30% speed improvement when forcing stack storage, and explains why heap allocations cause audible glitches in audio threads. Adds small buffer optimization with selectable stack size, an option that fails to compile if a callable is too large providing a compile-time no-alloc guarantee, and discusses allocator-pooling and move-only alternative patterns (Folly/SG14 comparisons).
AI-written summary · about
chapters (13)
- 0:00Introduction and motivation
- 3:39Why std::function matters
- 8:19Problems with std::function
- 10:51Implementing std::function
- 14:03Benchmarking performance
- 17:21Memory overhead analysis
- 19:32Optimizing with vtables
- 21:23Compile-time allocation safety
- 22:51Memory management with allocators
- 24:55Indirection and flexibility
- 27:07When to avoid type erasure
- 29:19Conclusion and takeaways
- 30:23Q&A
related talks
01h 13mNot Leaving Performance On The Jump Table
31m“folly::Function: A Non-copyable Alternative to std::function”
01h 29mHigher-order functions and function_ref
01h 02mFunctional C++
01h 01mHigher-order functions in Modern C++: existing techniques and function_ref
01h 01mFunctional C++
01h 17mRefactoring Legacy C++ OOP Application Code with Functional Programming and Type Erasure Concepts
46mPractical Functional Programming in C++
