The Small Functor Optimization in std::function was implemented before we started to respect over-aligned types. Currently, we emit silent bad codegen when we decide to locally store an over-aligned function object.
C:\Temp>type meow.cpp
#include <functional>
#include <stdint.h>
#include <stdio.h>
using namespace std;
struct alignas(16) Meow {
int i;
void operator()() {
printf("This should be 0: %d\n", static_cast<int>(reinterpret_cast<uintptr_t>(this) % alignof(Meow)));
}
};
int main() {
function<void()> f(Meow{});
int x = 1729;
function<void()> g(Meow{});
f();
g();
(void) x;
}
C:\Temp>cl /EHsc /nologo /W4 /wd4324 meow.cpp
meow.cpp
C:\Temp>meow
This should be 0: 0
This should be 0: 8
Casey Carter (@CaseyCarter) observed: "I think all that's required here is to add || alignof(_Impl) > alignof(max_align_t) to _Is_large and write a quick regression test."
|
template <class _Impl> // determine whether _Impl must be dynamically allocated
|
|
_INLINE_VAR constexpr bool _Is_large = (_Space_size < sizeof(_Impl)) || !_Impl::_Nothrow_move::value;
|
|
union _Storage { // storage for small objects (basic_string is small)
|
|
max_align_t _Dummy1; // for maximum alignment
|
|
char _Dummy2[_Space_size]; // to permit aliasing
|
|
_Ptrt* _Ptrs[_Small_object_num_ptrs]; // _Ptrs[_Small_object_num_ptrs - 1] is reserved
|
|
};
|
Tracked by Microsoft-internal VSO-157296 and VSO-1062649.
The Small Functor Optimization in
std::functionwas implemented before we started to respect over-aligned types. Currently, we emit silent bad codegen when we decide to locally store an over-aligned function object.Casey Carter (@CaseyCarter) observed: "I think all that's required here is to add
|| alignof(_Impl) > alignof(max_align_t)to_Is_largeand write a quick regression test."STL/stl/inc/functional
Lines 797 to 798 in 2239101
STL/stl/inc/functional
Lines 1065 to 1069 in 2239101
Tracked by Microsoft-internal VSO-157296 and VSO-1062649.