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

@@ -68,6 +68,15 @@ namespace resumef
};
template<class T>
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)

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

@@ -32,17 +32,15 @@ namespace resumef
future_type get_return_object();
void cancellation_requested();

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

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

_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);
return ptr + _State_size;
return ptr;
}

void operator delete(void* _Ptr, size_t _Size)
@@ -71,10 +69,5 @@ namespace resumef
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

@@ -27,7 +27,7 @@ future_t<int64_t> async_get_long(int64_t val)
void* frame_ptr = _coro_frame_ptr();
size_t frame_size = _coro_frame_size();
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 st = handler.promise()._state;
@@ -35,7 +35,6 @@ future_t<int64_t> async_get_long(int64_t val)
auto parent = st->get_parent();
std::cout << "st=" << st.get() << ", scheduler=" << sch << ", parent=" << parent << std::endl;
*/
resumef::awaitable_t<int64_t> awaitable;
callback_get_long(val, [awaitable](int64_t val)
{

+ 1
- 1
tutorial/test_async_routine.cpp View File

@@ -16,7 +16,7 @@ future_t<> test_routine_use_timer()
void* frame_ptr = _coro_frame_ptr();
size_t frame_size = _coro_frame_size();
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 st = handler.promise()._state;

+ 1
- 1
vs_proj/librf.vcxproj View File

@@ -104,7 +104,7 @@
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<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>
<AdditionalIncludeDirectories>..\librf;..\..\asio\asio\include;..\..\asio-1.10.6\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalOptions>/await</AdditionalOptions>

Loading…
Cancel
Save