从基础开始解释 async-task 的内部结构

  • async-task crate: It is one of the most complicated crates in the smol ecosystem and is just a future on the heap. It predates smol and was originally used as the task implementation for async-std.
  • smol package: The author prides himself on making smol packages easy to parse for beginners.
  • futures-lite crate: Used to run two futures at the same time easily by using the zip combinator and block_on.
  • Scalability solutions: The zip combinator has limitations for higher-level scenarios. The slab crate is used to fit futures and boxed Future to use multiple implementors. A GiantZip struct is created to poll multiple futures at once. But it has problems like being inefficient and unfair.
  • Queue structure: A queue structure is added to the GiantZip to solve the O(n) problem and the fairness problem. The queue contains the indexes of the futures that are ready to be woken. The poll implementation is adjusted to pop from the queue instead of polling each future.
  • Persistent problems: There are inefficiencies in the current implementation like three main allocations (a Vec, a Box for each future, and a Waker). There are also issues like using usize as an index, not being able to get the result of a future, and difficulty in removing a running future. These problems will be addressed in the next blog post when building a real task abstraction.
阅读 13
0 条评论