Open coffeenotfound opened 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
One point to consider is that the creation of a weml type really isn't that fast, mainly because of the manual Object.assign()
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.
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.
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.
At the moment the proposed functions are the following:
weml.tempVec3(reset)
to borrow a temporary Vec3 from the pool (creating a new one if none is unused). reset
is a bool and temporary with a default value of true
that signifies whether the vector should be reset to identityweml.returnTempVec3(vec)
or Vec3.return()
to return a temporary Vec3 to the pool. (temporary types should have a _temptype
property to speed up the validation of whether the given type actually is a temporary type)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.
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