Achtung2 icon indicating copy to clipboard operation
Achtung2 copied to clipboard

Fix FPS drops from excessive iterator work in forced job expansion and add immediate expansion mode

Open Copilot opened this issue 3 months ago • 14 comments

Fix for FPS Drops During Achtung Forced Jobs

Problem Summary

Users experienced significant FPS drops when using Achtung forced (⚡) jobs, particularly during harvesting or hauling operations.

Solution

Frame time budgeting - limiting work to 8ms per frame regardless of iteration count.

Immediate Expansion Mode

New optional setting (off by default) that fully expands forced work immediately when command is issued.

Behavior (This Commit)

  • First click (no SHIFT): Rebuild from scratch - clear and rebuild from initialTarget
  • SHIFT+click (adding to existing): Only ADD new targets, never clear existing ones
  • Not first job: Only ADD new targets, preserve other work
  • Dragging without SHIFT: Rebuild THIS job's targets (allows retraction)
  • Dragging with SHIFT: Only add, never remove

Uses existing Tools.IsModKeyPressed(AchtungModKey.Shift) for DRY code.

Implementation

public void ExpandJobImmediately(bool addOnly = false)
{
    // If not adding only, rebuild from scratch (allows retraction during drag)
    if (!addOnly)
    {
        targets.Clear();
        // Start with the initial target
        if (initialTarget.IsValid)
            targets.Add(new ForcedTarget(initialTarget, MaterialScore(initialTarget)));
    }
    
    // Expand using cellRadius...
}

// Called with:
forcedJob.ExpandJobImmediately(addOnly: !isFirstJob || isShiftHeld);

UI Changes

  • "Immediate work expansion" checkbox in main settings page below "Maximum forced items" slider

Impact

✅ Prevents FPS drops via frame time budgeting
✅ New immediate mode for instant results
✅ Correctly respects cellRadius
✅ Preserves existing work when SHIFT held or adding additional zones
✅ Uses existing utility methods (DRY)

Original prompt

I want you to analyse and comment on the following interaction iI had with a Achtung mod user:

Rimworld — v1.6.4633 rev1260. Achtung — latest from Steam (v4.1.8?)

At some point after I added Achtung to my modlist, the ⚡ jobs suddenly started causing significant FPS drops due to extremely long calls / a lot of calls of IEnumerator:MoveNext inside AchtungMod.Game_UpdatePlay_Patch:Postfix. So far I saw two manifestation of this:

I. Either FPS straight plummets, and MoveNext timings spike constantly: image.png (view on web) image.png (view on web) II. Or FPS slowly crawls from 75 down for some time, then abruptly restores back, and the process repeats, as MoveNext timings get worse and then reset: image.png (view on web) image.png (view on web) The resets in this second case appear to relate with the if (fps < minFps) iterations = 0 part in Main.cs.

First time I encountered it after my pawns were just done ⚡-hauling a lot of stuff into "Adaptive Primitive Storage"'s huge cellar – I thought if maybe the achtung marks somehow survived being hauled and were glitching because of a storage unit allowing for 50+ items in a single cell. The issue persisted through multiple reloads, restarts, and even removing "Achtung" from the modlist, re-saving the game, and enabling it back on – I was still getting the cyclic FPS drops from the second case right after a reload. Pretty sure nobody had any prioritized jobs at the moment, and the issue just went away on its own after an hour of playing.

Now however it happens quite often if f.x. I assign someone ⚡-harvest crops in the field. I got a savefile with my original modlist of 300+ mods where it happens immediately if I order a pawn to ⚡-harvest.

I am able to reproduce the second case issue on a dev quickstart – spawn a bunch of small things in neighboring cells, assign a stockpile, and then order a pawn to ⚡-haul the things. Although the graph looks different from my main colony, with bigger spikes in the midst of the cycle.

image.png (view on web) I cannot easily reproduce it when doing harvesting on a quicktest, though – the graph looks completely different:

image.png (view on web) If only Harmony, Analyzer and Achtung are loaded, things seem fine and I do not get FPS drops neither on hauling nor on harvesting. The performance of MoveNext slightly worsens over time after the ⚡ is pressed, and there are suspicious repeating spikes up to 10ms, but the FPS is stable otherwise.

image.png (view on web) image.png (view on web) If I add "Pick Up And Haul" to the mix, the performance of hauling specifically gets straight out worse, but the cycling behavior still does not occur:

image.png (view on web) Let me know if you need me to do any more digging on my side. In the meantime – huge thanks for Harmony and all the mods

I wrote back:

What you are seeing is that some mods are not optimising their discovery methods. Jobs work by a mechanism where the game calls into different registered abilities to see if a job can be done and then issues the job. Achtung does its job by doing what vanilla does or else it would need to support every single job related mod out there which is not possible.

There is absolutely nothing I can do in Achtung that would make this faster because that’s how the game mechanics works.

Sorry this is not the first time this kind of problem is brought up with people using Achtung.

And they answered:

Thank you for the explanation! But I'm curious – why does the slowdown happen in the Updates and not Ticks?

I would not be surprised if the ticks were getting slower when a lot of Achtung jobs exist. Or if there was a spike every time I hit ⚡, or a spike every time an ⚡ed job completes (crop harvested) and a new cell get selected. But from the graph it seems as if the very traversing of IEnumerator gets slower – and also it does not seem like lowering the Maximum forced items in settings affect anything...


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot avatar Nov 21 '25 16:11 Copilot