Re: Sync API for workers

Ok, this thread is clearly heading off the deep end. Let me clear up a
few points of confusion:

* You can not wrap a truly synchronous library around an asynchronous
API. Spinning the event loop gets you close, but breaks
run-to-completion. Furthermore, spinning the event loop is irrelevant
as we don't have an API to do that, nor are we planning to introduce
one.
* yield only works within generators in JS.
* You could solve the use case of compile-to-JS for code that uses
sync APIs using yield. However it requires changing all functions into
generators, and all function calls into yield* statements. That comes
at a performance overhead that is significant enough as to make it an
unacceptable solution (several times slower in current
implementations).
* You could likewise solve the compile-to-JS use case by instead of
generating plain JS generate JS that implements a virtual machine that
runs the compiled code. This would allow pausing the virtual machine
whenever an async call is happening. The performance overhead here is
simply too large (in fact, that's essentially what using generators
and yield* does).
* yield would not solve the use-case of allowing libraries that use
features from the main thread as it, again, would require a rewrite of
all code that directly or indirectly uses that library to change all
functions into generators and all function calls into yield*.

/ Jonas

On Sun, Oct 13, 2013 at 9:33 AM, piranna@gmail.com <piranna@gmail.com> wrote:
> Don't know, I only know behavior of Python yield statement, but Javascript
> one was developed following it and I'm 90% secure it follows the same
> behaviour (almost all new functionalities of Javascript are being borrowed
> from Python since seems Mozilla Javascript implementors are Python
> ex-programmers in purpose) so yes, I believe it should work this way :-)
>
> El 13/10/2013 18:27, "James Greene" <james.m.greene@gmail.com> escribió:
>
>> Oh, does `yield` work anywhere? I thought it was only for use within
>> generators. Admittedly, I haven't been keeping up with the latest ES6
>> changes.
>>
>> On Oct 13, 2013 9:38 AM, "piranna@gmail.com" <piranna@gmail.com> wrote:
>>>
>>> Javascript now has support for yield statements the same way Python does,
>>> that's a way to stop (ie. sleep) the execution of a script to allow another
>>> to work and restart from there. It's not their main function, but allow to
>>> create what's called "greenlets", green threads, and that's how I seen sync
>>> APIs are build in top of async ones...
>>>
>>> El 13/10/2013 16:21, "James Greene" <james.m.greene@gmail.com> escribió:
>>>>
>>>> > a) is necessary, but for b) it is sufficient for the sync thread to be
>>>> > able to sleep until a condition/mutex/... is lifted
>>>>
>>>> In other words, your clarification is completely true but my initial
>>>> statement was written with regard to client-side JavaScript, which cannot
>>>> sleep. As such, I believe my original assertions are still correct with
>>>> regard to writing a sync wrapper in JS.
>>>>
>>>> On Oct 13, 2013 9:09 AM, "James Greene" <james.m.greene@gmail.com>
>>>> wrote:
>>>>>
>>>>> Thanks for adding clarification. That CAN be true but it depends on the
>>>>> environment [so far as I can see].
>>>>>
>>>>> For example, such an API wrapper couldn't be built in today's
>>>>> client-side JavaScript because the UI thread can't do a synchronous yielding
>>>>> "sleep" but rather can only do a synchronous blocking wait, which means it
>>>>> wouldn't yield to allow for the Worker thread to asynchronously respond and
>>>>> toggle such a condition/mutex/etc. unless such can be synchronously
>>>>> requested by the blocking thread from within the busy wait loop (e.g.
>>>>> `processEvents();`) as browsers won't interrupt the synchronous flow of the
>>>>> JS busy loop to trigger `onmessage` handlers for async messages sent from
>>>>> the Worker.
>>>>>
>>>>> If I'm mistaken, please consider providing a code snippet, gist, etc.
>>>>> to get me back on track. Thanks!
>>>>>
>>>>> On Oct 13, 2013 5:06 AM, "David Rajchenbach-Teller"
>>>>> <dteller@mozilla.com> wrote:
>>>>>>
>>>>>> On 10/12/13 3:48 PM, James Greene wrote:
>>>>>> > You can only build a synchronous API on top of an asynchronous API
>>>>>> > if
>>>>>> > they are (a) running in separate threads/processes AND (b) the sync
>>>>>> > thread can synchronously poll (busy loop) for the
>>>>>> > progress/completion of
>>>>>> > the async thread.
>>>>>>
>>>>>> a) is necessary, but for b) it is sufficient for the sync thread to be
>>>>>> able to sleep until a condition/mutex/... is lifted
>>>>>>
>>>>>>
>>>>>> --
>>>>>> David Rajchenbach-Teller, PhD
>>>>>>  Performance Team, Mozilla

Received on Sunday, 13 October 2013 19:40:02 UTC