News for hackers. Stuff that matters.
Re:Mozilla can produce near-native performance on the Web by npalli on Wednesday May 22, 2013 @11:16PM (#5755139)
BTW, C/C++ code that is compared has multithreading and SIMD capability disabled. In the third page they benchmark asm.js and native with multithreading and SSE enabled. It shows upto 50x slowdown! Not exactly sure what the point of a benchmark without multithreading is. I mean anyone writing performance sensitive apps will use MT right? (given JS is single threaded, it seems this is a dealbreaker).

http://cdn.arstechnica.net/wp-content/uploads/2013/05/classi...

Re:Mozilla can produce near-native performance on the Web by azakai on Wednesday May 22, 2013 @11:24PM (#5755172)
Of course single-threaded code will lose to SIMD or threaded code. That's not saying anything surprising.

The important thing is to realize that until just recently - a few months or so - people did not believe that SINGLE-threaded JS code on the web could be close to native speed. The Ars article here even has "surprise" in the title when it concludes that in fact that is possible :)

So that is a crucial milestone. Is the job done? No, as you say, SIMD and threads are important. But we had to first get single-threaded performance to the right area.

Next, browser vendors need to work together to standardize stuff that will allow SIMD and more threading. This is definitely possible if everyone is interested in making the web faster, which I certainly believe is the case.

Re:Mozilla can produce near-native performance on the Web by anon1385 on Thursday May 23, 2013 @05:38AM (#5755962)
>The important thing is to realize that until just recently - a few months or so - people did not believe that SINGLE-threaded JS code on the web could be close to native speed.

Single threaded Javascript was already 'fast enough' for the CRUD interfaces that makes up most web apps. Web apps are horrible to use because the network is unreliable for many people, latency is terrible for just about everybody, browsers provide a very poor and limited set of APIs for many of the tasks people actually want to do (interacting with hardware, file systems, audio, inter-app interoperability etc) and apps are forced to run inside a sandbox that can't be escaped. Cloud services nearly always mean that users lose control of their data. Web apps are rarely open source (and if they are you still can't change the version of the app that you actually use because it runs on a server you don't control). A faster Facebook with 3D effects is still a terrible platform.

Homer Simpson: Kids: there's three ways to do things; the right way, the wrong way and the Max Power way!

Bart: Isn't that the wrong way?

Homer Simpson: Yeah, but faster!

I think a lot of this obsession over Javascript speed is a distraction (support for more languages is nice for developers of course). Making slightly faster VMs is just an engineering problem: throw more resources at it and things will improve. The hard problems are political and also related to the basic network infrastructure (constrained by the laws of physics). Trying to recreate the entire operating system inside the browser, for all possible uses (even performance sensitive things), is a huge overreach when we don't seem to have a clue how to make the basic web app experience good. Why is it hard? Because the founding idea of a single sandboxed standard for all platforms is unworkable in the real world. Not only does it mean all developers surrendering complete control to the people defining the standard (who also happen to be big players in many other related markets), it relies on the big players actually agreeing, which is often not in their interest. If the world had moved to Gopher based OSs in the early 90s the web as we know it would not have come into existence. What future possible technologies are we destroying by locking ourselves into the web sandbox?

Re:Mozilla can produce near-native performance on the Web by hughw on Thursday May 23, 2013 @01:12PM (#5757993)
Your premise is demonstrably wrong

> Why is it hard? Because the founding idea of a single sandboxed standard for all platforms is unworkable in the real world. Not only does it mean all developers surrendering complete control to the people defining the standard (who also happen to be big players in many other related markets), it relies on the big players actually agreeing, which is often not in their interest.

The innovation in browsers and webapps is coming from the vendors each acting individually, and puling standards bodies along with them.

If you as a developer, are waiting around for them all to agree on any technology, before leveraging it, then you are just paralyzed. Develop against WebGL and asm.js now to get early mover advantages. If you wait for universal uptake, you'll never get anything done.

Re:Mozilla can produce near-native performance on the Web by anon1385 on Friday May 24, 2013 @08:36AM (#5762319)
Demonstrably wrong? Remind us how many years it took to get rounded corners into CSS?

>The innovation in browsers and webapps is coming from the vendors

Yes, which does not contradict what I said at all: "the people defining the standard (who also happen to be big players in many other related markets)". The vendors are big industry players who have their own agendas that rarely coincide with my interests (yes even Mozilla). They also get to veto any proposal they don't like.

The Kinect has been available for years now, which is an age in the consumer tech space. Where is the open standard web API? Do you see how it might be difficult to get Mozilla, Google and Apple to agree to standardise, implement and support an API for proprietary Microsoft technology? Creating a specific API for every type of device (photo cameras, video cameras, accelerometer, touch screens etc) and not providing a low level interface to hardware is the wrong route to take. High level APIs are fine, but it is important to also provide the ability for people to extend platform support in new and unforeseeable ways, and that requires lower level access.

WebRTC is another excellent example of conflicts of interest with Microsoft opposed to it because it challenges Skype (or because it is technically deficient, depending upon which side you are on). The difference between the web as a platform and real open operating systems is stark. If Windows had been like the web -- locked down with only a limited set of approved APIs -- then Skype could not have been created unless Microsoft decided to allow it. Substitute Microsoft for Apple/Google/Mozilla/Microsoft and you have the situation we have with the web.

Perhaps some people don't see the this because of the tribal nature of technology discussions. People don't see the web as being restrictive because their 'team' (be that Apple or Google or Microsoft or Mozilla) has a say in it. So most arguments devolve into discussion about how X is blocking a proposal by Y and the proposal is either good for the web or bad for the web depending on whether you favour X or Y. Few people seem to stop and wonder why we are creating a system where X can veto technologies, even for people who don't use a single product created by X.

>If you as a developer, are waiting around for them all to agree on any technology, before leveraging it, then you are just paralyzed. Develop against WebGL and asm.js now to get early mover advantages.

How does developing against asm.js and WebGL help me interface with new input devices? It doesn't help me one bit. All it does is let me create a faster shinier version of current webapps (that only runs in certain browsers). This is a reoccurring argument though: the web will be a decent platform once we finally get $NEW_API_PROPOSAL that fill fix everything 'once and for all'.

Re:Mozilla can produce near-native performance on the Web by npalli on Thursday May 23, 2013 @12:02AM (#5755288)
Ok thanks. I guess I'm confused about the gains with asm.js. Looks like it is in the same ballpark performance wise as regular JS (and slower in some cases). So, why would anyone write in a crippled C-like subset of javascript and get normal javascript like performance. In the future if threading and SIMD is standardized, won't regular JS gain performance as well?
Re:Mozilla can produce near-native performance on the Web by azakai on Thursday May 23, 2013 @12:05AM (#5755294)
No, look again at the numbers - asm.js gets much closer to native speed than normal JS, in many cases, especially large apps. In addition to the numbers in the article, you can see for example,

http://kripken.github.io/mloc_emscripten_talk/#/28

asm.js code makes it easier to get within reach of native performance.

Re:Mozilla can produce near-native performance on the Web by bad_user on Thursday May 23, 2013 @03:35AM (#5755720)
> why would anyone write in a crippled C-like subset of javascript

Because asm.js is not meant for people, but for compilers.

For example the Unreal Engine has been recently compiled from C/C++ to asm.js and is running in the browser: http://www.unrealengine.com/html5/

Re:Mozilla can produce near-native performance on the Web by camus on Thursday May 23, 2013 @09:32AM (#5756709)
... best viewed in ... netscape ?
Re:Mozilla can produce near-native performance on the Web by cristiantincu on Thursday May 23, 2013 @01:54PM (#5758286)
“Best viewed in Netscape” still wins over “Download for Windows (x86)”.
Re:Mozilla can produce near-native performance on the Web by riffraff on Thursday May 23, 2013 @03:50AM (#5755750)
you are not expected to write asm.js, it's to be considered a compiler target (for e.g. emscripten etc)
Re:Mozilla can produce near-native performance on the Web by haberman on Thursday May 23, 2013 @01:08AM (#5755450)
> multithreading and SIMD capability disabled

[EDIT: it looks like I may have misinterpreted the article; see replies]

To clarify, I believe they ran the benchmarks with SSE disabled. SSE is more than just SIMD; these days SSE is used for most (all?) floating-point calculations, even non-SIMD. If you disable SSE, you force floating-point ops to use the old x87 FPU, which has been discouraged by the CPU manufacturers for over 10 years.

In other words, disabling SSE is significantly crippling all floating-point performance.

http://www.realworldtech.com/physx87/4/

http://stackoverflow.com/questions/3206101/extended-80-bit-d...

(nothing against asm.js, which I think is very cool)

Re:Mozilla can produce near-native performance on the Web by azakai on Thursday May 23, 2013 @01:41AM (#5755519)
Why would you assume they disabled SSE? Compilers generate SSE by default when optimizing, I would be very surprised if Peter Bright disabled it. And not only do normal C/C++ compilers generate SSE, but JS engines generate SSE as well. So SSE was being used on both sides of the comparisons here, I am pretty sure.

What was disabled was C/C++ code that explicitly used SIMD intrinsics or explicitly used SSE function calls in C/C++.

Re:Mozilla can produce near-native performance on the Web by haberman on Thursday May 23, 2013 @01:50AM (#5755535)
I must have misinterpreted this remark:

> In general, I took the highest-ranked pure C++ routines for each test. Versions that used, for example, SSE functions were excluded, as were those with explicit multithreading.

For "functions" I interpreted "functionality," but I think now it must mean SSE intrinsics?

Sorry for the confusion.

Re:Mozilla can produce near-native performance on the Web by DrPizza on Thursday May 23, 2013 @02:43AM (#5755633)
Yes, sorry, I meant SSE intrinsics. As I think I note later in the article, the compiler I used emitted scalar SSE2 for floating point.
Re:Mozilla can produce near-native performance on the Web by Gravityloss on Thursday May 23, 2013 @02:16AM (#5755586)
I'm glad you made the comment, others might read it the same way as you did originally. This clears the potential misunderstanding.
Re:Mozilla can produce near-native performance on the Web by sillysaurus on Wednesday May 22, 2013 @11:34PM (#5755201)
anyone writing performance sensitive apps will use multithreading

That assumes the performance sensitive app is amenable to multithreading. Many aren't.

Re:Mozilla can produce near-native performance on the Web by vilya on Thursday May 23, 2013 @06:10AM (#5756035)
Your comment has made me curious. I tried to think of some examples of performance sensitive apps that aren't amenable to multi-threading and I couldn't, but it seems like they could offer interesting areas for R&D. Can you share some examples?
Re:Mozilla can produce near-native performance on the Web by Yoric on Thursday May 23, 2013 @06:30AM (#5756098)
Consider a single tab of a web browser, for instance. By specification, much of the code must run in the same thread. Keeping the semantics while getting code out of the thread is extremely difficult, often impossible.

I should know, that's my dayjob :)

Re:Mozilla can produce near-native performance on the Web by vilya on Thursday May 23, 2013 @12:17PM (#5757665)
So the reason you can't make it faster than JavaScript by multi-threading it is... because of JavaScript? :-)

I had a look at your profile and it looks like you're doing some very valuable work. I was rather hoping for some examples which didn't involve JavaScript, though.

Re:Mozilla can produce near-native performance on the Web by saucetenuto on Thursday May 23, 2013 @02:14PM (#5758442)
It used to be that almost all PC games were single threaded. Many still are: Civilization 4, Dwarf Fortress, and Morrowind, for example.
Re:Mozilla can produce near-native performance on the Web by fulafel on Friday May 24, 2013 @06:30AM (#5762048)
Many things are hard to parallelize using current technology, and there indeed has been lots of research done on it. History goes back to the supercomputer era (Cray, Connection Machine, Transputer etc.)

The term of art is "inherently sequential" problems (vs "embarrassingly parallel").

In addition, the programming effort gets untenable quickly due to Amdahl's law when the amount of parallelism increases. Eg. if you have 100 cores, even if only 1% of work in your program is sequential, your performance goes down the drain. Currently we're in the comfy phase of the curve with few cores...

We've been making good progress with parallel-minded reimaginings of programming languages and computer architectures on one front: GPUs. On the CPU we are saddled with the inertia and backwards compatibiltiy lock-in of tools and culture (witness lasting use of C++ as a tool to write parallel software)

Re:Mozilla can produce near-native performance on the Web by gcr on Thursday May 23, 2013 @09:51AM (#5756788)
If you have a multicore machine -- the only kind of machine that brings a benefit to multithreading -- and if your problem is embarassingly parallel, you can usually get away with multiprocesses (MapReduce etc). That way, your single-threaded process can run on many cores.
Re:Mozilla can produce near-native performance on the Web by crabasa on Wednesday May 22, 2013 @11:30PM (#5755192)
Is it fair to describe JS as single-threaded when all modern browsers support web workers?

Related: Why does it seem like web workers don't exist? I can hardly think of any popular libraries or web apps that make use of them, despite the seemingly obvious utility.

Re:Mozilla can produce near-native performance on the Web by Yoric on Thursday May 23, 2013 @06:37AM (#5756110)
Let me detail tachyonbeam's answer.

All modern web browsers (I'll try not to troll about IE) support web workers. However, the semantics of web workers is message-passing (by copy, most of the time), which is the semantics generally associated to multi-process rather than multi-threading. Most developers using threads expect some kind of memory sharing, which is impossible with web workers at the moment.

Shared workers (not implemented in any browser atm, I believe) will take care of some usage scenarios. Other alternatives are being explored (e.g. [Parallel JS](http://smallcultfollowing.com/babysteps/blog/2012/01/09/para...). However, for the time being, don't expect any support from JS to do shared memory concurrency.

Note: I'm a Firefox dev.

Re:Mozilla can produce near-native performance on the Web by kybernetikos on Thursday May 23, 2013 @04:11AM (#5755790)
After getting very excited and doing a bunch of work, we did a lot of performance tests and discovered that for our use case all they did was increase processor load for the same work - the penalty associated with serialising and deserialising things to go across postMessage was greater than the work we were getting done on the other thread.

Of course this was in the early days, and it'd be good to retest it to see if it's better now.

I think that there are a lot of interesting use cases for WebWorkers, but you also have to be aware that for some things they will actually slow down your application.

Re:Mozilla can produce near-native performance on the Web by i_s on Thursday May 23, 2013 @11:54AM (#5757532)
It is still not good. I've started working on an asteroids game [0], and I tried moving my physics calculations to a background thread, but it ended up being slower.

0 http://www.isaksky.com/asteroids/

Re:Mozilla can produce near-native performance on the Web by Yoric on Thursday May 23, 2013 @06:38AM (#5756112)
Working on it :)
Re:Mozilla can produce near-native performance on the Web by cwmma on Thursday May 23, 2013 @07:09AM (#5756190)
1. It's hard to use them in libraries due to restrictions on how to load them, Opera, Ie 10 and safari 5 require a same origin file, makes it hard to drop a worker into a slow part of jQuery. 2. Message passing can be slow and transferring only works for typed arrays which take time to convert to. Unless your calculations are much bigger then your data or converting to typed arrays isn't a bottleneck it may slow you down. 3. OO based inheritance patterns make it very hard to put pieces of your code into the worker, if your slow thing involved a call to jQuery. Ajax you can't call jQuery from the worker because it references the DOM but you can't pull out the Ajax method because it relies on $. extend and others meaning you have to rewrite $. Ajax from scratch (mot too bad in this case) or do major surgery on the library.
Re:Mozilla can produce near-native performance on the Web by tachyonbeam on Wednesday May 22, 2013 @11:33PM (#5755198)
Web workers are more like multiple processes than multiple threads, but yes, there are ways to use more than one CPU core in JS too.
Re:Mozilla can produce near-native performance on the Web by danmaz74 on Thursday May 23, 2013 @01:34AM (#5755507)
Our hashtagify.me uses them :)
Re:Mozilla can produce near-native performance on the Web by VoxPelli on Thursday May 23, 2013 @03:22AM (#5755696)
There is work going on in enabling Javascript to get better at doing things in parallel - both on CPU and GPU.

Eg. check out the ParallelArray() from the River Trail project by Intel: http://wiki.ecmascript.org/doku.php?id=strawman:data_paralle... & http://en.m.wikipedia.org/wiki/River_Trail_(JavaScript_engin... At least Firefox has an initial implementation: https://developer.mozilla.org/en-US/docs/JavaScript/Referenc...

The benefit if asm.js being based on ordinary javascript is that they can use these improvements to javascript itself right away more or less.

Re:Mozilla can produce near-native performance on the Web by fulafel on Thursday May 23, 2013 @08:25AM (#5756412)
> anyone writing performance sensitive apps will use MT right?

No, multithreading not that simple - usually it's the last trick you should try to pull and even then only if the stars align just right.

You need to have expert programmers on hand (of the rare breed who can pull this off), the good luck to succeed and the time budget to spend on the big code reachitecting and experimentation.

If you're running on something else than modern consoles, you don't know how many cores your users will have, the average might be 2 so your average returns on the effort will suck (vs spending equivalent effort elsewhere).

Witness modern Web browsers, for example, where Google, Mozilla, Apple and Microsoft employ some of the best C++ programmers in the world, routinely of pulling off heroic feats, and they haven't seen it worth the effort yet despite Core 2 and Athlon X2 hitting mainstream desktop at about 2005-2006, 8 years ago.

Re:Mozilla can produce near-native performance on the Web by w0utert on Thursday May 23, 2013 @09:09AM (#5756598)
I think you are overstating the problems writing multithreaded code a little here. Yes, it's a lot harder than single-threaded code, yes you need to know what you are doing, yes you need to carefully design your code to enable efficient and safe multithreading, and no you don't get speed for free. All true, but you are making it sound as if writing multithreaded code is rocket science, some kind of mad skill only the top 0.5% programmers can handle. It's not that bad you know... If you're a half-way decent programmer and don't litter your single-threaded code with globals, shared memory and functions with side-effects, like you shouldn't anyway, parallelizing it after the fact to at least is usually pretty straightforward even. Maybe not optimal, but safe, and faster than single-core on multi-core hardware.
Re:Mozilla can produce near-native performance on the Web by fulafel on Thursday May 23, 2013 @09:14AM (#5756615)
Sure, you can write the code if you're a half-way decent programmer. But getting it to work correctly and bug free in presence of nondeterministic behaviour and races is in the domain of the very small minority, when you're talking about large code bases.

I'm betting the minority is less than 0.5%. Half way decent C++ programmers aren't many % of programmers, and people who can make a reliably working parallelized do-over for a nontrivial code base are maybe 1% of that. I'll say 0.05% max.

Re:Mozilla can produce near-native performance on the Web by w0utert on Thursday May 23, 2013 @11:12AM (#5757241)
The challenge is to design your code in a way that either makes it impossible to end up with race conditions or non-deterministic behavior, or if at least limit the number of possible points-of-failure. This requires a good understanding in what coding styles and data structures to avoid or pay extra attention to, but if you're aware of the pitfalls, it's actually not all that difficult. Just make sure your workers don't share any state, collect their outputs in a single thread-safe queue, use off the shelf components to represent threads, thread pools and locking mechanisms, be aware of things like multiple-read-single-write locks, and if you really can't avoid writes to shared state, only then start thinking about mutexes or semaphores. If you're smart about how you set up the code, more often than not you can get away with just some thread-safe queues. Take a look at Objective-C's block syntax and GCD for example, it makes maybe 90% of tasks suitable for multithreading dead easy.

Your estimate only 0.05% of programmers could write reliable multithreaded code is extremely pessimistic, cynical even. I almost feel flattered, having written a fair amount of multithreaded code that works perfectly myself, but I most definitely would dare count me with the 0.05% top programmers or whatever. My estimate is that with proper preparation, at least 50% of all programmers could learn to write reliable multithreaded code, and the remaining 50% probably wouldn't have a use for it anyway.

Re:Mozilla can produce near-native performance on the Web by fulafel on Friday May 24, 2013 @06:05AM (#5762007)
I was talking about the percentage of existing programmers with the abilities to pull it off. I'm sure more than 0.05% have the potential to learn it eventually, given enough time, motivation and opportunity. Like musicians & theremin!
Re:Mozilla can produce near-native performance on the Web by nnnnnnnninnnnnn on Thursday May 23, 2013 @09:02AM (#5756567)
You're making writing multi-threaded code sound like a herculean task. Sure, doing MT with mutexes is a pain, but programming with actors (message passing) is simpler. You can easily scale this for variable number of cores.
Re:Mozilla can produce near-native performance on the Web by Marat_Dukhan on Thursday May 23, 2013 @12:48PM (#5757855)
It really should be a 100x slowdown! Their "optimized" Linpack numbers (41 GFLOPs) are too low for this system. The CPU they use can deliver 108 GFLOPs at base frequency and 120 GFLOPs at max turbo frequency, and good LU implementations (e.g. MKL) can achieve >90% of these peak numbers.
Re:Mozilla can produce near-native performance on the Web by Spearchucker on Thursday May 23, 2013 @05:25AM (#5755934)
It's good progress all the same, but doesn't do much for the end user that still relies on an often shitty Internet connection.