On Thu, Jan 29, 2026 at 09:17:46AM -0500, Patrick Palka wrote:
> The flatten attribute on a function tells the compiler to recursively
> inline all calls within it. The DFS entryoint _M_dfs, now that it's
> non-recursive, is a good candidate for this attribute because its
> implementation spans multiple subroutines that are only called from
> _M_dfs, and so should obviously be inlined, but the compiler doesn't
> know this (specifically it doesn't know that the subroutines are used
> in the same way by every TU).
>
> We could instead annotate each of its subroutines with always_inline,
> but this forces the function to get inlined even with -O0, which we
> don't really want here, whereas flatten only has an effect when
> compiling with optimizations AFAICT. It also has the benefit of
> inlining all the repetitive std::vector operations which themselves
> are quite hot.
You could also use
#ifdef __OPTIMIZE__
[[__gnu__::__always_inline__]]
#endif
For a flatten, is there a guarantee that it calls only functions defined in
libstdc++-v3 headers and nothing else (say some user class ctors/dtors
or whatever else supplied by the user)? Because if user can instantiate it
with some huge functions (or small functions with very deep call graphs),
flatten would try to inline all that and could cause huge compile time/memory
regressions.
Jakub