performance: reduce LINQ allocations in UpdateInstanceControllers#2811
Closed
performance: reduce LINQ allocations in UpdateInstanceControllers#2811
Conversation
Replace LINQ-heavy hot paths in InstanceProcessor that were executing every tick with zero-allocation equivalents. Changes: - Add a static `_activeIdScratch` HashSet reused across calls to CleanupOrphanedControllers, replacing the LINQ .Except().ToArray() chain that allocated on every invocation. - Change CleanupOrphanedControllers signature from IEnumerable<Guid> to MapInstance[] so the caller's already-available array is used directly, avoiding the intermediate .Select(map => map.MapInstanceId) projection. - Replaces GroupBy().ToDictionary().ToArray() in UpdateInstanceControllers with a manual Dictionary<Guid, List<MapInstance>> build loop, removing three allocating LINQ operators from the 250 ms update path. - Use lazy-init (toRemove ??= new List<Guid>()) so the removal list is only allocated when at least one orphaned controller is actually found. - Mark InstanceControllers as readonly to prevent accidental reassignment. No behaviour changes; purely allocation and GC pressure reduction. Signed-off-by: Arufonsu <17498701+Arufonsu@users.noreply.github.com>
pandinocoder
requested changes
Feb 22, 2026
| .GroupBy(m => m.MapInstanceId) | ||
| .ToDictionary(m => m.Key, m => m.ToArray()); | ||
| // Manual grouping + ToDictionary allocations | ||
| var mapsAndInstances = new Dictionary<Guid, List<MapInstance>>(); |
Member
There was a problem hiding this comment.
Prefer CollectionType name = []; syntax when available
| var map = activeMaps[i]; | ||
| if (!mapsAndInstances.TryGetValue(map.MapInstanceId, out var list)) | ||
| { | ||
| mapsAndInstances[map.MapInstanceId] = list = new List<MapInstance>(); |
Member
There was a problem hiding this comment.
Prefer collectionName = []; syntax when available
|
|
||
| if (!ActiveIdScratch.Contains(id)) | ||
| { | ||
| (toRemove ??= new List<Guid>()).Add(id); |
Member
There was a problem hiding this comment.
Prefer = []
Separate the assignment and .Add() into separate lines
| } | ||
|
|
||
| List<Guid>? toRemove = null; | ||
| foreach (var id in InstanceControllers.Keys) |
| .Except(activeInstanceIds) | ||
| .Except(new Guid[1] { default }) // Never cleanup the overworld instance | ||
| .ToArray(); | ||
| ActiveIdScratch.Clear(); |
Member
There was a problem hiding this comment.
The Clear() would most likely end up destroying the backing buffer, I'm not sure there's a point in a static variable for this.
| foreach (var id in toRemove) | ||
| { | ||
| InstanceControllers.Remove(id); | ||
| ApplicationContext.Context.Value?.Logger.LogDebug($"Removing instance controller {id}"); |
Member
There was a problem hiding this comment.
I don't remember anymore what the correct code is but ApplicationContext.Context.Value?.Logger should be able to be shortened to something, maybe ApplicationContext.CurrentContext.Logger? This should be present elsewhere in the repo as well.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Replace LINQ-heavy calls in InstanceProcessor that were executing every tick with zero-allocation equivalents.
Changes:
_activeIdScratchHashSet reused across calls to CleanupOrphanedControllers, replacing the LINQ .Except().ToArray() chain that allocated on every invocation.No behaviour changes; purely allocation and GC pressure reduction.