Skip to content

Commit

Permalink
[improve][broker] Replace String.intern() with Guava Interner (#20432)
Browse files Browse the repository at this point in the history
  • Loading branch information
lhotari authored May 29, 2023
1 parent fafadee commit bdd1bf1
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.pulsar.client.impl.PulsarClientImpl;
import org.apache.pulsar.common.naming.TopicName;
import org.apache.pulsar.common.util.FutureUtil;
import org.apache.pulsar.common.util.StringInterner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -73,9 +74,9 @@ public AbstractReplicator(String localCluster, String localTopicName, String rem
this.brokerService = brokerService;
this.localTopicName = localTopicName;
this.replicatorPrefix = replicatorPrefix;
this.localCluster = localCluster.intern();
this.localCluster = StringInterner.intern(localCluster);
this.remoteTopicName = remoteTopicName;
this.remoteCluster = remoteCluster.intern();
this.remoteCluster = StringInterner.intern(remoteCluster);
this.replicationClient = replicationClient;
this.client = (PulsarClientImpl) brokerService.pulsar().getClient();
this.producer = null;
Expand Down Expand Up @@ -228,7 +229,7 @@ public static String getRemoteCluster(String remoteCursor) {
}

public static String getReplicatorName(String replicatorPrefix, String cluster) {
return (replicatorPrefix + "." + cluster).intern();
return StringInterner.intern(replicatorPrefix + "." + cluster);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@
import org.apache.pulsar.common.schema.SchemaType;
import org.apache.pulsar.common.topics.TopicList;
import org.apache.pulsar.common.util.FutureUtil;
import org.apache.pulsar.common.util.StringInterner;
import org.apache.pulsar.common.util.collections.ConcurrentLongHashMap;
import org.apache.pulsar.common.util.netty.NettyChannelUtil;
import org.apache.pulsar.common.util.netty.NettyFutureUtil;
Expand Down Expand Up @@ -717,7 +718,7 @@ private void completeConnect(int clientProtoVersion, String clientVersion) {
}
setRemoteEndpointProtocolVersion(clientProtoVersion);
if (isNotBlank(clientVersion)) {
this.clientVersion = clientVersion.intern();
this.clientVersion = StringInterner.intern(clientVersion);
}
if (!service.isAuthenticationEnabled()) {
log.info("[{}] connected with clientVersion={}, clientProtocolVersion={}, proxyVersion={}", remoteAddress,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.pulsar.common.util;

import com.google.common.collect.Interner;
import com.google.common.collect.Interners;

/**
* Deduplicates String instances by interning them using Guava's Interner
* which is more efficient than String.intern().
*/
public class StringInterner {
private static final StringInterner INSTANCE = new StringInterner();
private final Interner<String> interner;

public static String intern(String sample) {
return INSTANCE.doIntern(sample);
}

private StringInterner() {
this.interner = Interners.newWeakInterner();
}

String doIntern(String sample) {
if (sample == null) {
return null;
}
return interner.intern(sample);
}
}

0 comments on commit bdd1bf1

Please sign in to comment.