UPS-friendly Selector combinator


Adds a Selector combinator that can select from the inputs sorted by largest/smallest, count inputs, output a random input, calculate stack sizes, and, if the Janky quality mod is installed, transfer quality between network signals.

Content
7 months ago
1.1
519
Circuit network

g Great! Thanks for the Mod!

8 months ago
(updated 8 months ago)

I was looking on the deprecated mod's code the other day and i've noticed it sorts the table every tick. I know you are not doing the same thing, but an optimization suggestion: for indexes less than 4, just keep an array with the top 4 seen values, and do a single pass. For bigger indeces, using a heap might help as well. Getting the k-th element in a heap is O(k * log(n)). Which is better than sorting (n*log(n)). Need to profile it to be sure, of course.

Other, simpler suggestion: a setting for update frequency. I think I could live with one update every 10 ticks, for instance, and its an easy way to save ups

8 months ago

Thanks for the feedback!

I may add an update frequency setting, but I think the benefit would be limited. Selectors without inputs have very low CPU impact (<2 μs/tick each), so players can already get nearly all of the benefit by using a clock to pulse inputs. This has the benefit of not clustering updates on the same tick. That said, we could minimize clustering effects by having two settings (update X selectors every Y ticks), but any players using that would lose the ability to have specific selectors update together. It also has the sticky side effect of having the behavior of selectors be affected by the number of selectors in the game. Adding or removing selectors could break existing circuitry, and a blueprint that works in my save might not work in yours. That said, if there is demand for an update frequency setting, I will look into adding it, but I hesitate to do so.

I'll test out getting the k-th element using a heap if sorting becomes the bottleneck. (I'm interested in finding out how it would compare to using table.sort, since Factorio's version of Lua implements it in C: https://github.com/Rseding91/Factorio-Lua/blob/master/src/ltablib.c#L332) Right now, most of the mod's update time is spent having selectors read their inputs from the circuit network. This is what I'm looking in to now, since we might be able to save time here by noticing when selectors are sharing input wires, and then reading signals once per wire instead of once per selector.

New response