coffeenotfound / weml.js

Weml.js is JavaScript linear algebra WebGL Math Library with intuitive syntax, seamless integration with WebGL and fast SISD and SIMD implementations.
https://coffeenotfound.github.io/weml.js/
MIT License
3 stars 0 forks source link

Add temporary object pools #2

Open coffeenotfound opened 7 years ago

coffeenotfound commented 7 years ago

Add functions for retrieving temporary objects from a pool. Most of the time Vector and Matrix instances are only needed for some calculations.

As weml types are Float32Arrays, creating loads of short-lived vectors and such may seriously impact performance

coffeenotfound commented 7 years ago

After doing some minor research I think the GC is good enough and creating temp weml types is fine.

This topic is still open to debate though

coffeenotfound commented 7 years ago

One point to consider is that the creation of a weml type really isn't that fast, mainly because of the manual Object.assign()

coffeenotfound commented 7 years ago

I found that it's quite easy to create mulitple temporary vectors, even for seemingly simple calculations. Most of the vectors aren't even needed for long at all.

At that rate, it may actually be benefitial to have object pools. They probably won't even need to hold many elements. As temporary vectors are only needed for one calculation most of the time the pool would just be passing one or two vectors from calculation to calculation.

coffeenotfound commented 7 years ago

When implemented, the function to retrieve a type should have an optional bool (true by default) that signifies wether to reset the returned type to identity as many operations override the old values anyway.

coffeenotfound commented 7 years ago

I'm not too sure whether it's possible have WeakRefs in javascript and even if it were, local vars would keep the references till the end of the function.

So, we need some sort of release function to return them to the pool.

Maybe temporary types should be created with a special release function that has to be called. It would maybe also be useful to have some pushTemporaryScope and popTemporaryScope functions that make sure all temporary types retrieved in the pushed scope will be returned to the pool.

coffeenotfound commented 7 years ago

At the moment the proposed functions are the following:

I'm not a big fan of the term "return". Borrow/return is correct in the context of pools but especially the Vec3.return() may be kind of ambiguous, meaning-wise.