feat(vtag): passthrough fast-path skips Roslyn for mirror scripts (A)

This commit is contained in:
Joseph Doherty
2026-06-07 15:26:20 -04:00
parent 3834400f05
commit 08d7477860
5 changed files with 256 additions and 3 deletions
@@ -0,0 +1,29 @@
namespace ZB.MOM.WW.OtOpcUa.Core.Scripting;
using System.Text.RegularExpressions;
/// <summary>
/// Classifies the trivial "mirror" VirtualTag script shape <c>return ctx.GetTag("X").Value;</c>,
/// which can be evaluated by returning the dependency value directly — no Roslyn compilation.
/// Narrow, exact pattern: any near-miss returns false and falls through to the Roslyn path.
/// </summary>
public static partial class PassthroughScript
{
// ^ \s* return \s+ ctx . GetTag ( "X" ) . Value ; \s* $ (whitespace-tolerant around tokens)
[GeneratedRegex(@"^\s*return\s+ctx\s*\.\s*GetTag\s*\(\s*""([^""]+)""\s*\)\s*\.\s*Value\s*;\s*$")]
private static partial Regex MirrorRegex();
/// <summary>True if <paramref name="source"/> is the mirror passthrough shape; outputs the referenced tag.</summary>
/// <param name="source">The VirtualTag script source to classify.</param>
/// <param name="tagRef">On success, the tag reference captured from the mirror shape; otherwise empty.</param>
/// <returns><see langword="true"/> if the source is the mirror passthrough shape; otherwise <see langword="false"/>.</returns>
public static bool TryMatch(string? source, out string tagRef)
{
tagRef = string.Empty;
if (string.IsNullOrEmpty(source)) return false;
var m = MirrorRegex().Match(source);
if (!m.Success) return false;
tagRef = m.Groups[1].Value;
return true;
}
}
@@ -50,6 +50,18 @@ public sealed class RoslynVirtualTagEvaluator : IVirtualTagEvaluator, IDisposabl
if (_disposed) return VirtualTagEvalResult.Failure("evaluator disposed");
if (string.IsNullOrWhiteSpace(expression)) return VirtualTagEvalResult.Failure("empty expression");
// A — passthrough fast-path: the mirror shape `return ctx.GetTag("X").Value;` needs no
// Roslyn. Narrow exact pattern; near-misses fall through to the compiled path below.
// Semantics are byte-identical to the compiled mirror: a present dependency returns its
// value (Ok), and an absent one makes GetTag yield a Bad snapshot whose .Value is null,
// so the script returns null — Ok(null) here too.
if (PassthroughScript.TryMatch(expression, out var passthroughRef))
{
return dependencies.TryGetValue(passthroughRef, out var ptValue)
? VirtualTagEvalResult.Ok(ptValue)
: VirtualTagEvalResult.Ok(null);
}
ScriptEvaluator<VirtualTagContext, object?> evaluator;
try
{