Small object optimization in type-erasure /concept-based polymorphism
1、是在阅读 open-std P1144R2 Object relocation in terms of move plus destroy 时,其中提及的:
Given a reliable way of detecting trivial relocatability, we can de-duplicate the code generated by small-buffer-optimized (SBO) type-erasing wrappers such as
std::function
andstd::any
.
2、关于这个topic的非常好的文章:
a、sean-parent Polymorphic Task Template in Ten
b、sodocumentation C++Type Erasure
3、Small buffer optimization是一种场景的optimization technique,由于type erasure中,通常涉及dynamic allocation,因此可以使用small buffer optimization来进行优化。
stackoverflow Easy way to implement small buffer optimization for arbitrary type erasure (like in std::function.)
I know nothing about the small buffer optimization required by the standard or any proposal, though it is often allowed or encouraged.
On the other hand, you can just make your own solution from scratch, based on the standard library (e.g. std::aligned_storage
). This may still verbose from the view of users, but not too hard.
Actually I implemented (not proposed then) any
with such optimization and some related utilities several years ago. Lately, libstdc++'s implementation of std::experimental::any
used the technique almost exactly as this (however, __
prefixed internal names are certainly not good for ordinary library users).
My implementation now uses some common helpers to deal with the storage. These helpers do ease to implement the type erasure storage strategy (at least fit for something similar to any
enough). But I am still interested in more general high-level solution to simplify the interface redirecting.