RunUnsynced, do not recalculate sync hash on reentry. Cache debug settings.

This commit is contained in:
Vapre
2021-08-13 20:19:40 +02:00
committed by teinarss
parent b3159d7515
commit 5ae4662f08
7 changed files with 29 additions and 18 deletions

View File

@@ -161,6 +161,11 @@ namespace OpenRA
return t.GetHashCode();
}
public static void RunUnsynced(World world, Action fn)
{
RunUnsynced(Game.Settings.Debug.SyncCheckUnsyncedCode, world, () => { fn(); return true; });
}
public static void RunUnsynced(bool checkSyncHash, World world, Action fn)
{
RunUnsynced(checkSyncHash, world, () => { fn(); return true; });
@@ -168,13 +173,18 @@ namespace OpenRA
static bool inUnsyncedCode = false;
public static T RunUnsynced<T>(World world, Func<T> fn)
{
return RunUnsynced(Game.Settings.Debug.SyncCheckUnsyncedCode, world, fn);
}
public static T RunUnsynced<T>(bool checkSyncHash, World world, Func<T> fn)
{
if (world == null)
// PERF: Detect sync changes in top level entry point only. Do not recalculate sync hash during reentry.
if (inUnsyncedCode || world == null)
return fn();
var sync = checkSyncHash ? world.SyncHash() : 0;
var prevInUnsyncedCode = inUnsyncedCode;
inUnsyncedCode = true;
try
@@ -183,7 +193,7 @@ namespace OpenRA
}
finally
{
inUnsyncedCode = prevInUnsyncedCode;
inUnsyncedCode = false;
if (checkSyncHash && sync != world.SyncHash())
throw new InvalidOperationException("RunUnsynced: sync-changing code may not run here");
}