Slashdot Mirror


Asm.js Gets Faster

mikejuk writes "Asm.js is a subset of standard JavaScript that is simple enough for JavaScript engines to optimize. Now Mozilla claims that with some new improvements it is at worst only 1.5 times slower than native code. How and why? The problem with JavaScript as an assembly language is that it doesn't support the range of datatypes that are needed for optimization. This is good for human programmers because they can simply use a numeric variable and not worry about the difference between int, int32, float, float32 or float64. JavaScript always uses float64 and this provides maximum precision, but not always maximum efficiency. The big single improvement that Mozilla has made to its SpiderMonkey engine is to add a float32 numeric type to asm.js. This allows the translation of float32 arithmetic in a C/C++ program directly into float32 arithmetic in asm.js. This is also backed up by an earlier float32 optimization introduced into Firefox that benefits JavaScript more generally. Benchmarks show that firefox f32 i.e. with the float32 type is still nearly always slower than native code, it is now approaching the typical speed range of native code. Mozilla thinks this isn't the last speed improvement they can squeeze from JavaScript. So who needs native code now?"

35 of 289 comments (clear)

  1. "So who needs native code now?" by Anonymous Coward · · Score: 5, Informative

    Umm, anyone who wants their code to not run substantially slower. Seriously, do you front end programmers really think nobody does numerical simulations or other performance-sensitive work? In my line of work, I'd kill for the opportunity to make my code 1.5 times faster!

    1. Re:"So who needs native code now?" by gagol · · Score: 2

      Also, the major down point of JavaScript I found to hinder performance in ambitious projects, is memory management. In the cases where I need gigantic arrays, the performance slows down to a crawl as data grows. Anyone knowledgeable can bring some light about this aspect of asm.js?

      --
      Tomorrow is another day...
    2. Re:"So who needs native code now?" by Anonymous Coward · · Score: 3, Funny

      You mean I can't write that OS in JavaScript for my CS degree?

    3. Re:"So who needs native code now?" by MightyMartian · · Score: 4, Insightful

      What a bizarre statement. Of course it's programming. It may not be very elegant programming, but then again, the bulk of C code I've seen in my years in the business isn't terribly elegant either.

      --
      The world's burning. Moped Jesus spotted on I50. Details at 11.
    4. Re:"So who needs native code now?" by phantomfive · · Score: 2
      --
      "First they came for the slanderers and i said nothing."
    5. Re:"So who needs native code now?" by phantomfive · · Score: 5, Insightful

      Correct this to any real programmer. I'm tired of web designers being colluded with actual programmers, scripting isn't programming.

      Heh....typing isn't programming. If you aren't connecting patch wires on an accumulator bank, it's not worth doing. You get more efficiency that way too!

      --
      "First they came for the slanderers and i said nothing."
    6. Re:"So who needs native code now?" by Anonymous Coward · · Score: 2, Informative

      Tracing garbage collected languages will always be slower because:

      1) The tracing adds a ridiculous amount of unnecessary work; and

      2) While allocation is at best O(N) for GCd and regular programs, deallocation can (and often is) made O(1) using memory pools in C and C++ programs, something that can't be done in GCd languages because the collector doesn't understand semantic interdependencies.

      For ref-counted collectors #2 still applies.

      Unless and until some unforeseen, miraculous breakthrough happens in language design, GCd languages will always be slower when it comes to memory management. And because memory management is so critical for complex applications, GCd languages will effectively always be slower, period.

      But the only people who care that GCd languages are slower are people who only know GCd languages. I extensively code in C and Lua, and I love both.

    7. Re:"So who needs native code now?" by dshk · · Score: 4, Informative

      deallocation can (and often is) made O(1) using memory pools in C and C++ programs, something that can't be done in GCd languages

      I believe current Java (not Javascript!) virtual machines do exactly this. They do escape analysis, and free a complete block of objects in a single step. This works out of the box, there is no need for memory pools or any other special constructs.

    8. Re:"So who needs native code now?" by Flammon · · Score: 2

      Some of the most elegant code that I've seen has been with web scripting languages.

      $('img').bind('mouseenter mouseleave', function() {
              $(this).attr({
                      src: $(this).attr('data-other-src')
                      , 'data-other-src': $(this).attr('src')
              })
      });

      Shameless plug

      Try doing that in C

    9. Re:"So who needs native code now?" by beelsebob · · Score: 2

      What makes you think this can't be done in C (at least C with apple's block extension)?

      99% of the "elegance" of this is the particular framework in use, not the language.

    10. Re:"So who needs native code now?" by Anonymous Coward · · Score: 2, Insightful

      Try doing WHAT in C? The idiot who wrote that code doesn't know how to comment!

    11. Re:"So who needs native code now?" by darkHanzz · · Score: 2, Insightful

      In C++ it'd come down to a lambda function and std::swap, I don't see how that's less 'elegant'.

    12. Re:"So who needs native code now?" by Carewolf · · Score: 3, Interesting

      Some of the most elegant code that I've seen has been with web scripting languages. $('img').bind('mouseenter mouseleave', function() { $(this).attr({ src: $(this).attr('data-other-src') , 'data-other-src': $(this).attr('src') }) }); Shameless plug

      Try doing that in C

      In modern C++:

      img.mouseenter = img.mouseleave = [] () { std::swap(img.attributes["src"], img.attributes["data-other-src"]); };

      Ofcourse it requires the C++ programmer hasn't been too damaged by exposure to Java and tries to pointlessly ruin the language by using getters and setters instead of real property access.

  2. Javascript as a Virtual Machine Representation by ndykman · · Score: 5, Interesting

    The idea of compiling to JavaScript has been done a lot. Microsoft Labs had a project to take CLR codes and compile down to JavaScript. It was abandoned as too slow. I'm sure improvements to the JavaScript engines have made it more feasible. But, as noted the lack of certain native types and immutable data types (i.e. DateTime) forces a ton of static analysis to be done just to figure out that hey, that variable could be an plain integer. And it has to be conservative. Much easier to just have integers and be done with it.

    If there is such an insistence on making the web an application platform for everything, then I think at some point you just have to standardize on a VM. Yes, yet another one. So, you can use Dart, JavaScript, Scheme, C#, Java, whatever there's a compiler for. Treat the DOM as core API and enjoy.

    Personally, I just hope people realize that operating systems have been doing this well for years, that sandboxing isn't unique to web browsers and that "native" applications are actually a good thing.

    The mobile app thing gives me a bit of hope, but it's sad that people seem unwilling to download a installer from the web from a trusted source and install it. I find it a bit strange that people are turning to the web to solve a problem that the web greatly expanded the widespread propagation of viruses and other malware.

    And what people are surprised by is a bit baffling as well. A web browser isn't magic. Being impressed that you can run Doom on a modern web browser is missing the forest for a tree. I could run Doom on my computer for ages now. Me having to visit a URL to do so isn't not a major actual change nor improvement, despite the technical accomplishments that went into it.

  3. So who needs native code now? by sribe · · Score: 3, Interesting

    Anybody who writes performance-sensitive code other than trivial contrived benchmarks.

  4. Re:Update the ecma standard by sribe · · Score: 2

    Update the ecma standard to include a set of features that is meant to be used when compiling a better language (C#, C++, Java, Perl, Python, LISP, Haskell, x86 asm, MUMPS, pig-latin, etc) down to javascript so it will run in any browser. Then I think everyone will be happy.

    Fixed that for you ;-)

  5. Re:Suspect even at -O0 -g by gbjbaanb · · Score: 2

    asm.js is not javascript though, its a specific subset with restricted coding rules that allow the compiler to do its stuff. General js will be just as slow as any of the other script languages.

  6. 64-bit computation vs. 64-bit storage by K.+S.+Kyosuke · · Score: 3, Interesting

    JavaScript always uses float64 and this provides maximum precision, but not always maximum efficiency.

    That doesn't make too much sense to me, I thought that typed arrays have always had 32-bit floats as an option. Why should 32-bit storage (on-heap typed arrays) and 64-bit registers (scalar values) be significantly slower than 32-bit storage and 32-bit registers? I thought the performance discrepancy between singles and doubles in CPU float units disappeared a decade and a half ago? (Or are simply single-to-double loads and double-to-single stores somehow significantly slower?)

    --
    Ezekiel 23:20
    1. Re:64-bit computation vs. 64-bit storage by Pinhedd · · Score: 4, Informative

      Take a look at the image at the following link

      http://www.anandtech.com/show/6355/intels-haswell-architecture/8

      That's the backend of the Haswell microarchitecture. Note that 4 of the 8 execution ports have integer ALUs on them, allowing for up to 4 scalar integer operations to begin execution every cycle (including multiplication). Two of these are on the same port as vector integer unit, which can be exploited for an obnoxious amount of integer math to be performed at once. There are only two scalar FP units, one for multiplication on port-0 and one for addition on port-1.

      The same FP hardware is used to perform scalar, vector, and fused FP operations, but taking advantage of this requires a compiler that is smart enough to exploit those instructions in the presence of a Haswell microprocessor only and fast enough to do it quickly. Exploiting multiple identical execution units in a dynamically scheduled machine requires no extra effort on behalf of the compiler.

      Microprocessors used in PCs have always been very integer heavy for practical reasons (they're just not needed for most applications), and mobile devices are even more integer heavy for power consumption reasons.

      Using FP64 for all data types is obnoxiously lazy and it makes me want to strangle front end developers.

  7. Re:Suspect even at -O0 -g by K.+S.+Kyosuke · · Score: 2

    If LuaJIT 2.x was *ten times* slower than C for you, perhaps you were doing something wrong?

    --
    Ezekiel 23:20
  8. Re:So who needs native code now? by Anonymous Coward · · Score: 2, Funny

    The person writing the javascript engine.

    Unless it's implemented in asm.js

    It's JavaScript engines all the way down, man...

  9. Re:So who needs native code now? by Billly+Gates · · Score: 2

    Actually this has nothing to do with javascript.

    ASM.js is a java VM like technology which compilies code in C++ and Java to javascript which then uses a JIT compiler.

    This could be very usefull for things like Citrix or crappy activeX like functions being made available to mobile devices and browsers. Useful yes, but like java and activeX a big fucking security hole if not done right.

    I think ASM.JS is dead out of the water as Google refuses to support it. Google wants you to use Dart and Google Chrome apps that run near native speed and require a Google ecosystem. MS wont support it either as it gets rid of the dependancy for Windows and crappy Metro apps.

    I think Google is turning more into the IE of the late 1990s as it is becoming so standard now.

  10. Re:Numerical Calculations dont use Java by _merlin · · Score: 2

    Not mainframes, more clusters. All the stuff used for reconciliation and position management uses pure integer and fixed point maths. Market makers like pushing floating point numbers around, but that's just for quick profit calculations when doing the trades. At the end of the day, everything's reconciled with precise maths.

  11. Re:So who needs native code now? by gbjbaanb · · Score: 2

    Emscriptem is a technology that compiles code in C++ to javascript. Asm.js is a restricted subset of javascript that is much easier for the compiler to handle.

    Google wants you to use quite a few different things, probably the 'best' is NaCl (native client) which runs practically native code in a sandboxed runtime.

  12. Or anything running in a VM by Sycraft-fu · · Score: 5, Informative

    I get pissed when you hear programmers say "Oh memory is cheap, we don't need to optimize!" Yes you do. In the server world these days we don't run things on physical hardware usually, we run it in a VM. The less resources a given VM uses, the more VMs we can pack on a system. So if you have some crap code that gobbles up tons of memory that is memory that can't go to other things.

    It is seriously like some programmers can't think out of the confines of their own system/setup. They have 16GB of RAM on their desktop so they write some sprawling mess that uses 4GB. They don't think this is an issue after all "16GB was super cheap!" Heck, they'll look at a server and see 256GB in it and say "Why are you worried!" I'm worried because your code doesn't get its own 256GB server, it gets to share that with 100, 200, or even more other things. I want to pack in services as efficient as possible.

    The less CPU, memory, disk, etc a given program uses, the more a system can do. Conversely, the less powerful a system needs to be. In terms of a single user system, like maybe an end user computer, well it would always be nice is we could make them less powerful because that means less power hungry. If we could make everything run 1.5 times as fast, what that would really mean is we could cut CPU power by that amount and not affect the user experience. That means longer battery life, less heat, less waste, smaller devices, etc, etc.

    1. Re:Or anything running in a VM by NormalVisual · · Score: 2

      I get pissed when you hear programmers say "Oh memory is cheap, we don't need to optimize!"

      Preach on, brother. I'd love to see how some of these guys would function in the embedded world, where you often get 1K of flash and 128 bytes of RAM to work with.

      --
      Please stand clear of the doors, por favor mantenganse alejado de las puertas
    2. Re:Or anything running in a VM by doublebackslash · · Score: 2, Informative

      Just fine, at least I do. Just different sets of optimizations to keep in mind, as well as different expectations. I don't think any reasonable person would approach the two problems the same way, but it all boils down to basic computer science.

      Light up pin 1 when the ADC says voltage is dropping which indicates that pressure is too low on the other side of the PPC. Compare that to indexing a few gigs of text into a search engine. Completely different goals, completely different expectations. I'm not master of the embedded domain, but I don't think it is a dark art.

      Perhaps I'm looking at it the wrong way or perhaps my experience is unique or at least rare, but in my eyes it is all the same thing at different scales. Tell me my app is using too much memory then I'll first look at how I can reduce memory pressure, then I'll tell you what is and isn't possible to do and give you a list of sacrifices that would be needed to reduce memory pressure (time to refactor, concurrent operations, latency because of disk, etc etc etc. Not just talking about capabilities but the whole deal). Find the balance and go for it. On the embedded side the same sorts of compromises are made but the scale is just so much smaller. Finite number of IO pins, time to optimize your code to accommodate a new feature, meeting real-time, writing something in ASM to get around a GOD FREAKING AWFUL EMBEDDED COMPILER, etc etc etc.

      I dunno, do I have my head on straight here? All seems fairly straightforward in the end. Specialists can do their bits faster than someone less familiar but with equal skill and understanding. Thats what earns the bucks, getting things done in a timely fassion.

      ((Or at heart I'm an embedded guy. Possible!))

      --
      md5sum /boot/vmlinuz
      d41d8cd98f00b204e9800998ecf8427e /boot/vmlinuz
    3. Re:Or anything running in a VM by bberens · · Score: 3, Insightful

      That depends a lot on what you're doing. It costs about $125/hr for me to optimize my code. Every 8 hours spent optimizing is $1k down the drain and you can buy an entry level server for that price. If it's running on one or two VMs it's probably not worth my company's time to optimize it vs buying more hardware. Conversely if I'm Google a 1% optimization could mean literally thousands of servers. I mean, don't get me wrong. I pay attention to memory allocation and CPU optimization as I'm coding, but only to the level of not making unnecessarily egregious use of resources. Pretty much anything beyond that is a waste for most of the projects I work on.

      --
      Check out my lame java blog at www.javachopshop.com
    4. Re:Or anything running in a VM by zippthorne · · Score: 5, Insightful

      Another question is why we need to duplicate an entire operating system to encapsulate applications. If you have 100 things that need to run on a machine why should you need to also run 100 entire operating systems? Something is wrong with the way we're designing servers.

      --
      Can you be Even More Awesome?!
    5. Re:Or anything running in a VM by Pinhedd · · Score: 3, Informative

      Ideal virtual machines are indistinguishable from networked servers. Most x86 VMMs don't quite reach this level of isolation, but the VMMs used on IBM's PowerPC based servers and mainframes do.

      From the perspective of system security, a single compromised application risks exposing to an attacker data used by other applications which would normally be outside of the scope of the compromised application. Most of these issues can be addressed through some simple best practices such as proper use of chroot and user restrictions, but those do not scale well and do not address usability concerns. A good example is the shared hosting that grew dominant in the early 2000s while x86 virtualization was still in its infancy. It was common to see web servers with dozens if not hundreds of amateur websites running on them at once. For performance reasons a web server would have read access to all of the web data; a vulnerability in one website allowing arbitrary script execution would allow an attacker to read data belonging to other websites on the same server.

      From the perspective of users, a system designed to run 100 applications from 20 different working groups does not provide a lot of room for rapid reconfiguration. Shared resource conflicts, version conflicts, permissions, mounts, network access, etc... it gets extremely messy extremely quickly. Addressing this requires a lot of administrative overhead and every additional person that is given root privileges is an additional person that can bring the entire system down.

      Virtual machines on the other hand give every user their own playground, including full administrative privileges, in which they can screw around with to their hearts content without the possibility of screwing up anything else or compromising anything that is not a part of their environment. Everyone gets to be a god in their own little sandbox.

      Now, that doesn't mean that the entire operating system needs to be duplicated for every single application. Certain elements such as the kernel and drivers can be factored out and applied to all environments. Solaris provides OS level virtualization in which a single kernel can manage multiple fully independent "zones" for a great deal of reduced overhead. Linux Containers is a very similar approach that has garnered some recent attention.

  13. Re:Suspect even at -O0 -g by Anonymous Coward · · Score: 2, Informative

    Asm.js is not JavaScript. It's Mozilla's way of hacking together a very specific optimization for JS-targeting compilers such as Emscripten, because they don't want to adopt the sane route of just implementing PPAPI and Google's Native Client sandbox, both of which don't work well with single-process browsers. From a developer perspective it's fairly trivial to target both Asm.js and PNaCl (Google's Native Client except with LLVM bytecodes), or target one and write a polyfill for the other. In either case, both of these environments are for executing C/C++ native code in the browser with minimal slowdown, they don't touch run of the mill JS anyway.

  14. Not really true by SoftwareArtist · · Score: 2

    Let's correct that sentence:

    "Now Mozilla claims that with some new improvements it is at worst only 1.5 times slower than single threaded, non-vectorized native code."

    In other words, it's only 1.5 times slower than native code that you haven't made any serious effort to optimize. Hey, I think it's great they're improving the performance of Javascript. But this is still nowhere close to what you can do in native code when you actually care about performance.

    --
    "I'm too busy to research this and form an educated opinion, but I do have time to tell everyone my uninformed opinion."
  15. Re:don't we know it by Concerned+Onlooker · · Score: 3, Informative

    Websites are no less than distributed applications. If you had been paying attention you would have noticed that website development has gotten a lot more rigorous than in the old days.

    --
    http://www.rootstrikers.org/
  16. Maximum precision? by raddan · · Score: 4, Informative

    Let's just open up my handy Javascript console in Chrome...

    (0.1 + 0.2) == 0.3
    false

    It doesn't matter how many bits you use in floating point. It is always an approximation. And in base-2 floating point, the above will never be true.

    If they're saying that JavaScript is within 1.5x of native code, they're cherry-picking the results. There's a reason why people who care have a rich set of numeric datatypes.

  17. Everything Must Be a Web Site by Sanians · · Score: 2

    The "everything must be a web site" mentality has confused the hell out of me for ages as well.

    For some time I assumed it was probably for portability, but while writing a game I'm sure no one cares about I found that portability actually isn't all that difficult. The number of #ifdef involved to make it compile for both Windows and Linux (and FreeBSD, though I haven't compiled for it in about a year) amount to a very minor part of the code, and probably wouldn't have been even that much if I were more of a fan of using other people's libraries. (It does use GLFW, which no doubt is taking care of at least 95% of the portability issues for me.) I suspect even a Mac OS version wouldn't be too difficult though I never could figure out how to produce one from Linux. Granted, I don't have a reliable Linux version available due to LGPL issues, but no one cares about Linux anyway. They could certainly achieve portability between the platforms they care about (anything Windows runs on, and Mac OS) easily enough with a compiled C program, and if they really cared they could also set up a server farm of Linux machines so that they have a copy of each distribution available to compile a Linux version too.

    So then why is it I go to YouTube and deal with shitty web video that barely plays correctly if it does at all? I've seen the red and blue channels reversed in video, I've seen 30-second seek times (even seeking backwards), and at times it simply doesn't work at all. The same goes for hulu.com or any online radio web site. Even Facebook could benefit from having a custom application. ...and all of these things would run more efficiently as an application than they do as a web site.

    Hell, the new flickr.com is probably the best example. In their desire to have a wall of images in the search results, the javascript spends so much time examining possible arrangements of the images that the computer grinds to a halt. The web site is damn-near unusable, and certainly only "usable" if you're desperate. As far as "I'm bored so I'll look for something interesting on Flickr" goes, it's positively worse than being bored. Granted, they should do the intelligent thing and just go back to the web site they had, but as long as they're insistent on styling their image indexes like that, if they were to do it in a custom application, it could figure out the arrangements it wants in the blink of an eye and so the user experience wouldn't be so miserable.

    The craziest thing about it for me is that, given the choice between making a web site and making a proper application, I'd rather make a proper application. Communicating with a server over a TCP connection is a piece of cake, so I don't need a web browser for that, nor do I want to structure everything as an HTTP request anyway. I also don't have to worry about cross-testing in every version of every web browser. Granted, you do then have to cross-test to different operating systems, but at least between Windows and Linux, I've found most of the issues that crop up tend to be data access errors that show up in one version but not the other simply due to data being arranged differently in each executable, and I haven't even seen that since compiling with -fstack-protector-all and writing a wrapper for malloc() in an effort to catch any invalid data access. With web sites I always seem to spend at least 20% of my time trying to make things look at least reasonable in every web browser, and I'm not one of those people who even tries to do fancy shit, I just keep running issues into things like one web browser inserting a blank line somewhere where another doesn't and trying to figure out some way to structure everything so that, if they can't all look the same, then at least none of them end up with ass-ugly formatting.

    So I can only guess that the "everything must be a web site" is a user-driven demand.

    Part of that might be the Windows convention that no matter how trivial