Automatic Train Painter


Automatically colors locomotives and wagons based on their contents.

Tweaks
a month ago
0.17 - 2.0
98.3K
Trains

i [Implemented] Easy way to increase UPS efficiency by a factor of 10

3 years ago
(updated 3 years ago)

Hi!

Love your mod, but not a huge fan of the UPS impact it has on my 100-train base. So I ran it through jarg's profiler and found that the main issue could be solved with a bit of caching. Currently, each time a train changes state, you're checking the active mod list against a table of ~80 mods to see if any item names need to be adjusted (and then repeating this for each unique item on the train). Thing is, that active mod list isn't ever going to change except during startup, so you're better off caching it and adjusting it only when needed.

I went ahead and implemented a fix on my local copy of the mod--here's what I did:

In control.lua, I added the following:

local function cache_mods()
    global.mods = {}
    for mod, t in pairs(mod_list) do
        if script.active_mods[mod] then
            global.mods[mod] = t
        end
    end
end

script.on_init(cache_mods)
script.on_configuration_changed(cache_mods)

I also changed the line for LTN check to:

        if global.mods["LogisticTrainNetwork"] then

In color/util.lua, I replaced lines 10 and 11 with:

    for mod, filterlist in pairs(global.mods) do
        if filterlist[3] ~=nil then

There were a few other places that might benefit from some caching, but honestly they're pretty irrelevant when compared to this. I tested this new code on my big train base. Before the changes, I was seeing ~0.6 ms per average update, and after, I saw ~0.05 ms per average update.

I hope you consider making those changes!

3 years ago

Wow, I appreciate the effort! I'll test it out and update soon if all goes well.

3 years ago

This fix has been implemented! Thanks again for your contribution!

3 years ago

Awesome, thanks for implementing it :) I'm glad I was able to help!

2 years ago

Was reading through the code and found some strangeness, it looks like it was introduced here... caching the mod list is a good idea but you made a few mistakes:

You can only have one event handler per event. When you do script.on_init and script.on_configuration_changed in control.lua, that is overriding those handlers from LTN.lua so the LTN setup is never called.

Furthermore, since "LogisticTrainNetwork" isn't in mod_list, global.mods["LogisticTrainNetwork"] will always be false.

It looks like LTN compatibility has been broken for a year :/ (personally I'm not a fan of LTN so I don't particularly care though!)

2 years ago

You are correct! I also noticed this while doing updates this weekend and have the fix ready to go.

2 years ago

Nice :D

2 years ago

Whoops haha, my bad! I never tested this with LTN installed so I didn't discover any of that. And it never occurred to me to check if those functions already had handlers defined elsewhere. Glad it's getting fixed though!

New response