Repository navigation
Stop re-parsing topic URIs when a validated TopicUri already exists - #150
Merged
Merged
Conversation
vahidhashemian
force-pushed
the
topic_uri_validation_optimization
branch
from
November 10, 2026 22:19
e4ab179 to
77dc43a
Compare
vahidhashemian
force-pushed
the
topic_uri_validation_optimization
branch
from
November 10, 2026 22:42
77dc43a to
c4ba7ba
Compare
ArtemTetenkin
approved these changes
Nov 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The PSC client was parsing a topic URI every time it built a
TopicUriPartitionon the producer send path and the consumer record path, then immediately discarding that parse and attaching aTopicUriit already had.TopicUriPartitionnow has a constructor that keeps the caller's URI string and an already validatedTopicUriwithout running the URI and topic-RN regular expressions again. Call sites that already hold a validatedTopicUriuse that constructor, or the existingTopicUriPartition(TopicUri, int)constructor when the string is already the canonical form fromgetTopicUriAsString().This change is limited to the
pscclient. The Flink connector is unchanged. The publicTopicUriPartition(String)andTopicUriPartition(String, int)constructors still parse, so callers that only have a URI string behave as before.PscProducer.validateTopicUriandPscConsumer.validateTopicUriare still the cached entry points for a new string.Impact
Each
TopicUri.validatecall runs two regular expressions and allocates the parsedTopicUri,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 callPer operation, the change removes one URI parse:
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.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.sendstill 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 backendTopicUriinstance. A later send of the same string reuses that instance.PscKafkaProduceruses the same approach when it builds theKafkaMessageIdafter an ack, both in the completion callback and inFuture.get(), and whengetPartitionsreturns one partition per Kafka partition. Those message ids still carry the canonical URI string, the registeredKafkaTopicUri, and the partition Kafka assigned.Consumer
Each Kafka record and each Memq record now builds its partition from the
TopicUrialready 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 Memqassignment(), Kafka and MemqgetPartitions(), KafkacommitSync(), and the Kafka and Memq commit callbacks, including Kafka auto-resolution both enabled and disabled.PscKafkaConsumer.committed(Collection)no longer builds a partition fromTopicPartition.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 theTopicUriPartitionthe caller passed in, which is whatstartOffsetsandendOffsetsalready 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, andInteger.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.