Skip to content

Commit 355140a

Browse files
committed
Don't initialize namespace by default
1 parent f56810a commit 355140a

File tree

2 files changed

+6
-25
lines changed

2 files changed

+6
-25
lines changed

microbench/src/main/java/org/apache/pulsar/broker/qos/TopicNameBenchmark.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,7 @@ public void testReadFromCache(Blackhole blackhole) {
6262
public void testConstruct(Blackhole blackhole) {
6363
for (int i = 0; i < 10000; i++) {
6464
for (final var topicBase : topicBases) {
65-
blackhole.consume(new TopicName(topicBase + i, false));
66-
}
67-
}
68-
}
69-
70-
@Threads(1)
71-
@Benchmark
72-
@Warmup(time = 5, iterations = 1)
73-
@Measurement(time = 5, iterations = 1)
74-
public void testConstructWithNamespaceInitialized(Blackhole blackhole) {
75-
for (int i = 0; i < 10000; i++) {
76-
for (final var topicBase : topicBases) {
77-
blackhole.consume(new TopicName(topicBase + i, true));
65+
blackhole.consume(new TopicName(topicBase + i));
7866
}
7967
}
8068
}

pulsar-common/src/main/java/org/apache/pulsar/common/naming/TopicName.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -110,19 +110,16 @@ public static String getPattern(String topic) {
110110
return "^" + Pattern.quote(get(topic).getPartitionedTopicName().toString()) + "$";
111111
}
112112

113-
private TopicName(String completeTopicName) {
114-
this(completeTopicName, true);
115-
}
116-
117113
/**
118-
* The constructor from a topic name string. You can leverage {@link TopicName#get(String)} to get benefits from
119-
* the built-in cache mechanism.
114+
* The constructor from a topic name string. The difference from {@link TopicName#get(String)} is that the `get`
115+
* method can leverage the built-in cache mechanism, which can be slightly faster, but at the cost of higher JVM
116+
* memory usage that could lead to unnecessary GC, as well as potentially OOM in extreme cases.
117+
* You can benchmark `TopicName.get(topic)` and `new TopicName(topic)` to see the actual performance gap.
120118
*
121119
* @param completeTopicName the topic name
122-
* @param initializeNamespaceName whether to initializing the internal {@link NamespaceName} field
123120
*/
124121
@SuppressFBWarnings("DCN_NULLPOINTER_EXCEPTION")
125-
public TopicName(String completeTopicName, boolean initializeNamespaceName) {
122+
public TopicName(String completeTopicName) {
126123
try {
127124
// The topic name can be in two different forms, one is fully qualified topic name,
128125
// the other one is short topic name
@@ -179,10 +176,6 @@ public TopicName(String completeTopicName, boolean initializeNamespaceName) {
179176
this.domain = TopicDomain.getEnum(completeTopicName.substring(0, index));
180177
}
181178

182-
if (initializeNamespaceName) {
183-
getNamespaceObject();
184-
}
185-
186179
if (StringUtils.isBlank(localName)) {
187180
throw new IllegalArgumentException(String.format("Invalid topic name: %s. Topic local name must not"
188181
+ " be blank.", completeTopicName));

0 commit comments

Comments
 (0)