PistonDevelopers / dyon

A rusty dynamically typed scripting language
Apache License 2.0
1.77k stars 55 forks source link

Added async support #744

Closed bvssvni closed 8 months ago

bvssvni commented 8 months ago

See https://github.com/PistonDevelopers/dyon/issues/741

Compiled behind Cargo feature: "async"

To compile the "dyonrun" example with async:

cargo build --release --features="async" --example dyonrun

Why async?

Dyon uses a Go-like model for concurrency. However, until now this used one OS thread per co-routine. Since it is expensive to swap between threads in the OS, this becomes a bottleneck.

Async code in Rust makes it possible to execute thousands of tasks in parallel at much higher performance, using an executor runtime like Tokio.

Example

This example sums the first 10 000 natural numbers, creating a task for each number:

test.dyon

fn main() {
    xs := sift i 10_000 {go foo(i)}
    println(sum i len(xs) {
        x := pop(mut xs)
        unwrap(join(thread: x))
    })
}

fn foo(i: f64) -> bool {
    return clone(i)
}

On my laptop: Without async (default): 3.15s user 1.82s system 118% cpu 4.185 total With async: 0.06s user 0.02s system 155% cpu 0.057 total