Skip to content

⚡ Bolt: Replace expensive FindObjectsOfType with Rack cache#21

Open
mleem97 wants to merge 1 commit into
mainfrom
bolt-optimize-rack-count-292053595981774687
Open

⚡ Bolt: Replace expensive FindObjectsOfType with Rack cache#21
mleem97 wants to merge 1 commit into
mainfrom
bolt-optimize-rack-count-292053595981774687

Conversation

@mleem97

@mleem97 mleem97 commented May 4, 2026

Copy link
Copy Markdown
Owner

💡 What:
Replaces the expensive UnityEngine.Object.FindObjectsOfType<Rack>() call across the codebase with an O(1) thread-safe static cache in RackPatch. The cache tracks active Racks by hooking into their Awake and OnDestroy lifecycle methods via Harmony.

🎯 Why:
FindObjectsOfType is an O(N) operation that traverses every active GameObject in the scene. In late-game saves with thousands of objects, frequent API or Lua polling of Rack counts or data causes significant main thread hitches and severe Garbage Collection pressure due to array allocation.

📊 Impact:

  • Reduces GC allocations to 0 bytes for count queries.
  • Turns O(N) scene traversal into O(1) cached lookup.
  • Prevents main-thread stutter when API/Lua scripts query Rack info frequently.

🔬 Measurement:

  • Use a Unity profiler or MelonLogger stopwatch around GregAPI.GetRackCount() or greg.rack.count(). You will see time drop from several milliseconds (in large saves) to negligible nanoseconds.
  • Build compiles and tests succeed (dotnet build gregCore.csproj -c Release).

PR created automatically by Jules for task 292053595981774687 started by @mleem97

Replaces the expensive O(N) `UnityEngine.Object.FindObjectsOfType<Rack>()` call which blocks the main thread and causes severe GC pressure in Unity/IL2CPP with an O(1) thread-safe static cache populated via Harmony `Awake` and `OnDestroy` patches. This eliminates major performance hitches when counting or querying Racks in the API and Lua scripts.
@google-labs-jules

Copy link
Copy Markdown

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant