alibaba / alibaba/async_simple
支持像thread_local一样的动态lazy local
- Dominant language
- C++
- Stars
- 2.2k
- Forks
- 316
- Avg merge
- 1d 13h
- Merged PRs (30d)
- 3
Description
目前的lazy local设计性能良好,但用户启动协程时必须事先知道需要访问哪些变量,并事先分配好。
是否能新增一种方式,允许用户动态的创建lazy local变量,并且在协程启动时无需知道需要访问哪些lazy local变量。就像`thread_local`那样,不要求线程启动时手动创建变量。
一种可能的实现思路:
```cpp
template
struct LazyLocalVariable {
// 每次初始化LazyLocalVariable都会获得一个不同的ID。
static std::size_t get_ID() noexcept {
return (std::size_t)ID;
}
};
class Lazy {
// ....
class LazyLocal {
// .....
// 在hash表中记录变量地址。
std::unique_ptr> hash_table_;
~LazyLocal() {
// free lazy local here
}
};
template
TransformAwaiter await_transform(LazyLocalVariable localVariable) {
if (!lazy_local_.hash_table_) [[unlikely]] {
lazy_local_.hash_table_ =
std::make_unique>();
}
// 查找id对应的变量地址
void*& address = lazy_local_.hash_table_[localVariable.get_ID()];
// 如果当前协程还未创建过变量,则创建该对象。
if (address == nullptr) [[unlikely]] {
address = new T{};
}
return TransformAwaiter{*address};
}
}
Lazy getLocalCounter() {
// 创建lazy local变量。
LazyLocalVariable var;
// 访问lazy local,如果当前协程还不存在这个变量,则立即分配。
int & i = co_await var;
// 该变量会在当前协程析构时被销毁
return i++;
}
Lazy hi() {
while (true) {
std::cout<<"now count:" << getLocalCounter()<
Contributor guide
No contributing guide indexed for this repository
Research direction
The issue names no files or tests; begin by locating the existing lazy-local implementation and the coroutine await_transform path. Compare the proposed dynamic hash-table lookup and per-coroutine destruction with current semantics, and define correctness and performance checks before implementation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- backend
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100