Hacker News new | ask | show | jobs
by vvanders 3386 days ago
I'll give you one: value types.

Until you can specify memory layout you'll never come near a proper gamedev engine, regardless of GC issues(which are also a problem).

(source: I used to work on professional game engines)

3 comments

You're right, which is why one of the fundamental optimizations in a modern Javascript engine is inferring static memory layouts for your objects. Another is getting rid of unnecessary allocations to reduce the need for GC.
> inferring static memory layouts for your objects

I'm deeply suspicious that you'll do better than something purpose built. Things like arena allocators for levels. Knowing how your scene-graph and animation traversal are really hard to get right and without direct control over SoA vs AoS and the like I don't see how you get within a factor of 10 to something written in native.

asm.js and WebAssembly give you the same control over memory layout as C (in fact the memory layout is absolutely identical to a little-endian 32-bit host) and unless you need to call a lot out into HTML5 APIs the garbage collector will be idle. These two things together are basically the magic sauce why asm.js/wasm are faster than manually written JS and faster then most 'managed languages' where most objects are created on the heap and passed around by reference.
Totally, since the memory model is identical. However then you're not really talking about something that looks like JS(or a managed language) at all.

I'm actually a huge fan of WASM, I think it gives you a great escape hatch to break out of these problems(much like JNI/etc in other managed languages).

> asm.js and WebAssembly give you the same control over memory layout as C

That's a bold statement right there.

Not at all, both asm.js and wasm give you a flat memory region (in asm.js it's a single, big JS typed array), the stack and all allocations live in this 'heap', managed by emscripten's malloc wrapper (which is jemalloc I think), the resulting memory layout is the same as on a native 32-bit platform, with the same alignment rules. There are no 'managed' Javascript objects in asm.js or wasm (unless you need to call out into web APIs).
Luckily most JS engines "unbox" simple values to "value types" if it can be determined that they will stay that way.

so aside from startup speed you get the same perf

> simple values

So does every other language, where it matters is with objects. Until I can mark a whole object as a value type and fix it in memory relative to other objects then you'll pay a 10-50x slowdown for cache misses and the like.

Sorry, that should have said "simple objects".

But you don't even need to rely on that. JS has TypedArrays[0] which are basically just a chunk of memory you can read and write to.

Aside from some bounds checking, it's about as "low level" as you can get. Sounds like exactly what you are looking for.

[0] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Type...

Typed arrays are close, but still not good enough.

On the Java side I've used ProtoBuf for similar approach(using byte[] which does have memory placement semantics). While it helps, the bounds checking and accessors that you need still have a non-trivial cost both in branching and cache thrashing(len is usually stored for an array at a different location or at the head so data items near the mid/tail will still take a hit).

Realistically you can get within about 1/5th the speed of native using cache aware data structures and techniques. As always it matters if your use case needs that speed but choosing a JS/managed based tech-stack will always limit you from getting that last 5x perf improvement.