News for hackers. Stuff that matters.
Re:WebAssembly Raytracer by Sharlin on Saturday December 16, 2017 @06:50AM (#15939107)
Firefox really benefits from WebASM: the time per frame is ~90ms on my machine whereas the JS version is close to 400ms or so.

On the other hand, on Chrome the WebASM performance is ~100ms and JS is just a tiny bit slower at around 120ms!

Re:WebAssembly Raytracer by Klathmon on Saturday December 16, 2017 @07:04AM (#15939170)
That's a really interesting observation on so many levels!

Firefox (or rather SpiderMonkey) seems to really be focusing on WebASM performance, and it's easy to forget just how goddamn fast V8 has made JavaScript...

Re:WebAssembly Raytracer by jsheard on Saturday December 16, 2017 @07:46AM (#15939300)
It's worth noting that this isn't idiomatic Javascript though, the author is jumping through hoops to avoid ever triggering the garbage collector.

Javascript can be very fast if you go down that route but the code gets extremely ugly.

Re:WebAssembly Raytracer by Klathmon on Saturday December 16, 2017 @07:50AM (#15939315)
Isn't most "fast path" code in a GCd language written like this?

I've jumped through similar hoops in Go, and even C++ to avoid allocation or GC.

The fact that a dynamic language like JavaScript can compile and execute as fast as it does is nothing short of incredible! Even if it's not idiomatic, it's still far from "unreadable".

Re:WebAssembly Raytracer by pjmlp on Saturday December 16, 2017 @10:10AM (#15939845)
Not if the language supports a mix of value types, off heap allocations or untraced references.

There are a few like that.

Re:WebAssembly Raytracer by imtringued on Sunday December 17, 2017 @05:18PM (#15948008)
Can you name them?
Re:WebAssembly Raytracer by pjmlp on Monday December 18, 2017 @03:27AM (#15950269)
Yes, surely.

- Eiffel

- Oberon family (Oberon, Oberon-2, Active Oberon, Component Pascal, Oberon-07)

- Mesa/Cedar

- Modula-3

- D

- Nim

- C# (specially now with 7.2 improvements)

- Go

- Swift (RC is also a form of GC algorithm)

These are just the most well known ones, there are others if you feel like having a dive into SIGPLAN papers.

I can also provide examples, how to do it in any of them, if you wish.

Re:WebAssembly Raytracer by rcfox on Saturday December 16, 2017 @10:51AM (#15940036)
What do you consider jumping through hoops? The code looked pretty normal to me.
Re:WebAssembly Raytracer by sniklaus on Saturday December 16, 2017 @04:29PM (#15942242)
I initially wrote the raytracer in more idiomatic way. However, I wanted to show my students a side-by-side comparison, demonstrating that the program logic is pretty much the same. Take it as you will, I understand your critique though.
Re:WebAssembly Raytracer by djellybeans on Saturday December 16, 2017 @06:32PM (#15942911)
Idiomatic will probably entail a lot of objects for the pixel data, I'm assuming? I've seen some raytracers written with arrays of simple "bags" of numbers, which would increase performance.

I wrote a real-time 3D polygonal rasterizer in JS that supports texture mapping. After switching from regular arrays to Float32array to store coordinates, it got a huge (~50%) jump in performance.

Re:WebAssembly Raytracer by bartread on Saturday December 16, 2017 @11:46AM (#15940267)
It really depends what you mean by "very fast". For most event driven UI applications JavaScript can certainly be more than fast enough, even on relatively underpowered devices (say, iPhone 5S).

With that being said, I recently ported a bunch of data heavy JS calculation code that had previously been running in the browser (and downloading about 100MB of data to the browser to do so - ouch) to run on a .NET back-end.

(Ignore the fact that doing this on the client will sound crazy to a professional software engineer but I would argue is totally legitimate in the context of a smart person who is not a software engineer building a prototype with tools they understand, which is what happened.)

Now, in doing the port I did improve and optimise the way the calculations were performed: basically I eliminated some redundant operations and structured the data up-front in a way that made it easier to work with. This in itself only sped things up by a factor of 2 or 3.

What really gave me the performance boost was taking advantage of the strong typing that languages such as C# offer. Just switching over to using strongly typed dictionaries via generics, and strongly typed arrays, having initially used IDictionary<string, object> (this, because I just wanted to get it working to start off with, and wasn't sure I'd always be working with the same type of data), gave me an extra 75 to 100 times performance boost.

In the end the C# version outperformed the JS version by a factor of around 200, mostly due to being able to take advantage of strong typing. And here I'm comparing the classic .NET 4.6 CLR to the latest version of Google Chrome, both running on the same machine.

I'm not JavaScript bashing - I use it all the time, and it's good for what it's good for - and V8 is a fantastic runtime, but due to the constraints of the language it simply does not approach the performance of strongly typed languages. I believe I would be able to do even better than the above with C++. Due to the parallelizable nature of the calculations we are considering a switch to CUDA to boost performance further.

If you are running performance-critical CPU-intensive code, unless you have to run that in the browser, JavaScript is generally not going to be the right choice. Based on the figures I see for this renderer, neither is WebAssembly.

If you do need to run that sort of code in the browser, and you want consistent performance across browsers, then WebAssembly might be[1].

[1] As long as you don't need to support IE11. If you do need to support it I'd suggest you find a way to avoid running large amounts of JS since, whilst its performance is definitely the best of any version of Internet Explorer, it's still not what you'd call fast and doesn't do a great job of memory management.

Re:WebAssembly Raytracer by jbergens on Saturday December 16, 2017 @07:52PM (#15943247)
This really sounds strange. Can you put the code up somewhere? There shouldn't be a 75 times improvement by going to .NET. A 50% improvement is more likely.
Re:WebAssembly Raytracer by simooooo on Saturday December 16, 2017 @11:45PM (#15944161)
That is such a picked from the air number.
Re:WebAssembly Raytracer by asgs on Saturday December 16, 2017 @02:39PM (#15941469)
nice comment! how does enforcing type system help improve the performance? is there any article that sheds more light? also, did you mean static typing instead of strong?
Re:WebAssembly Raytracer by khedoros1 on Saturday December 16, 2017 @07:32PM (#15943164)
Static typing means that typechecking can be done at compile-time, and optimizations can be applied then. Dynamic typing generally means that some runtime layer needs to do typechecking during execution. So, as a rule of thumb, it's usually assumed that a statically-typed language will be faster than a dynamically-typed one.
Re:WebAssembly Raytracer by flavio81 on Saturday December 16, 2017 @11:59PM (#15944221)
... unless the dynamically typed language also allows type declarations, intended mainly for enhancing speed.
Re:WebAssembly Raytracer by khedoros1 on Sunday December 17, 2017 @03:56AM (#15944894)
"Dynamic" or "static" isn't a binary value. A dynamic language that allows some static typing is less dynamic than a language that doesn't allow that.
Re:WebAssembly Raytracer by JepZ on Saturday December 16, 2017 @07:14AM (#15939207)
Btw. similar numbers are available on the projects github page: https://github.com/sniklaus/experiment-raytracer
Re:WebAssembly Raytracer by ajross on Saturday December 16, 2017 @12:59PM (#15940713)
Alternatively, it's pretty amazing that a nearly pure FPU task with small data that should live well in cache is only about 5x slower when written in straightforward JavaScript.

This is the kind of code that in previous generations would have expected to see a 20-100x penalty in perl or python.

Re:WebAssembly Raytracer by heavenlyblue on Saturday December 16, 2017 @03:08PM (#15941698)
As previous threads have already mentioned: that is not straightforward JavaScript. There’s nothing impressive about stripping down a scripting language by making it’s structure exactly the same as c in the end.

What’s the point of JavaScript in the first place? What we had done is we took a microscope and then made sure it’s made of toughened glass and the strengthened steel so that we could nail a few nails with it.

Re:WebAssembly Raytracer by WhitneyLand on Saturday December 16, 2017 @10:55AM (#15940061)
But would adding antialiasing kill the performance? It’s a small thing, but it’s kind of like a movie where you can totally suspend disbelief and one bad actor or effect jolts you out it.
Re:WebAssembly Raytracer by Sharlin on Saturday December 16, 2017 @11:13AM (#15940131)
Well, sure, but so would rendering at a more reasonable resolution, or a more realistic scene than five spheres on a plane. And it's not like 10fps is a useful frame rate in the first place. But it's the relative, not absolute, performance figures that are interesting here.
Re:WebAssembly Raytracer by bartread on Saturday December 16, 2017 @11:49AM (#15940280)
> And it's not like 10fps is a useful frame rate in the first place.

How times have changed. I remember running flight sims back in the 16-bit era (Amiga 500, for example) when 10fps would have been considered pretty good, especially if there was a lot going on.

But yes, I think nowadays you're right: 60fps or bust. And this is really why ray-tracing isn't used that much in games - it's just so computationally expensive.

Re:WebAssembly Raytracer by BillinghamJ on Saturday December 16, 2017 @09:19AM (#15939628)
Getting 60ms on Safari on macOS for WebASM.

The other three don't seem to work though.

Re:WebAssembly Raytracer by clouddrover on Saturday December 16, 2017 @10:35AM (#15939955)
The Shader version doesn't work for me in Safari 11.0.1 but the other three did work. I found that I couldn't run any of the others after I had tried the Shader version. I reloaded the page and then I could select between the JavaScript, asm.js, and WebAssembly versions without issue.
Re:WebAssembly Raytracer by renchap on Saturday December 16, 2017 @10:57AM (#15940070)
The Shader version needs WebGL 2.0, and works with the latest Safari Technology Preview
Re:WebAssembly Raytracer by amelius on Saturday December 16, 2017 @07:03AM (#15939167)
Perhaps it was written for Chrome specifically (?)
Re:WebAssembly Raytracer by malloryerik on Saturday December 16, 2017 @07:02AM (#15939163)
Firefox Quantum?
Re:WebAssembly Raytracer by Tuna-Fish on Saturday December 16, 2017 @07:42AM (#15939287)
The first phase of Quantum made styling and layout faster than any browser. The next phase will make rendering faster than anything else. The JS engine is still the old one. (Maybe it's time will come third?)
Re:WebAssembly Raytracer by azdle on Saturday December 16, 2017 @09:31AM (#15939673)
Mozilla people have been working on SpiderMonkey performance already: https://jandemooij.nl/blog/2017/12/06/some-spidermonkey-opti..., it's just not some wholesale replacement, so it probably never makes the top of the list for what they shout about. It also sounds like it has been a steady progression rather than a big jump, so maybe not quite as noticeable.
Re:WebAssembly Raytracer by Sharlin on Saturday December 16, 2017 @07:16AM (#15939215)
Yep, v57.