Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 43 additions & 20 deletions Intersect.Server.Core/Core/MapInstancing/InstanceProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,44 @@
using Microsoft.Extensions.Logging;

namespace Intersect.Server.Core.MapInstancing;

public static class InstanceProcessor
{
private static Dictionary<Guid, InstanceController> InstanceControllers = new();
private static readonly Dictionary<Guid, InstanceController> InstanceControllers = new();

public static Guid[] CurrentControllers => InstanceControllers.Keys.ToArray();
private static readonly HashSet<Guid> ActiveIdScratch = [];

public static bool TryGetInstanceController(Guid instanceId, out InstanceController controller)
{
return InstanceControllers.TryGetValue(instanceId, out controller);
}
public static bool TryGetInstanceController(Guid instanceId, out InstanceController controller) => InstanceControllers.TryGetValue(instanceId, out controller);

private static void CleanupOrphanedControllers(IEnumerable<Guid> activeInstanceIds)
private static void CleanupOrphanedControllers(MapInstance[] activeMaps)
{
var processingInstances = InstanceControllers.Keys
.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.

for (var i = 0; i < activeMaps.Length; i++)
{
ActiveIdScratch.Add(activeMaps[i].MapInstanceId);
}

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?

{
if (id == default)
{
continue; // never clean overworld
}

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

}
}

foreach (var id in processingInstances)
if (toRemove != null)
{
InstanceControllers.Remove(id);
ApplicationContext.Context.Value?.Logger.LogDebug($"Removing instance controller {id}");
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.

}
}
}

Expand All @@ -48,14 +64,21 @@ public static void UpdateInstanceControllers(MapInstance[] activeMaps)
return;
}

// Cleanup inactive instances
CleanupOrphanedControllers(activeMaps.Select(map => map.MapInstanceId));
CleanupOrphanedControllers(activeMaps);

Dictionary<Guid, MapInstance[]> mapsAndInstances = activeMaps
.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

for (var i = 0; i < activeMaps.Length; i++)
{
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

}

list.Add(map);
}

// For each instance...
foreach (var (instanceId, mapsInInstance) in mapsAndInstances)
{
// Fetch our instance controller...
Expand Down
Loading