Browse Source

优化state的内存布局和大小

tags/v2.9.7
tearshark 4 years ago
parent
commit
a61c9b2c64
5 changed files with 14 additions and 13 deletions
  1. 9
    0
      librf/src/def.h
  2. 2
    9
      librf/src/promise.h
  3. 1
    2
      tutorial/test_async_cb.cpp
  4. 1
    1
      tutorial/test_async_routine.cpp
  5. 1
    1
      vs_proj/librf.vcxproj

+ 9
- 0
librf/src/def.h View File

}; };
template<class T> template<class T>
using remove_cvref_t = typename remove_cvref<T>::type; using remove_cvref_t = typename remove_cvref<T>::type;
template<class _Ty>
constexpr size_t _Align_size()
{
const size_t _ALIGN_REQ = sizeof(void*) * 2;
return std::is_empty_v<_Ty> ? 0 :
(sizeof(_Ty) + _ALIGN_REQ - 1) & ~(_ALIGN_REQ - 1);
}
} }
#if defined(RESUMEF_DLL_EXPORT) #if defined(RESUMEF_DLL_EXPORT)

+ 2
- 9
librf/src/promise.h View File

future_type get_return_object(); future_type get_return_object();
void cancellation_requested(); void cancellation_requested();


static const size_t _ALIGN_REQ = sizeof(void*) * 2;

using _Alloc_char = std::allocator<char>; using _Alloc_char = std::allocator<char>;
void* operator new(size_t _Size) void* operator new(size_t _Size)
{ {
std::cout << "promise::new, size=" << _Size << std::endl; std::cout << "promise::new, size=" << _Size << std::endl;


_Alloc_char _Al; _Alloc_char _Al;
size_t _State_size = ((sizeof(state_type) + _ALIGN_REQ - 1) & ~(_ALIGN_REQ - 1));
size_t _State_size = _Align_size<state_type>();
char* ptr = _Al.allocate(_Size + _State_size); char* ptr = _Al.allocate(_Size + _State_size);
return ptr + _State_size;
return ptr;
} }


void operator delete(void* _Ptr, size_t _Size) void operator delete(void* _Ptr, size_t _Size)
void yield_value(); void yield_value();
}; };


template<class _Ty = void>
constexpr size_t promise_align_size()
{
return (sizeof(promise_t<_Ty>) + promise_t<_Ty>::_ALIGN_REQ - 1) & ~(promise_t<_Ty>::_ALIGN_REQ - 1);
}
} }



+ 1
- 2
tutorial/test_async_cb.cpp View File

void* frame_ptr = _coro_frame_ptr(); void* frame_ptr = _coro_frame_ptr();
size_t frame_size = _coro_frame_size(); size_t frame_size = _coro_frame_size();
std::cout << "test_routine_use_timer" << std::endl; std::cout << "test_routine_use_timer" << std::endl;
std::cout << "frame point=" << frame_ptr << ", size=" << frame_size << ", promise_size=" << promise_align_size<>() << std::endl;
std::cout << "frame point=" << frame_ptr << ", size=" << frame_size << ", promise_size=" << _Align_size<promise_t<>>() << std::endl;
auto handler = coroutine_handle<promise_t<>>::from_address(frame_ptr); auto handler = coroutine_handle<promise_t<>>::from_address(frame_ptr);
auto st = handler.promise()._state; auto st = handler.promise()._state;
auto parent = st->get_parent(); auto parent = st->get_parent();
std::cout << "st=" << st.get() << ", scheduler=" << sch << ", parent=" << parent << std::endl; std::cout << "st=" << st.get() << ", scheduler=" << sch << ", parent=" << parent << std::endl;
*/ */
resumef::awaitable_t<int64_t> awaitable; resumef::awaitable_t<int64_t> awaitable;
callback_get_long(val, [awaitable](int64_t val) callback_get_long(val, [awaitable](int64_t val)
{ {

+ 1
- 1
tutorial/test_async_routine.cpp View File

void* frame_ptr = _coro_frame_ptr(); void* frame_ptr = _coro_frame_ptr();
size_t frame_size = _coro_frame_size(); size_t frame_size = _coro_frame_size();
std::cout << "test_routine_use_timer" << std::endl; std::cout << "test_routine_use_timer" << std::endl;
std::cout << "frame point=" << frame_ptr << ", size=" << frame_size << ", promise_size=" << promise_align_size<>() << std::endl;
std::cout << "frame point=" << frame_ptr << ", size=" << frame_size << ", promise_size=" << _Align_size<promise_t<>>() << std::endl;
auto handler = coroutine_handle<promise_t<>>::from_address(frame_ptr); auto handler = coroutine_handle<promise_t<>>::from_address(frame_ptr);
auto st = handler.promise()._state; auto st = handler.promise()._state;

+ 1
- 1
vs_proj/librf.vcxproj View File

<ClCompile> <ClCompile>
<WarningLevel>Level3</WarningLevel> <WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization> <Optimization>Disabled</Optimization>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;ASIO_STANDALONE;RESUMEF_DEBUG_COUNTER=1;RESUMEF_ENABLE_MULT_SCHEDULER=1;RESUMEF_USE_BOOST_ANY=0;_SILENCE_CXX17_ALLOCATOR_VOID_DEPRECATION_WARNING=1;ASIO_DISABLE_CONCEPTS=1;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;ASIO_STANDALONE;RESUMEF_DEBUG_COUNTER=0;RESUMEF_ENABLE_MULT_SCHEDULER=1;RESUMEF_USE_BOOST_ANY=0;_SILENCE_CXX17_ALLOCATOR_VOID_DEPRECATION_WARNING=1;ASIO_DISABLE_CONCEPTS=1;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck> <SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>..\librf;..\..\asio\asio\include;..\..\asio-1.10.6\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>..\librf;..\..\asio\asio\include;..\..\asio-1.10.6\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalOptions>/await</AdditionalOptions> <AdditionalOptions>/await</AdditionalOptions>

Loading…
Cancel
Save