perf(mqtt): wildcard topic trie and a shared strict UTF-8 decoder (R06:M-6, M-7)
Wildcard-authored tags were matched by one MqttTopicFilterComparer call per tag per inbound message on the dispatcher thread. They now live in an allocation-free level trie whose candidates are confirmed by the same comparer (semantics unchanged; pinned differentially against the comparer). TryDecodeUtf8 reuses one static throw-on-invalid UTF8Encoding instead of allocating one per Raw/Scalar delivery. Refs #744 Claude-Session: https://claude.ai/code/session_01Ub9hGmYLQvn9XETzw7CMsT
This commit is contained in:
@@ -48,7 +48,8 @@ namespace ZB.MOM.WW.OtOpcUa.Driver.Mqtt;
|
||||
/// accepted by the parser (warned at deploy, see <c>MqttTagDefinitionFactory.Inspect</c>), so
|
||||
/// matching uses MQTTnet's own <see cref="MqttTopicFilterComparer"/> rather than string
|
||||
/// equality — the same comparer a broker applies. Concrete topics stay a single dictionary
|
||||
/// lookup; the linear wildcard scan runs only when a wildcard tag actually exists.
|
||||
/// lookup; wildcard filters live in a level trie (<c>TopicFilterTrie</c>) whose candidates the comparer
|
||||
/// confirms, so a thousand wildcard tags cost a walk over the topic's levels, not a thousand compares.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// <b>Sparkplug seam.</b> P1 ingests plain topics only. Sparkplug B decodes a
|
||||
@@ -74,6 +75,13 @@ public sealed class MqttSubscriptionManager
|
||||
/// </summary>
|
||||
public const int DefaultQos = 1;
|
||||
|
||||
/// <summary>
|
||||
/// Throw-on-invalid UTF-8, shared (R06:M-7): it is stateless and thread-safe, and allocating one per
|
||||
/// Raw/Scalar delivery was per-message garbage on the dispatcher thread. Same choice
|
||||
/// <c>MqttBrowseSession</c> makes.
|
||||
/// </summary>
|
||||
private static readonly UTF8Encoding Utf8Strict = new(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true);
|
||||
|
||||
// OPC UA status codes (values verified against Opc.Ua.StatusCodes, not recalled).
|
||||
private const uint StatusGood = 0x00000000u;
|
||||
|
||||
@@ -644,13 +652,14 @@ public sealed class MqttSubscriptionManager
|
||||
}
|
||||
|
||||
// A published topic name may not itself contain a wildcard, so a wildcard-authored
|
||||
// definition can never also be an exact-index hit: no tag is delivered twice.
|
||||
foreach (var def in authored.Wildcards)
|
||||
// definition can never also be an exact-index hit: no tag is delivered twice. The trie walks
|
||||
// the topic's levels (R06:M-6 — this used to be one comparer call per wildcard tag, per
|
||||
// message, on the shared dispatcher thread) and every candidate it yields is confirmed by
|
||||
// MQTTnet's own comparer inside the visitor, so the semantics are the broker's.
|
||||
if (authored.WildcardTrie.Count > 0)
|
||||
{
|
||||
if (MqttTopicFilterComparer.Compare(topic, def.Topic) == MqttTopicFilterCompareResult.IsMatch)
|
||||
{
|
||||
Deliver(def, payload, retained, now, oversized, ref json);
|
||||
}
|
||||
var visitor = new WildcardDelivery(this, topic, payload, retained, now, oversized, ref json);
|
||||
authored.WildcardTrie.Match(topic, ref visitor);
|
||||
}
|
||||
}
|
||||
finally
|
||||
@@ -1447,8 +1456,7 @@ public sealed class MqttSubscriptionManager
|
||||
{
|
||||
try
|
||||
{
|
||||
text = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true)
|
||||
.GetString(payload);
|
||||
text = Utf8Strict.GetString(payload);
|
||||
return true;
|
||||
}
|
||||
catch (DecoderFallbackException)
|
||||
@@ -1476,6 +1484,47 @@ public sealed class MqttSubscriptionManager
|
||||
return $"mqtt:{_mode}:{id}:[{named}{overflow}]";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Delivers each wildcard-trie candidate whose filter the MQTTnet comparer confirms. A <c>ref struct</c>
|
||||
/// so the payload span and the fan-out's shared JSON parse ride along without a closure or a list.
|
||||
/// </summary>
|
||||
private ref struct WildcardDelivery : ITopicFilterVisitor<MqttTagDefinition>
|
||||
{
|
||||
private readonly MqttSubscriptionManager _manager;
|
||||
private readonly string _topic;
|
||||
private readonly ReadOnlySpan<byte> _payload;
|
||||
private readonly bool _retained;
|
||||
private readonly DateTime _now;
|
||||
private readonly bool _oversized;
|
||||
private ref PayloadJson _json;
|
||||
|
||||
public WildcardDelivery(
|
||||
MqttSubscriptionManager manager,
|
||||
string topic,
|
||||
ReadOnlySpan<byte> payload,
|
||||
bool retained,
|
||||
DateTime now,
|
||||
bool oversized,
|
||||
ref PayloadJson json)
|
||||
{
|
||||
_manager = manager;
|
||||
_topic = topic;
|
||||
_payload = payload;
|
||||
_retained = retained;
|
||||
_now = now;
|
||||
_oversized = oversized;
|
||||
_json = ref json;
|
||||
}
|
||||
|
||||
public void Visit(MqttTagDefinition def)
|
||||
{
|
||||
if (MqttTopicFilterComparer.Compare(_topic, def.Topic) == MqttTopicFilterCompareResult.IsMatch)
|
||||
{
|
||||
_manager.Deliver(def, _payload, _retained, _now, _oversized, ref _json);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Opaque subscription identity; the diagnostic id names the mode and its topics.</summary>
|
||||
/// <param name="DiagnosticId">The human-readable identity surfaced for diagnostics.</param>
|
||||
private sealed record MqttSubscriptionHandle(string DiagnosticId) : ISubscriptionHandle;
|
||||
@@ -1487,7 +1536,8 @@ public sealed class MqttSubscriptionManager
|
||||
/// <remarks>
|
||||
/// <b>Two topic indexes, and the difference is load-bearing.</b> Delivery matches an
|
||||
/// <i>incoming published topic</i>, which never carries a wildcard, so it uses
|
||||
/// <see cref="ByExactTopic"/> (concrete only) plus a comparer scan of <see cref="Wildcards"/>.
|
||||
/// <see cref="ByExactTopic"/> (concrete only) plus a <see cref="WildcardTrie"/> walk whose candidates the
|
||||
/// comparer confirms.
|
||||
/// Every path that instead reconstructs state from a <i>subscribed filter string</i> —
|
||||
/// reconnect re-subscribe and per-topic degradation — must use <see cref="ByFilter"/>, which is
|
||||
/// keyed by the tag's authored topic <b>including wildcard patterns</b>, because that is what
|
||||
@@ -1501,12 +1551,12 @@ public sealed class MqttSubscriptionManager
|
||||
/// included. Keyed the same way <c>_subscribedTopics</c> and SUBACK outcomes are.
|
||||
/// </param>
|
||||
/// <param name="ByExactTopic">Concrete topic → its definitions; the delivery fast path.</param>
|
||||
/// <param name="Wildcards">Definitions whose authored topic carries an MQTT wildcard.</param>
|
||||
/// <param name="WildcardTrie">Definitions whose authored topic carries an MQTT wildcard, by filter level.</param>
|
||||
private sealed record AuthoredTable(
|
||||
FrozenDictionary<string, MqttTagDefinition> ByRawPath,
|
||||
FrozenDictionary<string, MqttTagDefinition[]> ByFilter,
|
||||
FrozenDictionary<string, MqttTagDefinition[]> ByExactTopic,
|
||||
MqttTagDefinition[] Wildcards)
|
||||
TopicFilterTrie<MqttTagDefinition> WildcardTrie)
|
||||
{
|
||||
public static readonly AuthoredTable Empty =
|
||||
Build(new Dictionary<string, MqttTagDefinition>(StringComparer.Ordinal));
|
||||
@@ -1556,7 +1606,7 @@ public sealed class MqttSubscriptionManager
|
||||
byRawPath.ToFrozenDictionary(StringComparer.Ordinal),
|
||||
byFilter.ToFrozenDictionary(kv => kv.Key, kv => kv.Value.ToArray(), StringComparer.Ordinal),
|
||||
concrete.ToFrozenDictionary(kv => kv.Key, kv => kv.Value.ToArray(), StringComparer.Ordinal),
|
||||
[.. wildcards]);
|
||||
TopicFilterTrie<MqttTagDefinition>.Build(wildcards.Select(d => (d.Topic, d))));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user