can delegating yield be as fast as a normal function call?

# Jelle van den Hooff (11 years ago)

I would like to write taskjs coroutines, but I don't want to worry about what functions might yield. Instead, I plan to allow any function to yield by rewriting all my function calls to delegating yield:

Before:

  function fetch_url(url) {
    return yield create_promise_for_url(url);  // will not work as fetch_url is not yet a generator
  }

  function fetch_feed(name) {
    return fetch_url(url_for_feed[name]);
  }

  function get_rss_feeds() {
    return [
      fetch_feed('bbc'), fetch_feed('slashdot'), fetch_feed('cnn')
    ];
  }

  var task = spawn(get_rss_feeds());

After:

  function* fetch_url(url) {
    return yield create_promise_for_url(url); // now this promise will be passed all the way to spawn
  }

  function* fetch_feed(name) {
    return yield* fetch_url(url_for_feed[name]);
  }

  function* get_rss_feeds() {
    return [
      yield* fetch_feed('bbc'), yield* fetch_feed('slashdot'), yield* fetch_feed('cnn')
    ];
  }

  var task = spawn(get_rss_feeds());

I expect coroutine creation (the call to get_rss_feeds()) to be slow as it needs to allocate a new stack. The nested yield* calls perform no stack switching and could be as simple as normal function calls. The yield of the promise would switch stacks, but is free to ignore all intermediary yield* calls.

Essentially, I would like the Javascript VM to undo my rewrite. Do the delegating yield semantics allow a VM to transform nested yield* calls into normal function calls?

For reference, nested yield* calls in v8 are currently quite slow; compare for example yield_fac(10) and yield_then_plain_fac(10) on jsperf.com/delegating-yield.

# Andy Wingo (11 years ago)

This isn't really an es-discuss topic, as it is about performance of implementations rather than the language itself.

On Wed 12 Feb 2014 18:08, Jelle van den Hooff <jelle at vandenhooff.name> writes:

Do the delegating yield semantics allow a VM to transform nested yield* calls into normal function calls?

Sure. I don't think it will happen any time soon, though; it would take a considerable amount of hacking.

Andy

# David Bruant (11 years ago)

Le 20/02/2014 15:03, Andy Wingo a écrit :

Hi,

This isn't really an es-discuss topic, as it is about performance of implementations rather than the language itself.

Stating my own opinion only on behalf of myself: I think this thread appropriate for es-discuss. How developers use the language and what they would expect from implementations does inform how the language may/should/could evolve.