mojira.dev
MC-81098

Redstone dust updates cause lag

Redstone dust causes immense amounts of lag on servers (including local SinglePlayer "servers"), and here's why (deduced from the game code, as well as a mod I wrote to help track down some redstone querks):
Except for a redstone dust block updating (at least) 23 blocks around it, every time it is placed or receives any sort of update, apparently, redstone dust also DOES NOT directly de-power, as you'd expect. Instead, it continually loses 1 signal strength until it's "satisfied" and a check fails, meaning it doesn't need to update anymore. The 15 signal strength levels it needs to lose (one-by-one, mind you), each time causing at least 23 block updates, all happening within 1 gametick (1/20th of a second, around 15 * 23 = 345 block updates in total) cause a lot of unnecessary calculations to be done. Not only that, but this example only involved one piece of redstone dust. Imagine the same happening with a line, or even a grid. There it's even worse, since the dust blocks update each other again and again multiple times in 1 single game tick, so that a line of 15 redstone dust can easily amount to around 2,500 block updates in total.
Each calculation is independently quite quick and simple, but any calculation run 2,500+ times in a 1/20th of second, where a lot of other calculations also need to run in that short time frame, is heavy, to say the least.

I suggest rewriting the redstone dust update code, as it is quite simple and will reduce a whole load of lag on any server with redstone clocks running.
Redstone dust is a horrible lag causer, and it does so in one of the most unnecessary ways in the entire game.

Here is a short clip demonstrating (using my mod, as mentioned above) how 1 piece of redstone dust alone causes 15 * 23 = 345 block updates when de-powering:
https://www.youtube.com/watch?v=T3bST3JGgas

Final note: The "Affected Version/s" field only allows set versions, and only the most recent ones, however, this "bug" has existed ever since redstone dust was added to the game, all the way back in Alpha 1.0.1.

Possible solutions

Linked issues

Attachments

Comments 41

Dlawso the Really Lucky Rabbit

Dupe of MC-76561

@Sonic I disagree. He is talking about lag, not knowing what causes it, as well as talking specifically about 1.7 worlds running in 1.8 servers. I am talking about (pretty much) all versions of Minecraft, while also providing proof and a thorough explanation of the phenomenon.

Is any of this a "feature" to Redstone designers that would have to be considered before rewriting the implementation? (The performance savings sounds worthwhile regardless. I'm just curious.) For example, if two things on the same circuit are at power-13 and power-12, will the power-12 one always depower first, and does that ever matter?

I read your note about the affected versions but you should still specify it as all the versions it allows you to select.

@@unknown as of this moment you can only select the current version 1.8.7, so he has selected everything he can.

31 more comments

Hi all,

Like a few before me I have attempted to fix some of the issues with the current implementation of redstone dust. It is both laggy and unpredictable, making it a pain to work with, and leading to it being the most avoided redstone component in the game. Before I get to my solution to these issues, though, I want to acknowledge the work done by @unknown and @unknown, as both have been an inspiration in one way or another.

My implementation is mainly developed as a Fabric mod but has also been implemented into Paper.

Like the other two implementations, mine does not solely address the lag issue. It is a complete re-write of the power propagation code, fixing MC-11193 in the process. It is not a re-write of redstone as a whole, however. I attempted to keep existing behaviors as much as possible while fixing the lag of and inconsistencies with redstone dust that make it hard to rely on. To be more specific, it addresses the following issues:

1. Redstone wire does unnecessarily many calculations. Each wire in a network may calculate and update its power level over half a dozen times before settling on its final value. Moreover, each time it does so, it updates itself six times, doing even more completely redundant calculations.
2. Redstone wire emits unnecessarily many shape and block updates. This is, of course, related to the previous point, as a wire updates all neighboring blocks (and itself) each time it updates its power level. However, even if only the previous point is fixed, there would be many redundant shape and block updates that can be removed.
3. Redstone wire behaves unpredictably. The order in which a wire updates neighboring blocks is dependent on the location of the wire. Combined with the chaotic nature of the brute-force algorithm with which it updates, this makes it nigh impossible to predict and rely on how a wire network behaves.

I have designed a wire handler that addresses these issues in the following ways:

  • When a wire is updated, a breadth-first search through the network identifies all wires that require power changes, and finds any power sources around the network. 

  • If a wire is found to be unsupported, its removal and subsequent effects on power propagation are integrated into the search.

  • Power is spread from the power sources outward to give each wire its new power level.

  • Wires are updated in order of power level, from highest to lowest. This ensures power is spread most efficiently and makes the update order very predictable and intuitive.

  • Each wire emits shape updates as it updates its block state, in the standard { west, east, north south, below, above } order.

  • Shape updates to neighboring wires are avoided, as they are redundant.

  • Each wire emits block updates in an order dependent on the local direction of power flow. This leaves the update order nearly completely consistent across all locations and all orientations. All credit goes to @unknown for this idea. Unlike RedstoneWireTurbo, however, my implementation exibits directional rather than random behavior in the cases where the direction of power flow is ambiguous, though this is trivial to change.

  • Block updates to neighboring wires are avoided, as they are redundant.

  • While Vanilla parity is not 100% preserved, by far the biggest change is that contraptions that are locational in Vanilla work either everywhere or nowhere with this implementation. Beyond that parity issues appear to be rare.

  • The number of shape and block updates emitted is reduced by ~20x.

  • The MSPT contributions of redstone dust are reduced by up to ~20x.

Can confirm in 23w07a.

Can confirm in 24w13a.

Can confirm in 1.20.5.

Affects 1.21

Sharir

(Unassigned)

Confirmed

Platform

Important

Performance, Redstone

block, dust, experimental_redstone_fixed, lag, redstone, server, update

Minecraft 1.8.7, Minecraft 1.10.2, Minecraft 16w41a, Minecraft 17w18b, Minecraft 1.12.2, ..., 21w39a, 21w44a, 1.18.1, 23w07a, 1.21

Retrieved