when overhead matters: return tables, or multiple values?
Posted: Thu Jan 28, 2016 9:41 pm
I can already tell this is an "it depends" question, but I have to ask it anyway.
I've been thinking about my lower level design of my game a lot from the beginning. I chose to implement all my methods that return x,y coordinates as:
And then I have X globally defined as 1 and Y globally defined as 2, so I can easily pull out X or Y or use both. I like how this looks visually and it is pretty simple in my head. (I do the same thing with functions that return dimensions: return {self.width, self.height})
However, obviously get_location() functions are called LOTS of times per gametick on LOTS of different items. And each time, a table has to be created. So I'm making something on the order of thousands (millions? I honestly don't know) of tables per second just to return x,y pairs.
I'm not far enough to know if this will effect performance. I'm guessing it won't, not noticeably. But I can't help but think of it and ask people who might know more.
So how much overhead does simple table creation create? If I switch to multiple return values (i.e., say) instead of ) am I really saving any time/cycles? Again, I'm sure I might be overthinking this, but I'm so curious that I have to know.
Thanks!
EDIT: and how could I forget to mention function overhead? By calling a get_location() function on tons of objects every frame, I'm incorporating function overhead and table generation at the same time, when I could just be doing object.xy to directly access the table. Is it worth eliminating all that function overhead for slightly less readable and more confusing code?
I've been thinking about my lower level design of my game a lot from the beginning. I chose to implement all my methods that return x,y coordinates as:
Code: Select all
function whatever:get_location()
return {self.x, self.y}
end
However, obviously get_location() functions are called LOTS of times per gametick on LOTS of different items. And each time, a table has to be created. So I'm making something on the order of thousands (millions? I honestly don't know) of tables per second just to return x,y pairs.
I'm not far enough to know if this will effect performance. I'm guessing it won't, not noticeably. But I can't help but think of it and ask people who might know more.
So how much overhead does simple table creation create? If I switch to multiple return values (i.e., say
Code: Select all
return self.x, self.y
Code: Select all
return {self.x, self.y}
Thanks!
EDIT: and how could I forget to mention function overhead? By calling a get_location() function on tons of objects every frame, I'm incorporating function overhead and table generation at the same time, when I could just be doing object.xy to directly access the table. Is it worth eliminating all that function overhead for slightly less readable and more confusing code?