perf(grpc-data): #1057 trim the projected publish path's per-value allocation; allocation-free catalog/subject resolve
Measured with GrpcDataWave3PerfTests (10k-item cohort, 100 source versions at 1k/s; numbers in the issue report).
- Core: MappedSubjectResolver.Resolve returns shared immutable resolutions (built once per subject) and compares
scope/ancestry by index (no MappedSubjectResolution + 2 enumerators per call, several calls per value);
DataOperationPolicy checks segments by index and builds its local scope key with one exact-length string.Create.
- Origin: LocalExportCatalog.TryResolve compares the view field by field instead of building 'view with { Address }'
(and LocalAddress no longer copies an exactly-spelled address); the frame Eligible check peeks the latest READY
version (PartitionedSampleHub.TryPeekReadyVersion) instead of retaining a second sample per value; TickAsync reuses
worker-owned lists (interest groups, changes via SourceInterestLease.TryReadChanges(into), delivery slice via
DeliveryScheduler.TakeSlice(into), due items) and the delivery index reuses one ready-list node per source.
- Consumer: PublishPacedAsync is a ValueTask; the cohort and RemoteSubscription walk frame items by index; renewal
prunes expired witnesses in place.
This commit is contained in:
@@ -122,7 +122,7 @@ public sealed class DataOperationPolicy
|
||||
if (scope is null || caller is null || !snapshot.HasArtifact || string.IsNullOrWhiteSpace(snapshot.ClusterId)
|
||||
|| !string.Equals(scope.ClusterId, snapshot.ClusterId, StringComparison.OrdinalIgnoreCase)
|
||||
|| !Enum.IsDefined(operation) || (scope.Kind != NodeHierarchyKind.Raw && scope.Kind != NodeHierarchyKind.Uns)
|
||||
|| scope.Segments is null || scope.Segments.Any(string.IsNullOrEmpty)
|
||||
|| scope.Segments is null || HasEmptySegment(scope.Segments)
|
||||
|| !CredentialUsable(snapshot, caller, allowLegacyUnversionedKey)) return false;
|
||||
|
||||
var key = new DataAuthorizationCacheKey(snapshot.GenerationId, caller, operation,
|
||||
@@ -141,12 +141,46 @@ public sealed class DataOperationPolicy
|
||||
return allowed && ReferenceEquals(snapshot, _authority.Current) && CredentialUsable(snapshot, caller, allowLegacyUnversionedKey);
|
||||
}
|
||||
|
||||
private static string LocalScopeKey(NodeScope scope)
|
||||
// #1057: indexed — Any() over an IReadOnlyList allocated an enumerator on every authorization (several per projected value).
|
||||
private static bool HasEmptySegment(IReadOnlyList<string> segments)
|
||||
{
|
||||
var key = new StringBuilder().Append((int)scope.Kind).Append(':').Append(scope.ClusterId.Length)
|
||||
.Append(':').Append(scope.ClusterId);
|
||||
foreach (var segment in scope.Segments) key.Append(':').Append(segment.Length).Append(':').Append(segment);
|
||||
return key.ToString();
|
||||
for (var i = 0; i < segments.Count; i++)
|
||||
if (string.IsNullOrEmpty(segments[i])) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
// "{kind}:{len}:{cluster}" then ":{len}:{segment}" per segment. #1057: built in one exact-length string (no StringBuilder and
|
||||
// its buffers) — the origin's publish verdict asks for it per subscribed item about once a second.
|
||||
internal static string LocalScopeKey(NodeScope scope)
|
||||
{
|
||||
var segments = scope.Segments;
|
||||
var length = Digits((int)scope.Kind) + 1 + Digits(scope.ClusterId.Length) + 1 + scope.ClusterId.Length;
|
||||
for (var i = 0; i < segments.Count; i++) length = checked(length + 2 + Digits(segments[i].Length) + segments[i].Length);
|
||||
return string.Create(length, scope, static (span, scope) =>
|
||||
{
|
||||
var position = 0;
|
||||
Write(span, ref position, (int)scope.Kind); span[position++] = ':';
|
||||
Write(span, ref position, scope.ClusterId.Length); span[position++] = ':';
|
||||
scope.ClusterId.CopyTo(span[position..]); position += scope.ClusterId.Length;
|
||||
var segments = scope.Segments;
|
||||
for (var i = 0; i < segments.Count; i++)
|
||||
{
|
||||
span[position++] = ':'; Write(span, ref position, segments[i].Length); span[position++] = ':';
|
||||
segments[i].CopyTo(span[position..]); position += segments[i].Length;
|
||||
}
|
||||
});
|
||||
static int Digits(int value)
|
||||
{
|
||||
if (value < 0) return value.ToString(System.Globalization.CultureInfo.InvariantCulture).Length;
|
||||
var digits = 1;
|
||||
while (value >= 10) { value /= 10; digits++; }
|
||||
return digits;
|
||||
}
|
||||
static void Write(Span<char> span, ref int position, int value)
|
||||
{
|
||||
value.TryFormat(span[position..], out var written, default, System.Globalization.CultureInfo.InvariantCulture);
|
||||
position += written;
|
||||
}
|
||||
}
|
||||
|
||||
private bool Walk(DataPlaneAuthoritySnapshot snapshot, DataCallerIdentity caller, OpcUaOperation operation, NodeScope scope)
|
||||
|
||||
@@ -5,8 +5,12 @@ namespace ZB.MOM.WW.OtOpcUa.Core.Authorization;
|
||||
/// <summary>Immutable, generation-bound projection of deployed mapped subjects.</summary>
|
||||
public sealed class MappedSubjectResolver
|
||||
{
|
||||
private readonly Dictionary<DataView, MappedNamespaceSubject?> _subjects = new();
|
||||
// #1057: each subject's Mapped resolution is built once here; Resolve runs per projected value (several times) and allocated
|
||||
// a result record plus two sequence enumerators per call.
|
||||
private readonly Dictionary<DataView, MappedSubjectResolution?> _subjects = new();
|
||||
private readonly long _catalogEpoch;
|
||||
private static readonly MappedSubjectResolution Deny = new(MappedSubjectResolutionKind.Deny);
|
||||
private static readonly MappedSubjectResolution Local = new(MappedSubjectResolutionKind.Local);
|
||||
|
||||
/// <summary>The subjects must come from the trusted deployed catalog, never a request.</summary>
|
||||
public MappedSubjectResolver(IEnumerable<MappedNamespaceSubject> authoritativeSubjects, long catalogEpoch,
|
||||
@@ -18,30 +22,33 @@ public sealed class MappedSubjectResolver
|
||||
foreach (var subject in authoritativeSubjects)
|
||||
{
|
||||
var copy = Copy(subject);
|
||||
if (!_subjects.TryAdd(copy.View, copy)) _subjects[copy.View] = null;
|
||||
if (!_subjects.TryAdd(copy.View, new(MappedSubjectResolutionKind.Mapped, copy))) _subjects[copy.View] = null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The trusted local activation captured alongside this immutable projection.</summary>
|
||||
public IDataMappedCatalogAuthority LocalAuthority { get; }
|
||||
|
||||
/// <summary>Compares request and live evidence before any cached grant or local fallback is used.</summary>
|
||||
/// <summary>
|
||||
/// Compares request and live evidence before any cached grant or local fallback is used. Allocation-free (#1057): the
|
||||
/// returned resolutions are shared, immutable instances.
|
||||
/// </summary>
|
||||
public MappedSubjectResolution Resolve(DataView requested, MappedNamespaceSubject? live)
|
||||
{
|
||||
if (requested is null) return new(MappedSubjectResolutionKind.Deny);
|
||||
if (requested is null) return Deny;
|
||||
if (live is null)
|
||||
return requested.MappingId is null ? new(MappedSubjectResolutionKind.Local) : new(MappedSubjectResolutionKind.Deny);
|
||||
return requested.MappingId is null ? Local : Deny;
|
||||
if (requested.MappingId is null || _catalogEpoch <= 0 || live.Serving.CatalogEpoch != _catalogEpoch
|
||||
|| !LocalAuthority.IsActive || LocalAuthority.CatalogEpoch <= 0 || LocalAuthority.AuthorityEpoch <= 0)
|
||||
return new(MappedSubjectResolutionKind.Deny);
|
||||
return Deny;
|
||||
|
||||
if (!_subjects.TryGetValue(requested, out var found) || found is null
|
||||
if (!_subjects.TryGetValue(requested, out var resolution) || resolution?.Subject is not { } found
|
||||
|| found.Serving.CatalogEpoch != _catalogEpoch || !Matches(found, live)
|
||||
|| found.View.MappingRevision <= 0 || string.IsNullOrWhiteSpace(found.View.MappingId)
|
||||
|| string.IsNullOrWhiteSpace(found.View.Subject)
|
||||
|| found.View.Realm != (found.Scope.Kind == NodeHierarchyKind.Raw ? DataRealm.Raw : DataRealm.Uns))
|
||||
return new(MappedSubjectResolutionKind.Deny);
|
||||
return new(MappedSubjectResolutionKind.Mapped, found);
|
||||
return Deny;
|
||||
return resolution;
|
||||
}
|
||||
|
||||
private static bool Matches(MappedNamespaceSubject x, MappedNamespaceSubject y) =>
|
||||
@@ -49,8 +56,18 @@ public sealed class MappedSubjectResolver
|
||||
&& x.WriteThrough == y.WriteThrough
|
||||
&& string.Equals(x.Scope.ClusterId, y.Scope.ClusterId, StringComparison.OrdinalIgnoreCase)
|
||||
&& x.Scope.Kind == y.Scope.Kind
|
||||
&& x.Scope.Segments.SequenceEqual(y.Scope.Segments, StringComparer.Ordinal)
|
||||
&& x.LocalAncestry.SequenceEqual(y.LocalAncestry, StringComparer.OrdinalIgnoreCase);
|
||||
&& SameSequence(x.Scope.Segments, y.Scope.Segments, StringComparer.Ordinal)
|
||||
&& SameSequence(x.LocalAncestry, y.LocalAncestry, StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
// Indexed: SequenceEqual over IReadOnlyList allocated an enumerator per side.
|
||||
private static bool SameSequence(IReadOnlyList<string> x, IReadOnlyList<string> y, StringComparer comparer)
|
||||
{
|
||||
if (ReferenceEquals(x, y)) return true;
|
||||
if (x is null || y is null || x.Count != y.Count) return false;
|
||||
for (var i = 0; i < x.Count; i++)
|
||||
if (!comparer.Equals(x[i], y[i])) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
private static MappedNamespaceSubject Copy(MappedNamespaceSubject subject) => subject with
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user