What is Lua garbage, and how do I reduce it

Place to get help with not working mods / modding interface.
Post Reply
Mernom
Fast Inserter
Fast Inserter
Posts: 122
Joined: Sun Jul 15, 2018 10:05 pm
Contact:

What is Lua garbage, and how do I reduce it

Post by Mernom »

I'm trying to revive the hazard lights mods for my own use, and I have managed to update them to use selection tools instead of the deconstruction planner trick they had used prior, getting me past the boot stage.
Unfortunately, it seems like the mod did something else really wrong during the control phase, since the garbage collection number went absolutely BONKERS. It's over 900(0)!
By comparison, without it it was in the 0.9 range.

So, what sorts of actions can end up creating excessive amounts of garbage?

Qon
Smart Inserter
Smart Inserter
Posts: 2118
Joined: Thu Mar 17, 2016 6:27 am
Contact:

Re: What is Lua garbage, and how do I reduce it

Post by Qon »

The render API has O(n) time for access with ID. So modifying anything is slow. Everything is in a big array, per mod. Removals might be O(n^2). n is the total amount of render objects existing that are created by your mod. Doing clear() to remove everything is fast in comparison if you want to remove everything since it just removes the whole array of objects as one operation. So in some/many cases it's faster to remove everything and then rebuild all render objects you didn't want to get rid of since removing 1 thing takes maybe 100x more time than adding 1 thing which is O(1).

In my mod ChunkyChunks (renders grids) I just try to reduce the amount of rendered objects (lines) by having looooong lines and using "dashed" lines in a somewhat clever way to get many lines per render object.

This thread is probably helpful: viewtopic.php?f=28&t=85885

Mernom
Fast Inserter
Fast Inserter
Posts: 122
Joined: Sun Jul 15, 2018 10:05 pm
Contact:

Re: What is Lua garbage, and how do I reduce it

Post by Mernom »

I can see why it would be bad... That mod renders 4 lights per entity basically

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: What is Lua garbage, and how do I reduce it

Post by eradicator »

Mernom wrote:
Mon Jun 28, 2021 8:23 pm
I can see why it would be bad... That mod renders 4 lights per entity basically
I remember that the original bottleneck (with only one "light" per entity) used real entities instead of rendering because of how much faster it is to deal with them. Dealing with real entities you have to deal with removing them when the entity is removed though, but that got a lot easier since the introduction of LuaBootstrap.register_on_entity_destroyed. You also have to find a fitting entity prototype that can create light without requiring energy (aka void-energy capable?). And in case someone uses an entity-mover mod (like Picker Extensions) and you care about that kind of thing you'd also have to deal with moving the lights.

*If* the rendering is really the problem. I assumed you meant something quite different when you mentioned the garbage collector.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

Mernom
Fast Inserter
Fast Inserter
Posts: 122
Joined: Sun Jul 15, 2018 10:05 pm
Contact:

Re: What is Lua garbage, and how do I reduce it

Post by Mernom »

I honestly have no idea why the garbage collector went bonkers, just that it did. Without the mod, it's at 0.9. With the mod, it's over 900.
Also, it seems like it already has picker compatibility so I won't have to deal with that.

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: What is Lua garbage, and how do I reduce it

Post by eradicator »

Mernom wrote:
Tue Jun 29, 2021 8:59 am
Also, it seems like it already has picker compatibility so I won't have to deal with that.
LuaRendering has that built-in. I was talking about if you were to replace that with entities for performance.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

Post Reply

Return to “Modding help”