Skip to content

Commit 4598207

Browse files
committed
fix: delegate as much map processing to another thread unless it's the current map
1 parent c7fba34 commit 4598207

File tree

3 files changed

+49
-8
lines changed

3 files changed

+49
-8
lines changed

Framework/Intersect.Framework.Core/Threading/LockHelper.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
#if DIAGNOSTIC
12
using System.Diagnostics;
23
using System.Runtime.CompilerServices;
34
using Intersect.Logging;
5+
#endif
46

57
namespace Intersect.Threading;
68

@@ -18,24 +20,32 @@ public void Lock(Action work, string? debugInfo = default)
1820
return;
1921
}
2022

23+
#if DIAGNOSTIC
2124
DateTime takenAt = default;
25+
#endif
2226

2327
try
2428
{
29+
#if DIAGNOSTIC
2530
var waitStartedAt = DateTime.UtcNow;
31+
#endif
2632

2733
Monitor.Enter(_lock, ref _lockTaken);
2834

35+
#if DIAGNOSTIC
2936
var waitElapsed = DateTime.UtcNow - waitStartedAt;
3037

3138
DebugPrint($"[{name}] Waited for {waitElapsed.TotalMilliseconds}ms to acquire the lock [{debugInfo}]");
39+
#endif
3240

3341
if (!_lockTaken)
3442
{
3543
return;
3644
}
3745

46+
#if DIAGNOSTIC
3847
_takenAt = takenAt = DateTime.UtcNow;
48+
#endif
3949

4050
work();
4151
}
@@ -46,9 +56,11 @@ public void Lock(Action work, string? debugInfo = default)
4656
Monitor.Exit(_lock);
4757
_lockTaken = false;
4858

59+
#if DIAGNOSTIC
4960
var elapsedTaken = DateTime.UtcNow - takenAt;
5061

5162
DebugPrint($"[{name}] Held lock for {elapsedTaken.TotalMilliseconds}ms [{debugInfo}]");
63+
#endif
5264
}
5365
else
5466
{
@@ -68,6 +80,7 @@ public bool TryAcquireLock(string? debugInfo, out Reference lockHelperReference)
6880
{
6981
bool lockTaken = false;
7082

83+
#if DIAGNOSTIC
7184
DebugPrint($"TryAcquireLock from: {debugInfo}");
7285

7386
if (_lockTaken)
@@ -76,14 +89,17 @@ public bool TryAcquireLock(string? debugInfo, out Reference lockHelperReference)
7689
}
7790

7891
var waitStartedAt = DateTime.UtcNow;
92+
#endif
7993

8094
Monitor.Enter(_lock, ref lockTaken);
8195

96+
#if DIAGNOSTIC
8297
var waitElapsed = DateTime.UtcNow - waitStartedAt;
8398

8499
DebugPrint($"[{name}] Waited for {waitElapsed.TotalMilliseconds}ms to acquire the lock [{debugInfo}]");
85100

86101
_takenAt = DateTime.UtcNow;
102+
#endif
87103

88104
if (lockTaken)
89105
{
@@ -106,17 +122,19 @@ public bool ReleaseLock(string? debugInfo = default)
106122
Monitor.Exit(_lock);
107123
_lockTaken = false;
108124

125+
#if DIAGNOSTIC
109126
var elapsedTaken = DateTime.UtcNow - _takenAt;
110127

111128
DebugPrint($"[{name}] Held lock for {elapsedTaken.TotalMilliseconds}ms [{debugInfo}]");
129+
#endif
112130

113131
return true;
114132
}
115133

134+
#if DIAGNOSTIC
116135
[MethodImpl(MethodImplOptions.AggressiveInlining)]
117136
private static void DebugPrint(string message)
118137
{
119-
#if DEBUG
120138
// if (LegacyLogging.Logger is { } logger)
121139
// {
122140
// logger.Debug(message);
@@ -125,8 +143,8 @@ private static void DebugPrint(string message)
125143
// {
126144
// Console.WriteLine(message);
127145
// }
128-
#endif
129146
}
147+
#endif
130148

131149
public ref struct Reference(string name, object lockObject, DateTime takenAt, ref bool lockTaken) : IDisposable
132150
{
@@ -141,9 +159,11 @@ public void Dispose()
141159

142160
Monitor.Exit(lockObject);
143161

162+
#if DIAGNOSTIC
144163
var elapsedTaken = DateTime.UtcNow - takenAt;
145164

146165
DebugPrint($"[{name}] Held lock for {elapsedTaken.TotalMilliseconds}ms [from ref]");
166+
#endif
147167
}
148168
}
149169
}

Intersect.Client.Core/Networking/PacketHandler.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ internal static void HandleMap(MapPacket packet, bool skipSave = false)
316316
endGridXYCameraHolds = DateTime.UtcNow;
317317

318318
mapInstance.Autotiles.InitAutotiles(mapInstance.GenerateAutotileGrid());
319+
319320
endInitAutotiles = DateTime.UtcNow;
320321

321322
if (Globals.PendingEvents.TryGetValue(mapId, out var pendingEventsForMap))
@@ -375,8 +376,23 @@ internal static void HandleMap(MapPacket packet, bool skipSave = false)
375376
//MapPacket
376377
public void HandlePacket(IPacketSender packetSender, MapPacket packet)
377378
{
378-
HandleMap(packet);
379-
Player.FetchNewMaps();
379+
if (Globals.Me?.MapInstance is {} currentMap)
380+
{
381+
if (currentMap.Id != packet.MapId)
382+
{
383+
Task.Run(DoHandleMap);
384+
return;
385+
}
386+
}
387+
388+
DoHandleMap();
389+
return;
390+
391+
void DoHandleMap()
392+
{
393+
HandleMap(packet);
394+
Player.FetchNewMaps();
395+
}
380396
}
381397

382398
//PlayerEntityPacket

Intersect.Client.Core/Networking/PacketSender.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,15 @@ public static void SendNeedMap(params Guid[] mapIds)
9898

9999
SendNeedMap(cacheKeys.ToArray());
100100

101-
foreach (var cachedMap in loadedCachedMaps)
102-
{
103-
PacketHandler.HandleMap(cachedMap, skipSave: true);
104-
}
101+
Task.Run(
102+
() =>
103+
{
104+
foreach (var cachedMap in loadedCachedMaps)
105+
{
106+
PacketHandler.HandleMap(cachedMap, skipSave: true);
107+
}
108+
}
109+
);
105110
}
106111

107112
// public static void SendNeedMap(params Guid[] mapIds)

0 commit comments

Comments
 (0)