Use case of TempVars

I’m reading some engine code and noticed the use of TempVars. If I understood it right, this class is a “trick” to avoid instantiating commonly used classes and frequently called functions (loops), like Vector3f, Quartenions and such?

If that’s the case, may I use TempVars on my own code? (Following the correct use of it - get() and then release()).

Yes you can use it yourself… But do note that there’s a limit to how deep the stack can get.

1 Like

If i’m not wrong, it can go deep at most 5 levels, correct?

Yes, but I wouldn’t recommend to use it, IMO.
The engine is made to not go over this limit, but you don’t know how many tempvars are used for an instant t. So you can easily go over and have unexpected crash in your code. Especially If you have a multi threaded achitecture that could pick the tempVars at different time.

If you have a situation where a quaternion should be instantiated on every frame, you’re better off with a temp class variable IMO. The memory overhead is really minimal.
If you have that very often, I suggest you make your own MyProjectTempVars, and only query from it, so you can control the depth of the stack.

1 Like

Actually each thread gets its own copy of TempVars … But yeah, you’re still limited to using 5 of those in a single thread call stack.

1 Like

I got the point. Indeed even if I got the right timing and don’t mess with TempVars, maybe another future change on engine will do, so I’ll have to retest this feature every time a release came.

I’ll keep that on mind, if I need it, I’ll extend and make my own TempVars. Thanks guys, I loved this “trick” :stuck_out_tongue_closed_eyes:

Yep, my point is that with threading don’t control the moment a tempVar will be picked from the stack, so you could end in a situation where you need 6 of them randomly.

If you are not on android, you can get away with obscene amounts of garbage in the 1.8 jvm with g1 collection.

Since I got a little lost on what you said about g1 collection and had to search to understand that is a new GC “system”, do you have a good article to start reading about it? It’s time to dig deep into GC…


Angelika Langer wrote quite much about the internals of the java memory model as well as the garbage collectors.

1 Like

I’m afraid that this isn’t English :smile:. I’ll try Google Translator and see what I got.

Hm right ^^ forgot about that.
Maybee this slides will help


very interesting read.