Live data from Hacker News

Concurrent JavaScript: It can work

webkit.org

181–189 of 189 posts

Re: Concurrent JavaScript: It can work

#181

Earlier quoted context omitted.

It would have been a shame if the OSes on which Firefox runs had similarly limited access to shared memory to one particular "safe" style in the spirit of paternalistic caution. Would Rust/Servo have been possible without access to the raw rope?

This isn't about "paternalistic caution," it's about purposeful and sensible engineering. JavaScript wasn't designed for threading, and it's a less natural fit for it compared to a language like C, so this is understandably a pretty serious undertaking. Your point is that this may enable some currently unimplementable application in JavaScript. The assumption is that Firefox couldn't have existed without a free-for-a…

Many factors and capabilities went into Firefox's success. While it's easy to enumerate the primitives required in hindsight, I'm doubtful that if OSes of the period had taken a restrictive stance based on contemporary ideas of what should be allowed, that an easy time would have been had.

This is not hypothetical, consider the present. While Firefox on iOS exists, it's just a branding skin over WebKit, due to a similar flavor of security paternalism around JITing code (only bad people write self modifying code :-). If Firefox had needed to differentiate itself originally in such a market, it's doubtful it would have had much success.

A threading free-for-all may be the wrong abstraction to use for many applications, but it has the virtue of being a decent stand-in for the hardware's actual capabilities. It's also close enough to ground truth that most other abstractions can be built on top of it. Imagine how unpleasant building a browser on top of Workers + ArrayBuffer transfer would be (especially given the lousy multi-millisecond latency of postMessage in most browsers). Also, consider that while there is often loud agreement that raw threads are dangerous, after decades of research, there's little consensus on the "right" option amongst many alternatives.

SharedArrayBuffer is nearly as powerful as the proposal, but not quite. For example, while it allows writing a multi-threaded tree processing library, it would have trouble exposing an idiomatic JS API if the trees in the library live in a single SAB (as JS has no finalizers etc. to enable release of sub-allocations of the SAB). The options are either one SAB per tree (which likely performs badly), an API where JS users need to explicitly release trees when done with them, or leaking memory. With the proposal, each tree node could be represented directly as a JS object. The proposal may not be the best way to fix this problem, but we definitely still have gaps in JS concurrency.

Agreed this would be a serious undertaking, however, and not to be lightly considered.

The proposal goes a long way to make the case this can be implemented performantly, but some deep thought should go into how it would alter / constraint future optimizations in JS JITs.

Re: Concurrent JavaScript: It can work

#182

Earlier quoted context omitted.

Because there was some huge breakthrough since eliminating threading issues? (If you answer just use Erlang, CSP or any other variation, well, my point exactly).

The only breakthrough is that some of us learned to use threads. Apparently some of us didn’t.

Apparently, after decades of CS and tons of costly errors, some of us still haven't learned than issues like memory safety, race conditions, etc are either handled at the language level, or are dangerous for everybody -- and that those that consider themselves immune to them because they "know how to use them" and are "better programmers" are just deluded.

Re: Concurrent JavaScript: It can work

#183

Earlier quoted context omitted.

For your parallel search example, the data set has to be extremely large for parallel searching to have a significant improvement. When does a client-side JS app have access to many GBs of local data that would justify a parallel algorithm? It seems exceedingly rare but maybe you can imagine an example. If you're talking about a server side app, if your goal is speed, why would you choose JS over C++? It seems more s…

Not parallel searching. Concurrent searching. The data set only has to be large enough that the search takes more than 1/60th of a second. Then it's profitable to do it concurrently. GC is not single threaded at all. In WebKit, it is concurrent and parallel, and already supports mutable threads accessing the heap (since our JIT threads access the heap). Most of the famous high-performance GC implementations are at le…

JavaScript can already do concurrent searching. Concurrent is logical, parallel is physical.

Efficient parallel GC is non-trivial to implement. In the most common implementation, you have to pause all threads before you can collect. That will often negate the performance benefits of having independent parallel threads running, especially if they are thrashing the heap with shared objects as you suggest.

Re: Concurrent JavaScript: It can work

#184

Earlier quoted context omitted.

This isn't about "paternalistic caution," it's about purposeful and sensible engineering. JavaScript wasn't designed for threading, and it's a less natural fit for it compared to a language like C, so this is understandably a pretty serious undertaking. Your point is that this may enable some currently unimplementable application in JavaScript. The assumption is that Firefox couldn't have existed without a free-for-a…

Many factors and capabilities went into Firefox's success. While it's easy to enumerate the primitives required in hindsight, I'm doubtful that if OSes of the period had taken a restrictive stance based on contemporary ideas of what should be allowed, that an easy time would have been had. This is not hypothetical, consider the present. While Firefox on iOS exists, it's just a branding skin over WebKit, due to a simi…

As it stands now, adding threading to JS has a negative expected value. There is more potential downside than potential upside. It's illogical and irrational to undertake the effort under those conditions.

This should be an industry driven decision. Wait for the users of SAB to say it's not meeting their needs, and for them to provide clear reasons why (not hypothetical limitations, not vague falsely-equivalent comparisons to Firefox). Then we can tangibly weigh the pros against the cons.

Right now this is a solution looking for a problem. Your analogy comparing the JS runtime to iOS runtime isn't appropriate, no single company controls the web platform. Mozilla or Google or Apple or Microsoft can push for JS threads if the arguments for it make sense. Compare to WebAssembly.

In fact the evolution of WebAssembly is a good example of how this ought to happen. Imagine if the creator of emscripten opted to instead first propose a new VM/IL for the web? It would never happen because JS was already good enough. It was more natural to use JS first then create the VM with the goal of addressing the limitations encountered with the JS approach.

Let the tangible shortcomings of SAB bubble to the surface. Then we can sensibly design something that effectively addresses those shortcomings. Not a pattern-matched solution looking for a problem.

Re: Concurrent JavaScript: It can work

#185
post #149

Earlier quoted context omitted.

This seems rather racy in Chrome, despite the I/O being deferred to the end :-) (function() { var x = ''; setTimeout(function() { setTimeout(function() { x += 'a'; }, 6); }, 4); setTimeout(function() { setTimeout(function() { x += 'b'; }, 5); }, 5); setTimeout(function() { console.log(x); }, 50); })(); It's a fair point that generally I/O is the source of the bulk of the non-determinism in most JS programs. But if th…

I object to the term "similar care". There's a big difference in what it takes to understand thread issues compared to single-threaded event-based systems. For instance, if I'm not mistaken, your setTimeout thing is relying on precise timing. But this is documented when you look up setTimeout. It's not difficult to understand. How to use locks and conditions/monitors correctly IS difficult to understand. You can't ju…

setTimeout is obviously a simpler primitive to understand in isolation than monitors/conditionvars, but I would argue that when used in equally complex scenarios, similar challenges emerge.

Something like Dinning Philosophers is no less tricky to express + understand with an event loop, and with JS's async await probably would most cleanly be expressed in a style that mirrors a monitors/conditionvars version.

The fact that JS has added async await suggests demand for the convenience of concurrent blocking threads. While async-await manages to separate out non-async code to a degree, once an await happens, the global state can also be arbitrarily mutated no differently than with threads.

  function block() { return new Promise(function(a,b) { setTimeout(a, 0); }); }
  var x = 0;
  (async function foo() {
     x = 1;
     await block();
     console.log(x);
  })().then();
  (async function bar() {
     x = 2;
  })().then();
Concurrency is hard, but event loops aren't a magic wand.

Re: Concurrent JavaScript: It can work

#186

Earlier quoted context omitted.

You just called WebKit a twisted mess. I asked you for examples. Looks like you don’t have any. Like I said, WebKit uses threads and it’s great. Maybe some day you will also learn to use threads.

coldtea literally never said that WebKit was a twisted mess. robertwhiuk, responding to you, referenced using "twisted mess" pizlonator: I think it would be weird if objects were thread- restricted by default. Concurrency is most fun when it's easy for a thread to access an object it wants. robertwhiuk: You say fun, but what you mean is twisted mess. No reference yet to WebKit until your response: WebKit uses threads…

>What is that "extra effort"? Discipline, care, good work. coldtea does not call WebKit a twisted mess. In fact, they seem to be saying the opposite. That it is not a twisted mess despite using threads.

Exactly, thanks.

Re: Concurrent JavaScript: It can work

#187

Earlier quoted context omitted.

coldtea literally never said that WebKit was a twisted mess. robertwhiuk, responding to you, referenced using "twisted mess" pizlonator: I think it would be weird if objects were thread- restricted by default. Concurrency is most fun when it's easy for a thread to access an object it wants. robertwhiuk: You say fun, but what you mean is twisted mess. No reference yet to WebKit until your response: WebKit uses threads…

coldtea was unable to come up with a single example of where WebKit is a twisted mess or even needs effort to avoid becoming one. No matter what kinds of semantics you throw at this, I think it’s the case that these arguments against threads lack basis.

>coldtea was unable to come up with a single example of where WebKit is a twisted mess

That's because coldtea (me) never said it IS a twisted mess.

What I said is that with threads, WebKit (and any program for that matter) needs extra effort to not be a twisted mess. Exactly what the parent explained again, and you don't seem to have got even this second time.

Why that is the case (in other words, why parallel programming with threads is harder and requires more effort than without), is CS 101.

Some reasons?

Synchronization and scheduling access to resources. Race conditions -- data Races, deadlocks, etc. Starvation. Balancing the number of threads (diminishing returns). Locking subtleties. VM/state/etc overhead of threading.

In fact you'd be hard pressed to find seasoned programmers that would disagree that threads are problematic and require extra caution.

Heck:

  "Although threads seem to be a small step from sequential computation, in fact, they 
  represent a huge step. They discard the most essential and appealing properties of 
  sequential computation: understandability, predictability, and determinism. Threads, as a 
  model of computation, are wildly nondeterministic, and the job of the programmer becomes one 
  of pruning that nondeterminism" -- https://www2.eecs.berkeley.edu/Pubs/TechRpts/2006/EECS-2006-1.pdf

Re: Concurrent JavaScript: It can work

#188

Yeah, and lets have the gazillion scripts, loaded by your usual favourite news site, spawn 10 threads each... For browsers, this is a bad idea. The biggest advantage of the event loop approach is that it is somewhat deterministic. With threads, that determinism goes out of the window. Lock-management? No way I do that for Browsers. I will quit working on client-side code if that is becoming a requirement! Threads for…

Because of user inputs and networking, we already do not have determinism. And even without introducing thread feature, a site can spawn 10 threads each by using Web workers. So, it is not related to introducing threads.

Re: Concurrent JavaScript: It can work

#189

Earlier quoted context omitted.

coldtea was unable to come up with a single example of where WebKit is a twisted mess or even needs effort to avoid becoming one. No matter what kinds of semantics you throw at this, I think it’s the case that these arguments against threads lack basis.

> coldtea was unable to come up with a single example of where WebKit is a twisted mess That's because coldtea (me) never said it IS a twisted mess. What I said is that with threads, WebKit (and any program for that matter) needs extra effort to not be a twisted mess. Exactly what the parent explained again, and you don't seem to have got even this second time. Why that is the case (in other words, why parallel progr…

You won’t weasel out on this that easily. You still have not provided anything other than the usual BS reasons for why threads require extra effort.

You know, drawing stuff to the screen also requires effort. As does file I/O. It’s computing - people get things wrong. The interesting question is: are threads so unusual in this regard that your whining makes any sense?

We have lots of threads but except when we are in the middle of something big and concurrency related like concurrent GC, we have very few bugs of the sort you describe. Most of the bugs that bother me right now are in the compiler and runtime and they are not concurrency bugs at all. It’s true that if someone is deliberately trying to increase concurrency, they will fix some concurrency bugs along the way just like a person writing compiler phases will fix compiler bugs alone the way. We haven’t given up on compilers just because they are might have bugs.

Finally, you said: “WebKit needs tons of extra effort to keep it from being a twisted mess precisely because it uses threads.” You seem to have now significantly walked back from this statement since all you can come up with are reasons why threads are hard that are not related to WebKit or any specific software package. Sounds like maybe you just don’t know how to use threads so you spread FUD to avoid having to learn them.

Post reply on HN