mojira.dev
MC-272414

Redundant calculation causes 2x lag during explosions

Snapshot 23w45a introduces a redundant set of calculations during the explode method, which leads to a significant (approximately 2x) increase in lag from explosions. This is less likely to be felt on the scale of a single explosion at a time, however it has a notably outsized affect on players who use contraptions that use many TNT or have many entities in close proximity to explosions.

Data from in-game testing in versions 23w44a and 23w45a (shows test setup):
https://www.desmos.com/calculator/c4ccpegwpp

 

Background (for analysis):
The value returned by the getSeenPercent method represents what proportion of an entity is "exposed" to an explosion, and it is a value that ranges from 0 to 1. The value is sometimes colloquially called the "exposure". The lower an entity's exposure, the less of an effect the explosion has on the entity. Exposure is used when calculating how much the explosion damages an entity, as well as how much the explosion propels the entity. The method uses an array of "raycasts" that probe the area between the entity and the explosion for blocks that may protect the entity from the explosion. These raycasts can number in the dozens per entity, and each requires a non-trivial amount of math to compute.

Code Analysis [brackets refer to attached screenshots]:

  • Prior to 23w45a, getSeenPercent was computed only once and stored in a variable that was then used by both the entity damage and propulsion computations [1a].

  • Snapshot 23w45a introduced wind charges, which create non-damaging explosions. In order to allow some explosions to cause no health damage, a condition was added during the explode method that skips computing damage if the damageSource for the explosion is null [1b]. Therefore, explosions with damageSource == null (like those of wind charges) do not hurt entities.

  • The default Explosion constructor previously overwrote null damageSource values with a non-null value [2a], so in 23w45a the constructor was also modified to accept null damageSource [2b].
    Altogether, this means that the logic for skipping damage would work simply by wrapping the existing damage code (1 line of code; see [1a]) in the conditional statement previously mentioned.

Despite this, as of 23w45a, the explosion's damage calculation was relocated to a different class, in a new method that takes only an Explosion object and an entity as parameters [3]. This means that to have the necessary values to compute the damage, the costly exposure computation and a handful of helper values need to be recomputed from scratch using the positions of the explosion and entity, even though all of them are already computed in the explode method  for the applied velocity calculation [see 1b again]. As a result, each explosion now has about twice the amount of work when processing how it affects entities.

Linked issues

Attachments

Comments 4

[^[1a] _23w44a_explode_excerpt.png]
[^[1b]_23w45a_explode_excerpt.png]
[^[2a]_23w44a_Explosion_constructor.png]
[^[2b]_23w45a_Explosion_constructor.png]
[^[3]_23w45a_getEntityDamageAmount.png]

[media][media][media]

Should be clear from the writeup, but this affects versions since 23w45a, and 1.20.3 is the first full version that it affects. But the "Affects Version/s" box wouldn't accept anything but 1.20.6.

can confirm for 1.20.6, here is the test setup for those who want to replicate:

[media]

how to use the setup:
-load the world in 1.20.2 (version before the issue is introduced), press the button on the command block (its near the diamond block), wait till the game unfreezes from lag, check how much server is lagging behind in the console
-load the world in 1.20.6 (version after the issue is introduced), press the button, wait till the game unfreezes from lag and check how much server is lagging behind in the console, it should be roughly twice as much behind as the test in 1.20.2 was.

in my own testing this issue indeed made explosions lag about twice as much compared to how it was before the issue was introduced, exact amount of lag is in the attached screenshots.

[media]

[media]

It is important to note, that even though this issue is marked as related to two other reports, MC-238249 is about a completely different issue (the one related to block breaking and not entity interaction) and the MC-260762 just being a feature request asking to implement an optimization from a certain mod even though it is not vanilla-preserving (as in it breaks vanilla behavior thus breaking even more explosion related contraptions).
However this report (MC-272414), states that since 23w45a the entire exposure calculation is needlessly done twice which worsens the lag from it even more, this leads to tnt cannons becoming too laggy to use for many players. thus entire field of cannon and tnt related redstone suffers a lot, i (and many others from tnt tech community) really hope this issue gets fixed soon.

can confirm in 1.20.6

enbyd

billysjoberg

1242517

Community Consensus

Expansion B

Important

Performance

1.20.3, 1.20.6

24w33a

Retrieved