News for hackers. Stuff that matters.
Re:Why I Write Games in C (yes, C) by z3t4 on Saturday September 21, 2019 @02:48PM (#21037140)
With JS the trick is to avoid creating new objects and instead have a pool of objects that are always referenced.
Re:Why I Write Games in C (yes, C) by bcrescimanno on Saturday September 21, 2019 @03:22PM (#21037330)
Definitely! Object pools are common in game dev too from what I know. We used them extensively to reduce the amount of GC we needed.
Re:Why I Write Games in C (yes, C) by eutropia on Saturday September 21, 2019 @06:12PM (#21038209)
Speedrunners thank you for reusing objects! I'm certain that decisions like this are what lead to interesting teleportation techniques and item duplications. Games wouldn't be the same without these fun Easter eggs!
Re:Why I Write Games in C (yes, C) by tomxor on Saturday September 21, 2019 @03:32PM (#21037388)
Once I wrote a very small vector library in JS for this very reason: almost all JS vector libraries out there tend to dynamically create new vectors for every vector binary operation, this makes JS GC go nuts. It's also prohibitively expensive to dynamically instantiate typedarray based vectors on the fly, even though they are generally faster to operate on... most people focus on fixing the latter in order to be able to use typedarrays by creating vector pools (often as part of the same library), but this creates a not-insignificant overhead.

Instead my miniature library obviated pools by simply having binary operators operate directly on one of the vector objects passed to it, if more than one vector was required for the operation internally they would be "statically allocated" by defining them in the function definition's context (some variants i would also return one of these internal vectors - which was only safe to use until a subsequent call of the same operator!).

The result this had on the calling code looked quite out of place for JS, because you would effectively end up doing a bunch of static memory allocation by assigning a bunch of persistent vectors for each function in it's definition context, and then you would often need to explicitly reinitialize the vectors if they were expected to be zero.

... it was however super fast and always smooth - I wish it was possible to turn the GC off in cases like this when you know it's not necessary. It was more of a toy as a library, but i did write some small production simulations with it - i'm not sure how well the method would extend to comprehensive vector and matrix libraries, I think the main problem is that most users would not be willing to use a vector library this way, because they want to focus on the math and not have to think about memory.

Re:Why I Write Games in C (yes, C) by RobertKerans on Saturday September 21, 2019 @06:32PM (#21038302)
You wouldn't have this still up somewhere like GH would you? I'm currently writing a toy ECS implementation and have somewhat similar needs, and I've been trying to build up a reference library of sorts covering novel ways of dealing with these kind of JS issues
Re:Why I Write Games in C (yes, C) by tomxor on Saturday September 21, 2019 @07:32PM (#21038595)
No sorry, this is from more than 5 years ago and it was never FOSS, but I only implemented rudimentary operators anyway, you could easily adapt existing libraries or write your own, the above concept is more valuable than any specific implementation details... the core concept being, never implicitly generate objects, e.g operate directly on parameter objects, or re-use them for return value, or return persistent internal objects (potentially dangerously since they will continue to be referenced and used internally).

All of these ideas require more care when using the library though.

Re:Why I Write Games in C (yes, C) by RobertKerans on Sunday September 22, 2019 @06:57PM (#21044521)
Thank you though, what you've written here is very useful -- you're describing things I'm immediately recognising in what I'm doing. As I say, it's just a small toy (and I think one that would definitely be easier in a different language, but anyway...). At the minute I'm actually at the point where I'm preallocating and persisting a set of internal objects, and at a very very small scale it's ok, but each exploration in structure starts to become manageable pretty quickly.
Re:Why I Write Games in C (yes, C) by meheleventyone on Sunday September 22, 2019 @09:09AM (#21041184)
Three.js implements their math this way.

https://github.com/mrdoob/three.js/blob/master/src/math/Vect...

You can see most operations act on the Vector and there are some shared temporary variables that have been preallocated. If you look through some of the other parts you can see closures used to capture pre-allocated temporaries per function as well.

Re:Why I Write Games in C (yes, C) by RobertKerans on Sunday September 22, 2019 @06:50PM (#21044492)
Ah brilliant, that's definitely useful. Thank you for the pointer
Re:Why I Write Games in C (yes, C) by rienbdj on Saturday September 21, 2019 @04:31PM (#21037759)
This kind of place orientated programming can make the actual algorithm very hard to follow. I really hope that JS gets custom value types in the future.
Re:Why I Write Games in C (yes, C) by chrisseaton on Saturday September 21, 2019 @04:30PM (#21037752)
But then that damages performance because your objects are always globally visible rather than being able to be optimised away.