0

In Lua, I have a Player metatable:

Player = {}
Player.__index = Player
_players = {}

I create the player object when needed:

function Player:new(id)
 local player = {id = id, name = ...}
 setmetatable(player, self)
 _players[id] = player
 return player
end

Is this function hurting performance by creating new player tables, or simply holding references to the player tables in _players?

function Player:list()
 local player_list = {}
 for _, player in pairs(_players) do
 table.insert(player_list, player)
 end
 return player_list
end

In other words, does copying a table's values (which are tables themselves) into a new table double the memory used? Or is it just a reference?

asked Aug 16, 2022 at 6:05
1
  • The function stores references to existing player tables. Commented Aug 16, 2022 at 6:51

1 Answer 1

1

https://www.lua.org/manual/5.3/manual.html#2.5.3 Section "2.1 – Values and Types"

Tables, functions, threads, and (full) userdata values are objects: variables do not actually contain these values, only references to them. Assignment, parameter passing, and function returns always manipulate references to such values; these operations do not imply any kind of copy.

answered Aug 16, 2022 at 10:10
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.