Page 1 of 1

Cacheability of inventories

Posted: Mon Jul 19, 2021 8:51 am
by PFQNiet
I've noticed my code is actually doing "entity.get_inventory(...)" in a less-than-optimal manner.

Can I just save a reference to the LuaInventory in global and use that in future calls, instead of having to query the game engine for it?

Similarly, what about stacks within that inventory? If my inventory only has one slot, it would be better if I can just store "entity.get_inventory(...)[1]" once and use that local cache instead.

I already have a guard for "entity.valid". If the entity remains valid, can I just assume the entity's inventories and item stacks are also valid?

Re: Cacheability of inventories

Posted: Mon Jul 19, 2021 10:22 am
by Klonan
PFQNiet wrote:
Mon Jul 19, 2021 8:51 am
I've noticed my code is actually doing "entity.get_inventory(...)" in a less-than-optimal manner.

Can I just save a reference to the LuaInventory in global and use that in future calls, instead of having to query the game engine for it?

Similarly, what about stacks within that inventory? If my inventory only has one slot, it would be better if I can just store "entity.get_inventory(...)[1]" once and use that local cache instead.

I already have a guard for "entity.valid". If the entity remains valid, can I just assume the entity's inventories and item stacks are also valid?
Should be fine,
Also the stack has its own `.valid` operator that you should check before accessing it

Re: Cacheability of inventories

Posted: Mon Jul 19, 2021 12:33 pm
by PFQNiet
If I know that the entity I'm working with is a "container" with a 1-slot inventory, is it safe to assume:

Code: Select all

if entity.valid then
  local slot = entity.get_inventory(defines.inventory.chest)[1]
  -- slot IS valid here, right?
end
The slot will be valid, and will continue to be valid until the entity is removed. That's my understanding, anyway...

Re: Cacheability of inventories

Posted: Mon Jul 19, 2021 3:35 pm
by DaveMcW
If you are caching a single LuaItemStack, you can't use entity.valid or inventory.valid.

Re: Cacheability of inventories

Posted: Mon Jul 19, 2021 4:58 pm
by Klonan
PFQNiet wrote:
Mon Jul 19, 2021 12:33 pm
If I know that the entity I'm working with is a "container" with a 1-slot inventory, is it safe to assume:

Code: Select all

if entity.valid then
  local slot = entity.get_inventory(defines.inventory.chest)[1]
  -- slot IS valid here, right?
end
The slot will be valid, and will continue to be valid until the entity is removed. That's my understanding, anyway...
But I though the point was to not look it up from the entity each time?

Re: Cacheability of inventories

Posted: Mon Jul 19, 2021 9:41 pm
by PFQNiet
Right. I'm not explaining this very well XD

I am storing a reference to the entity, so entity.valid will be available to me. I want to know if it is "safe" to store the entity's inventory's first slot as well, so that I don't have to query the game for it each time I want to access that slot. Basically, reducing the number of API calls and thus improving the code performance.

Re: Cacheability of inventories

Posted: Wed Jul 21, 2021 10:15 pm
by eradicator
Eradicator's Visible Stockpile stores thousands of LuaItemStack references (for update randomization), never had a problem with that. They do have valid and valid_for_read.