Skip to content

Stop re-parsing topic URIs when a validated TopicUri already exists - #150

Merged
vahidhashemian merged 3 commits into
mainfrom
topic_uri_validation_optimization
Nov 10, 2026
Merged

vahidhashemian merged 3 commits into
mainfrom
topic_uri_validation_optimization

Conversation

@vahidhashemian

@vahidhashemian vahidhashemian commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The PSC client was parsing a topic URI every time it built a TopicUriPartition on the producer send path and the consumer record path, then immediately discarding that parse and attaching a TopicUri it already had. TopicUriPartition now has a constructor that keeps the caller's URI string and an already validated TopicUri without running the URI and topic-RN regular expressions again. Call sites that already hold a validated TopicUri use that constructor, or the existing TopicUriPartition(TopicUri, int) constructor when the string is already the canonical form from getTopicUriAsString().

This change is limited to the psc client. The Flink connector is unchanged. The public TopicUriPartition(String) and TopicUriPartition(String, int) constructors still parse, so callers that only have a URI string behave as before. PscProducer.validateTopicUri and PscConsumer.validateTopicUri are still the cached entry points for a new string.

Impact

Each TopicUri.validate call runs two regular expressions and allocates the parsed TopicUri, TopicRn, and matcher objects. The old partition construction ran that parse and then replaced the result, so every affected operation paid for a parse whose output was never used.

Measured in a single-threaded microbenchmark on JDK 11 with a warmed JIT, using a secure URI with a PSC topic RN and three rounds of 3 million iterations:

  • TopicUri.validate: about 735 ns per call
  • Old partition construction (string parse, then finalize): about 745 ns per call
  • New partition construction: about 4 ns per call

Per operation, the change removes one URI parse:

  • Producer: one parse per send() on the message-attach path, about 740 ns. The cached validation of the URI string is unchanged. Each Kafka producer acknowledgement that builds a message id also skips one parse.
  • Consumer: one parse per consumed record in both the Kafka and Memq converters, about 740 ns per record. Assignment, rebalance, getPartitions, and commit-callback paths skip one parse per partition each time they run.

At 100,000 records per second this removes about 74 ms of CPU time every second, roughly 7% of one core. At 1,000,000 records per second it removes about 0.74 seconds of CPU time every second. The discarded TopicUri, TopicRn, and matcher allocations are removed from the same paths, which also reduces garbage collection pressure.

These figures come from a microbenchmark on one machine. Production gain depends on URI length, JIT state, and whether the consumer or producer is CPU-bound.

Producer

PscProducer.send still validates the message URI through the cache. The partition it attaches keeps the exact string the caller passed, including a URI with no protocol, and the validated backend TopicUri instance. A later send of the same string reuses that instance. PscKafkaProducer uses the same approach when it builds the KafkaMessageId after an ack, both in the completion callback and in Future.get(), and when getPartitions returns one partition per Kafka partition. Those message ids still carry the canonical URI string, the registered KafkaTopicUri, and the partition Kafka assigned.

Consumer

Each Kafka record and each Memq record now builds its partition from the TopicUri already registered for that backend topic. The resulting message id still has the same URI string, partition, offset, timestamp, serialized sizes, and, for Memq, the end-of-batch flag. The same construction is used for the partition set on a poll, Kafka rebalance callbacks, Kafka and Memq assignment(), Kafka and Memq getPartitions(), Kafka commitSync(), and the Kafka and Memq commit callbacks, including Kafka auto-resolution both enabled and disabled.

PscKafkaConsumer.committed(Collection) no longer builds a partition from TopicPartition.topic(). That value is the Kafka topic name, so parsing it failed and the returned message id had a null topic URI. It now returns the TopicUriPartition the caller passed in, which is what startOffsets and endOffsets already do. assign() still returns the partitions the caller supplied when the consumer is not subscribed.

Equality, hash code, and ordering are based on the URI components rather than the concrete class, so a partition built this way still matches one finalized with the same URI.

Tests

The new tests compare every changed construction with the previous pattern of parsing the string and then finalizing the partition. They cover plaintext and secure Kafka and Memq URIs, the unset partition and partitions 0, 1, and Integer.MAX_VALUE, equality, hash code, ordering, map and set lookup, message ids, and Java serialization. A non-canonical caller string stays distinct from the canonical partition and is preserved. The string constructors still parse a valid URI and still leave the topic URI unset when the string is invalid. Producer send, Kafka and Memq record conversion, Kafka assignment, rebalance, getPartitions, committed, and both commit-callback modes, and Memq assignment and commit, are all checked against that previous finalized result.

@vahidhashemian
vahidhashemian requested a review from a team as a code owner November 10, 2026 16:34
@vahidhashemian
vahidhashemian force-pushed the topic_uri_validation_optimization branch from e4ab179 to 77dc43a Compare November 10, 2026 22:19
@vahidhashemian
vahidhashemian force-pushed the topic_uri_validation_optimization branch from 77dc43a to c4ba7ba Compare November 10, 2026 22:42
@vahidhashemian
vahidhashemian merged commit f52cdc2 into main Nov 10, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants