Private
Public Access
1
0

perf logging, cap catchup loops

This commit is contained in:
2026-03-29 09:41:08 +02:00
parent de86ddbeef
commit b8e8533699
9 changed files with 158 additions and 35 deletions

View File

@@ -0,0 +1,30 @@
using System.Diagnostics;
using System.Runtime.CompilerServices;
namespace OrekiWoofsBees.Common;
public static class DiagnosticsUtil
{
public static void StopAndLogTime<T>(
this Stopwatch stopwatch,
T instance,
double totalSecondsThreshold,
string? note = null,
[CallerMemberName] string? callerName = null) where T : IModEntity
{
stopwatch.Stop();
if (stopwatch.Elapsed.TotalSeconds >= totalSecondsThreshold)
instance.Mod?.Logger.Warning($"{typeof(T).Name}.{callerName} ({note}) took {stopwatch.Elapsed.TotalSeconds:F2}s");
}
public static void LogTime<T>(
this Stopwatch stopwatch,
T instance,
double totalSecondsThreshold,
string? note = null,
[CallerMemberName] string? callerName = null) where T : IModEntity
{
if (stopwatch.Elapsed.TotalSeconds >= totalSecondsThreshold)
instance.Mod?.Logger.Warning($"{typeof(T).Name}.{callerName} ({note}) took {stopwatch.Elapsed.TotalSeconds:F2}s");
}
}

View File

@@ -0,0 +1,8 @@
using Vintagestory.API.Common;
namespace OrekiWoofsBees.Common;
public interface IModEntity
{
public Mod? Mod { get; }
}

View File

@@ -13,7 +13,7 @@ namespace OrekiWoofsBees.Common;
/// Instead of each beehive scanning its radius every tick, this registry
/// incrementally scans blocks across all beehives to keep performance consistent
/// </summary>
public class PlantPositionRegistryModSystem2 : ModSystem, IPlantPositionRegistry
public class PlantPositionRegistryModSystem2 : ModSystem, IPlantPositionRegistry, IModEntity
{
private readonly static BlockPos staticBlockPos = new(0);
@@ -26,6 +26,7 @@ public class PlantPositionRegistryModSystem2 : ModSystem, IPlantPositionRegistry
private readonly Dictionary<StructVec3i, BeehiveScanCursor> beehives = [];
private readonly HashSet<StructVec3i> flowerPositions = [];
private readonly HashSet<StructVec3i> cropPositions = [];
private readonly Stopwatch stopwatch = new();
// blocks below this are skipped
private readonly Dictionary<(int X, int Z), int> soilFloorCache = [];
@@ -208,7 +209,7 @@ public class PlantPositionRegistryModSystem2 : ModSystem, IPlantPositionRegistry
if (Api is null)
return;
var stopwatch = Stopwatch.StartNew();
stopwatch.Restart();
if (beehives.Count == 0)
return;
@@ -279,11 +280,7 @@ public class PlantPositionRegistryModSystem2 : ModSystem, IPlantPositionRegistry
}
}
stopwatch.Stop();
if (stopwatch.Elapsed.TotalSeconds > 0.2)
{
Mod.Logger.Warning($"{nameof(PlantPositionRegistryModSystem2)} {nameof(OnTick)} took {stopwatch.Elapsed.TotalSeconds:F2}s (beehives: {beehives.Count}).");
}
stopwatch.StopAndLogTime(this, 0.01);
}
/// <summary>