Giter Site home page Giter Site logo

tco's Introduction

TCO

TCO is a tail-call optimization library. It's a proof-of-concept attribute macro you can slap onto item functions to optimize them if they are in tail-calling format.

Limitations

It's not very smart.

It only works on free functions (eg. fn foo(bar: Bar) -> u32 not in an impl block).

It can have problems with passing a non-copy argument.

It's untested with references.

It doesn't support mutual recursion.

It supports only basic patterns in the function argument (no tuple destructuring).

It can't turn a non-tail calling into a tail calling function.

Help wanted

This is just a proof-of-concept. I'd love help fleshing it out.

Acknowledgements

Thanks to user aloso for providing a better user experience by turning panics into compile errors and contributing a fix to prevent the optimization for happening to non tailcalling functions.

Alternatives

Examples

Enough talk, examples!

Sync Example: Factorial

#[tco::rewrite]
fn fac_with_acc(n: u128, acc: u128) -> u128 {
    if n > 1 {
        fac_with_acc(n - 1, acc * n)
    } else {
        acc
    }
}

expands to

fn fac_with_acc(n: u128, acc: u128) -> u128 {
    let mut n = n;
    let mut acc = acc;
    '__tco_loop: loop {
        return {
            if n > 1 {
                {
                    let __tco_0 = (n - 1, acc * n);
                    n = __tco_0.0;
                    acc = __tco_0.1;
                    continue '__tco_loop;
                }
            } else {
                acc
            }
        };
    }
}

Async Example: Factorial

#[tco::rewrite]
async fn fac_with_acc(n: u128, acc: u128) -> u128 {
    if n > 1 {
        fac_with_acc(n - 1, acc * n).await
    } else {
        acc
    }
}

expands to

async fn fac_with_acc(n: u128, acc: u128) -> u128 {
    let mut n = n;
    let mut acc = acc;
    '__tco_loop: loop {
        return {
            if n > 1 {
                {
                    let __tco_0 = (n - 1, acc * n);
                    n = __tco_0.0;
                    acc = __tco_0.1;
                    continue '__tco_loop;
                }
            } else {
                acc
            }
        };
    }
}

without the tco::rewrite attribute, you instead get the folliwing error:

error[E0733]: recursion in an `async fn` requires boxing
 --> $DIR/await_no_tco.rs:6:46
  |
6 | async fn fac_with_acc(n: u128, acc: u128) -> u128 {
  |                                              ^^^^ recursive `async fn`
  |
  = note: a recursive `async fn` must be rewritten to return a boxed `dyn Future`

tco's People

Contributors

aloso avatar samsieber 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.