You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@pekko.apache.org by "He-Pin (via GitHub)" <gi...@apache.org> on 2026/06/22 17:43:09 UTC

[I] Optimize DistributedPubSubMediator topic key construction with caching [pekko]

He-Pin opened a new issue, #3132:
URL: https://github.com/apache/pekko/issues/3132

   ### Motivation
   
   In `DistributedPubSubMediator`, topic name encoding (`URLEncoder.encode`) and key construction (`mkKey(self.path / encName(topic))`) are performed on every Publish, Subscribe, Unsubscribe, and CountSubscribers message. For high-throughput pub/sub workloads with a limited set of recurring topic names, this creates unnecessary allocations and CPU overhead on the hot path.
   
   The topic names and their encoded forms are typically stable — the same topics are published to and subscribed from repeatedly. Caching the encoded names and constructed keys avoids redundant string allocations and URL encoding operations.
   
   ### Proposed Enhancement
   
   Introduce a lightweight cache (e.g., a `HashMap` or similar structure) that stores:
   1. The URL-encoded form of topic names
   2. The fully constructed routing keys
   
   The cache should be:
   - Bounded to prevent unbounded memory growth if topic names are dynamic
   - Invalidated when topics are removed/pruned
   - Thread-safe within the mediator actor's single-threaded context (no locking needed since the mediator is an actor)
   
   ### Impact
   
   - Reduced allocations on the pub/sub hot path
   - Lower GC pressure in high-throughput scenarios
   - No API changes — purely internal optimization
   - The trade-off is a small amount of additional memory for the cache
   
   ### References
   
   This optimization was implemented in the Akka.NET project: https://github.com/akkadotnet/akka.net/issues/7642


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org


Re: [I] Optimize DistributedPubSubMediator topic key construction with caching [pekko]

Posted by "He-Pin (via GitHub)" <gi...@apache.org>.
He-Pin commented on issue #3132:
URL: https://github.com/apache/pekko/issues/3132#issuecomment-4771328818

   Closing as premature optimization. The proposed cache adds implementation complexity (bounded eviction, invalidation on topic pruning) without demonstrated benefit.
   
   `URLEncoder.encode` on short topic names and `ActorPath.toStringWithoutAddress` are not expensive operations — the GC pressure from a few string allocations per message is likely negligible compared to actor message dispatch, serialization, and network I/O costs.
   
   If this optimization is worth pursuing, please reopen with:
   - A JMH benchmark showing measurable throughput improvement (before vs. after) on a realistic high-throughput pub/sub workload
   - Evidence that the encoding/key-construction overhead is a meaningful fraction of total per-message cost


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org


Re: [I] Optimize DistributedPubSubMediator topic key construction with caching [pekko]

Posted by "He-Pin (via GitHub)" <gi...@apache.org>.
He-Pin closed issue #3132: Optimize DistributedPubSubMediator topic key construction with caching
URL: https://github.com/apache/pekko/issues/3132


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org