TeamWizardry / TMT-Refraction

A light manipulation based mod for The Modding Trials
MIT License
3 stars 4 forks source link

Lasers are causing some nasty TPS lag #64

Open TheDeviantCrafter opened 5 years ago

TheDeviantCrafter commented 5 years ago

I have roughly 100 lasers setup in a test world, and this has brought the TPS down to a beautiful 2.2. Using Lag Goggles, it shows that each laser is using around 1,500 microseconds of CPU time per tick.

This affects both creative and survival lasers. The amount of lag created depends on the complexity of the laser's path. A simple laser traveling for one block and then harmlessly hitting a block only uses 186 microseconds per tick.

Lag Goggles screenshots: 2019-01-12_14 03 05 2019-01-12_14 03 13

Latest.log: https://gist.github.com/TheDeviantCrafter/fd8ea96e4797be6900d1570c9929a567

Nonsanity commented 5 years ago

I'm seeing the same thing, even with fewer beams. My main test case was just one source, split through a prism and extending off into the distance with nothing blocking them. Adding the disco ball to one of the beams cause the client session to freeze completely. There's both an impact to the TPS and a possible continuously-growing memory requirement and/or a heavy allocation and release of memory causing frequent garbage collection delays. At least, that's what it feels like. The single-player client starts to pause at reducing intervals as soon as the beams are aimed at the horizon.

If there are checks going on every tick at every point along a beam's length, that would be a problem. It such a case, I'd suggest adding a config option to limit the length of the beams. It's not necessarily realistic, but call it "sudden and catastrophic atmospheric absorption." :)

TheDeviantCrafter commented 5 years ago

I did all of my testing with Lag Goggles. I found that the lag caused by the laser block increases based on how complex the laser beam's path is.

Each object the laser interacts with increases the TPS lag. I suspect it's recalculating the laser's path every tick.

I see two solutions to this:

  1. Make sure the mod isn't recalculating everything every tick. If no new blocks are placed that cross the path of the laser, it doesn't need to recalculate the angle.

  2. Expanding on Nonsanity's idea, add a configurable range limit. Each block that the laser interacts with (mirrors, prisms, etc) counts against this range. A mirror might reduce this range by 20, a fiber optic cable by 10, etc. Adjust these numbers so people avoid the laggiest blocks where possible.