GithubHelp home page GithubHelp logo

eager-futures's Introduction

eager-futures

Introduction

This crate implements completion-handler based futures. It's intended for single-threaded asynchronous programming with reactor / proactor pattern, similar to the asynchrony model of Node.js.

Motivation

In Rust's native future, when a sub-future completes, the root task is awakened, which recursively polls children futures until reaching the true point of reentrance. In the future provided by this crate, when a sub-future completes, the next future to run is directly awakened, rather than having to be polled by parent futures. Essentially, each future is its own root task. This resembles Javascript's Promises, where they immediately start execution upon creation. Hence the future type provided by this create is named [Eager].

Consider the following code using Rust's native future. It simply chains n futures together using then, where each future performs some I/O operation simulated here by yield_now. One would expect the time for running it to be linear to n, but actually it's quadratic to n because each time the I/O operation completes, we have to poll through the next chain from the beginning. The future provided by this crate doesn't exhibit this behavior.

fn chain_many(n: isize) -> BoxFuture<'static, isize> {
    let mut future = async { 0 }.boxed();
    for _ in 0..n {
        future = future.then(|x| async move {
            tokio::task::yield_now().await;
            x + 1
        }).boxed()
    }
    future
}

Notes

Compared to Rust's native futures, this approach introduces dynamic allocation overhead. This crate is suitable only if your control flow is dynamically composed such as in the example above. For control flow that is mostly static, Rust's native future could perform much better thanks to optimizations such as aggressive inlining. This crate doesn't work with async / await.

License: BSD-2-Clause

eager-futures's People

Contributors

cyb0124 avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.