Lua Signal Class Comparison & Optimal `GoodSignal` Class

The linked list has very good iteration stability while also being fast.

If you delete an entry out of an array, the index of each entry after it shifts. That means that any for loops that are in the middle of iterating over it may have their behavior messed up.

The SimpleSignal implementation solves this by copying the handler list and iterating over the copy instead.

You could also solve it by using a copy on write list of handlers, where you copy the handler list before you add or remove an entry.

But the intrusive linked list approach (intrusive in that the connections themselves serve as the links) solves the problem without any copy at all, without even sacrificing any performance in other ways. This works because even if a node is removed from the linked list (through the previous node no longer pointing to it), any loops sitting on that node can still follow the next pointer to get to the next node in the list.

8 Likes