Skip to content

performance: reduce LINQ allocations in UpdateInstanceControllers#2811

Closed
Arufonsu wants to merge 1 commit intomainfrom
performance/instance-processor
Closed

performance: reduce LINQ allocations in UpdateInstanceControllers#2811
Arufonsu wants to merge 1 commit intomainfrom
performance/instance-processor

Conversation

@Arufonsu
Copy link
Contributor

Replace LINQ-heavy calls 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 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> build loop, removing three allocating LINQ operators from the 250 ms update path.
  • Use lazy-init (toRemove ??= new List()) 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.

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>
@Arufonsu Arufonsu requested review from a team February 22, 2026 00:37
@Arufonsu Arufonsu added chore Cleans up code, documentation or project structure without altering functionality performance Performance optimization labels 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>>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer collectionName = []; syntax when available


if (!ActiveIdScratch.Contains(id))
{
(toRemove ??= new List<Guid>()).Add(id);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer = []
Separate the assignment and .Add() into separate lines

}

List<Guid>? toRemove = null;
foreach (var id in InstanceControllers.Keys)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not race condition safe?

.Except(activeInstanceIds)
.Except(new Guid[1] { default }) // Never cleanup the overworld instance
.ToArray();
ActiveIdScratch.Clear();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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}");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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

Labels

ai slop chore Cleans up code, documentation or project structure without altering functionality performance Performance optimization

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants