Speed of getters/setters as opposed to average variable access?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
allfalldown
Prole
Posts: 2
Joined: Sun Aug 25, 2019 2:21 am

Speed of getters/setters as opposed to average variable access?

Post 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?
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Speed of getters/setters as opposed to average variable access?

Post 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.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Speed of getters/setters as opposed to average variable access?

Post 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.
User avatar
Imagic
Prole
Posts: 45
Joined: Mon Sep 30, 2019 8:20 am
Contact:

Re: Speed of getters/setters as opposed to average variable access?

Post 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).
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Semrush [Bot] and 3 guests