promotional bannermobile promotional banner

TFMG Electricity Performance Patch

Fixes the per-tick electrical network rebuild loop and the quadratic network update in Create: The Factory Must Grow.

What this is

An unofficial performance patch for Create: The Factory Must Grow (TFMG) 1.2.0 on NeoForge 21.1.x / Minecraft 1.21.1.

Mixins only. TFMG itself is not modified or redistributed — no content, recipes, registries or worldgen are touched. Install it next to TFMG, remove it any time; nothing is written to your world.

The problem

If you have an electric motor on your grid, TFMG recalculates that entire electrical network 20 times per second, forever — even when nothing changes. And a single recalculation is written in a way that costs O(n²) in the size of the grid, so doubling your wiring quadruples the cost of every tick. That combination is why long power lines and electric motors tank TPS and FPS.

What it fixes

  1. Infinite network update loop on the electric motor. ElectricalNetwork.updateNetwork() notifies every member via onNetworkChanged(..). ElectricMotorBlockEntity answers that notification with updateNextTick(), which makes tickElectricity() call updateNetwork() again next tick, which notifies the motor again — forever. Every other consumer guards its reaction with a change check on voltage and power usage; the motor is the only one that reacts unconditionally. The redundant re-request is removed; delayedUpdate, notifyUpdate() and updateGeneratedRotation() behave exactly as before.
  2. Quadratic cost of a single network update. The last loop of updateNetwork() calls getCableCurrent(member) once per cable, and that method sums the current over all members — the same value every time, since it ignores its argument. It is now computed once per update and reused. Nothing in that loop changes voltages or resistances, so the reused value is identical to what the original code would produce. O(n²) → O(n). This matters even more with transformers and switches, because getCurrent() calls resistance(), and VoltageAlteringBlockEntity.resistance() is itself O(n) — the original update degrades towards O(n³).
  3. Empty accumulator: a second per-tick rebuild loop. AccumulatorBlockEntity.tick() calls updateNextTick() on every tick while the bank is empty, which is a completely normal state. Throttled to once per 20 ticks.
  4. Large switch rebuilding the whole graph on an overloaded grid. LargeSwitchBlockEntity.lazyTick() calls onPlaced() while notEnoughPower is set, which re-runs checkForLoops() and onConnected() — a recursive flood over the entire grid. Throttled to one attempt per 10 lazy ticks (~5 s).

Install

Drop the jar into mods/ alongside TFMG. Needed on both the server and the clients — the client builds electrical networks too and runs the same loop, so without the patch client-side you keep the FPS drop. displayTest is set to IGNORE_ALL_VERSION, so it can be installed on one side only without a mod-list mismatch.

If you update TFMG

The mixin config uses defaultRequire 1, so if a target method changes in a future TFMG version the game will fail to start with a clear Mixin error instead of silently doing nothing and bringing the lag back. If that happens, remove this patch.

Notes

Not affiliated with the TFMG author. Findings are from decompiling tfmg-1.2.0.jar; the intent is that these fixes end up upstream, at which point this patch becomes unnecessary.

The TFMG Electricity Performance Patch Team

profile avatar
Owner
  • 4
    Projects
  • 286
    Downloads

More from mzg