Page 1 of 1
Speed of getters/setters as opposed to average variable access?
Posted: Mon Sep 30, 2019 4:25 am
by allfalldown
So currently, I'm making a simple wrapper class, PhysicsObject (just to make handling it easier), for all three important physics objects needed for something in Love2D - the fixture, shape, and body. As one might expect, there will be a few cases where I'll need to directly access the separate objects' variables inside this wrapper class - like (where self.m_physObj is the wrapper class, and m_body is the "body" object)
Code: Select all
self.m_physObj.m_body:getX(), self.m_physObj.m_body:getY()
and such.
At first glance though, this looks like it'd be a lot neater if I just wrote out a function
Code: Select all
function PhysicsObject:GetX()
return self.m_body:getX()
end
and such. My question is, will this affect speed in any way, and/or is this bad practice?
Re: Speed of getters/setters as opposed to average variable access?
Posted: Mon Sep 30, 2019 5:30 am
by ivan
Welcome to forums.
The problem is not the speed (yes, using wrappers is marginally slower) the problem is that it's just redundant and clunky. I've been using Box2D for a long time, trust me when I say that you don't need to do that. The important thing is that the most commonly executed code runs fast. The most common code will be, x,y,a=body:getTransform() and then love.graphics.draw(sprite, x,y,a). This sort of operation happens several thousands of times per second (for every body and per every frame).
I'll need to directly access the separate objects' variables inside this wrapper class
Now if you absolutely need to access just the body's X position then go right ahead. But note this this a rare case that happens much less often and you don't need a wrapper for it.
I just published a
tutorial on Box2D and it shows how to inject your own functionality directly in the Box2D objects (without wrappers). But we do that just to de-clutter the API. So don't insert all sorts of redundant and useless functions in there and stay away from wrappers as much as possible. love.physics is already a wrapper for Box2D.
Re: Speed of getters/setters as opposed to average variable access?
Posted: Mon Sep 30, 2019 6:49 am
by raidho36
Lua doesn't support class encapsulation (or even classes for that matter) so there's no natural reason to use getters and setters. Non-trivial getters and setters - that do substantial amount of computation and not just fetch a single value - are still useful. Note that Lua nor LuaJIT in particular will inline your trivial getters and setters like C++ would, and using those over direct access would be slower, for no reason.
Re: Speed of getters/setters as opposed to average variable access?
Posted: Mon Sep 30, 2019 8:25 am
by Imagic
raidho36 wrote: ↑Mon Sep 30, 2019 6:49 am
Note that Lua nor LuaJIT in particular will inline your trivial getters and setters like C++ would, and using those over direct access would be slower, for no reason.
The JIT can inline calls, but in this case the C call will prevent the trace to be compiled (LuaJIT 2.0).