fix(scripted-alarms): #995 K-3 a deploy reuses unchanged alarm predicates and compiles new ones outside the engine gate
Every LoadAsync cleared the compile cache and recompiled every predicate serially while holding _evalGate (and, uncontended, on the ScriptedAlarmHostActor thread). Now the compile phase runs first on the thread pool (bounded parallelism) without the gate, reusing source-hash-cached compiles; only the swap holds the gate, and CompiledScriptCache.RetainOnly disposes just the retired predicates' ALCs. Out of process the host's validation compile is remembered per source too. Measured, 500 alarms (distinct predicates, in process, Release): reload of unchanged defs 1303 ms -> 15 ms; one edited 1040 -> 16 ms; all-new reload 1002 -> 748 ms; ack issued during that reload waited 800 -> 1 ms.
This commit is contained in:
@@ -44,6 +44,7 @@ public sealed class CompiledScriptCache<TContext, TResult> : IDisposable
|
||||
{
|
||||
private readonly ConcurrentDictionary<string, Lazy<ScriptEvaluator<TContext, TResult>>> _cache = new();
|
||||
private bool _disposed;
|
||||
private long _compiles;
|
||||
|
||||
/// <summary>
|
||||
/// Return the compiled evaluator for <paramref name="scriptSource"/>, compiling
|
||||
@@ -61,7 +62,11 @@ public sealed class CompiledScriptCache<TContext, TResult> : IDisposable
|
||||
|
||||
var key = HashSource(scriptSource);
|
||||
var lazy = _cache.GetOrAdd(key, _ => new Lazy<ScriptEvaluator<TContext, TResult>>(
|
||||
() => ScriptEvaluator<TContext, TResult>.Compile(scriptSource),
|
||||
() =>
|
||||
{
|
||||
Interlocked.Increment(ref _compiles);
|
||||
return ScriptEvaluator<TContext, TResult>.Compile(scriptSource);
|
||||
},
|
||||
LazyThreadSafetyMode.ExecutionAndPublication));
|
||||
|
||||
try
|
||||
@@ -84,6 +89,41 @@ public sealed class CompiledScriptCache<TContext, TResult> : IDisposable
|
||||
/// <summary>Current entry count. Exposed for Admin UI diagnostics / tests.</summary>
|
||||
public int Count => _cache.Count;
|
||||
|
||||
/// <summary>
|
||||
/// Number of Roslyn compiles this cache has started (cache misses, failed compiles included). Exposed for
|
||||
/// diagnostics and tests — #995 K-3 pins that a reload of unchanged sources compiles nothing.
|
||||
/// </summary>
|
||||
public long CompileCount => Interlocked.Read(ref _compiles);
|
||||
|
||||
/// <summary>
|
||||
/// #995 K-3: drop (and dispose) every cached compile whose source is NOT in <paramref name="keep"/>, leaving the
|
||||
/// kept entries — and their loaded evaluators — untouched. This is the publish-replace path for an engine that
|
||||
/// reuses unchanged scripts across deploys: <see cref="Clear"/> would throw away every compile and force the
|
||||
/// next generation to recompile sources it already had.
|
||||
/// </summary>
|
||||
/// <param name="keep">The sources the new generation references.</param>
|
||||
/// <returns>How many entries were removed.</returns>
|
||||
public int RetainOnly(IEnumerable<string> keep)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(keep);
|
||||
var keepKeys = new HashSet<string>(StringComparer.Ordinal);
|
||||
foreach (var source in keep)
|
||||
if (source is not null) keepKeys.Add(HashSource(source));
|
||||
|
||||
var removed = 0;
|
||||
// Value-scoped removal, as in Clear: a concurrent GetOrCompile that re-added the key keeps its fresh entry.
|
||||
foreach (var entry in _cache.ToArray())
|
||||
{
|
||||
if (keepKeys.Contains(entry.Key)) continue;
|
||||
if (_cache.TryRemove(entry))
|
||||
{
|
||||
DisposeLazyIfMaterialised(entry.Value);
|
||||
removed++;
|
||||
}
|
||||
}
|
||||
return removed;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Drop every cached compile. Used on config generation publish + tests.
|
||||
/// Disposes each materialised evaluator before removing it so its collectible
|
||||
|
||||
Reference in New Issue
Block a user