I'm porting a physics engine (chipmunk!) from C to javascript. Internally, 
chipmunk uses 2d vectors pretty extensively. In C, they're a simple struct 
passed by value. In javascript, my vectors are being allocated on the heap.

I did some benchmarks - in 5 seconds, chipmunk-js allocates about 20 
million vectors. The simulation spends about a third of its time in the 
garbage collector. (Eep!).

I would move across to simply storing x and y values, but a lot of the 
vector manipulation functions need to return new vectors. (Eg, add(), 
mult(), rotate(), lerp(), ... etc). If I store (x,y), I need a way to 
return two values from those functions.

My ideas:
- Try and use an object pool of vectors. It might be hard to track the 
lifetime of all the vectors the library uses, but I should be able to 
manually release most of them.
- Make all the functions that return a vector instead store the (x,y) pair 
in a pair of global variables. Other functions can then copy the result 
back when they're done computing. That way, I can remove heap-stored 
vectors entirely; though my code will get super messy.
- Manually inline a lot of the vector functions. Again, my code will get 
messier.

What do you guys reckon? Are there any other options?

My trivial vector implementation is here:
https://github.com/josephg/Chipmunk-js/blob/master/lib/cpVect.js

-J

-- 
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users

Reply via email to