Theurgy

Theurgy

15M Downloads

[CRITICAL] Fermentation Vat Causes Linear Server Lag Scaling - 5ms per Vat from Global Recipe Scan

WitcherBB opened this issue · 4 comments

commented

Describe the bug
A significant server-side performance issue has been identified with the Fermentation Vat apparatus. Whenever a Fermentation Vat (fermentation vat) contains a complete recipe (two items and water), it induces approximately 5ms of server lag. This delay is cumulative; for each additional vat with a valid recipe, an extra ~5ms of lag is added to the server tick, leading to severe performance degradation and unplayable conditions on servers with multiple vats.

To Reproduce
Steps to reproduce the behavior:

  1. Place a single Fermentation Vat in the world.
  2. Insert the two required items and water to form a complete, valid recipe.
  3. Observe server performance metrics (F3 + 2). A ~5ms increase in tick time will be noticeable.
  4. Place a second Fermentation Vat with a valid recipe.
  5. The server tick delay will increase to ~10ms.
  6. Repeat step 4; the lag will continue to accumulate linearly.

Expected behavior
The act of a Fermentation Vat containing a valid recipe should have a negligible performance impact. The validation of a recipe should be an inexpensive operation, and the number of active vats in the world should not linearly scale server lag.

Screenshots
Image
Image

Root Cause Analysis
After reviewing the source code, the problem has been traced to src/main/java/com/klikli_dev/theurgy/content/apparatus/fermentationvat/FermentationCachedCheck.java, specifically at line 99.
The method at this location is incorrectly iterating over all recipes in the world to check for a match every time it needs to validate a single vat's contents. This is an O(n) operation per vat, and when performed for multiple vats per tick, it results in an O(n*m) complexity, which is the direct cause of the linear performance degradation.

System (please complete the following information):

  • Theurgy Version: 1.63.1
  • OS: Windows 11
  • Minecraft Version: 1.21.1
  • Modpack Link and Version, or list of mods: ATM 10

Additional context

  • This was identified on a integrated server environment.
  • The 5ms figure is an average; the exact time may vary based on the total number of recipes loaded.
commented

@WitcherBB thanks for the report!

I haven't had time to debug it yet, just dumping some thoughts. Suggestions welcome haha

The cached check is modeled after the vanilla cached check (modified to account for fluids), and should prevent this exact issue. An iteration over the recipes is unavoidable, but should only be done once before using the cached result. So either the wrong method gets called (bypassing the cache check every time), or the cache conditions are somehow not met

commented

@klikli-dev I built this project and ran it, but found that the issue couldn't be reproduced when only this mod was installed. It might be an issue with the mod pack.

commented

@WitcherBB thank you so much for checking that!
It would be interesting to find out which mod causes it in combination with theurgy. I would assume that there must be some mod that is trying to optimize recipes in some way and that causes issues

commented

@klikli-dev You're welcome.
I think so too. After I updated ATM 10 to version 4.10, this bug disappeared. However, when I checked the ATM 10 changelog, I couldn’t find any updates related to this bug.