perf: optimize MQTT NatsToMqtt fast path and pre-warm topic cache
Add string.Create fast path in NatsToMqtt for subjects without _DOT_ escape sequences (common case), avoiding StringBuilder allocation. Pre-warm the topic bytes cache when MQTT subscriptions are added to eliminate cache miss on first message delivery.
This commit is contained in:
@@ -83,6 +83,9 @@ public sealed class MqttNatsClientAdapter : INatsClient
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public Subscription AddSubscription(string natsSubject, string sid, string? queue = null)
|
public Subscription AddSubscription(string natsSubject, string sid, string? queue = null)
|
||||||
{
|
{
|
||||||
|
// Pre-warm topic bytes cache for this subject to avoid cache miss on first message.
|
||||||
|
MqttTopicMapper.NatsToMqttBytes(natsSubject);
|
||||||
|
|
||||||
var sub = new Subscription
|
var sub = new Subscription
|
||||||
{
|
{
|
||||||
Client = this,
|
Client = this,
|
||||||
|
|||||||
@@ -107,7 +107,18 @@ public static class MqttTopicMapper
|
|||||||
if (natsSubject.Length == 0)
|
if (natsSubject.Length == 0)
|
||||||
return string.Empty;
|
return string.Empty;
|
||||||
|
|
||||||
// First, replace _DOT_ escape sequences back to dots
|
// Fast path: no _DOT_ escape sequences — just char replacement via string.Create
|
||||||
|
// (avoids StringBuilder allocation for the common case).
|
||||||
|
if (!natsSubject.Contains(DotEscape))
|
||||||
|
{
|
||||||
|
return string.Create(natsSubject.Length, natsSubject, static (span, src) =>
|
||||||
|
{
|
||||||
|
for (var i = 0; i < src.Length; i++)
|
||||||
|
span[i] = src[i] switch { '.' => '/', '*' => '+', '>' => '#', _ => src[i] };
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Slow path: has _DOT_ escape sequences — use StringBuilder
|
||||||
var working = natsSubject.Replace(DotEscape, "\x00");
|
var working = natsSubject.Replace(DotEscape, "\x00");
|
||||||
|
|
||||||
var sb = new StringBuilder(working.Length);
|
var sb = new StringBuilder(working.Length);
|
||||||
|
|||||||
Reference in New Issue
Block a user