Skip to content

Commit

Permalink
Performance tuning (#14604)
Browse files Browse the repository at this point in the history
Co-authored-by: Albumen Kevin <[email protected]>
  • Loading branch information
oxsean and AlbumenJ authored Sep 2, 2024
1 parent 010d926 commit 51f4f74
Show file tree
Hide file tree
Showing 29 changed files with 274 additions and 273 deletions.
2 changes: 2 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ ij_java_extends_list_wrap = normal
ij_java_extends_keyword_wrap = normal
ij_java_binary_operation_wrap = normal
ij_java_binary_operation_sign_on_next_line = true
ij_java_generate_final_locals = false
ij_java_generate_final_parameters = false

[*.groovy]
max_line_length = 180
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static String buildServiceKey(String path, String group, String version)
if (StringUtils.isNotEmpty(version)) {
buf.append(':').append(version);
}
return buf.toString().intern();
return buf.toString();
}

public static String versionFromServiceKey(String serviceKey) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@
import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.resource.Disposable;
import org.apache.dubbo.common.utils.ConcurrentHashMapUtils;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.common.utils.ConcurrentHashSet;
import org.apache.dubbo.common.utils.Pair;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.rpc.model.ScopeModelAccessor;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
Expand All @@ -44,18 +46,19 @@
/**
* A bean factory for internal sharing.
*/
public class ScopeBeanFactory {
public final class ScopeBeanFactory {

protected static final ErrorTypeAwareLogger LOGGER = LoggerFactory.getErrorTypeAwareLogger(ScopeBeanFactory.class);
private static final ErrorTypeAwareLogger LOGGER = LoggerFactory.getErrorTypeAwareLogger(ScopeBeanFactory.class);

private final ScopeBeanFactory parent;
private final ExtensionAccessor extensionAccessor;
private final List<ExtensionPostProcessor> extensionPostProcessors;
private final ConcurrentHashMap<Class<?>, AtomicInteger> beanNameIdCounterMap = new ConcurrentHashMap<>();
private final Map<Class<?>, AtomicInteger> beanNameIdCounterMap = CollectionUtils.newConcurrentHashMap();
private final List<BeanInfo> registeredBeanInfos = new CopyOnWriteArrayList<>();
private InstantiationStrategy instantiationStrategy;
private final AtomicBoolean destroyed = new AtomicBoolean();
private final Set<Class<?>> registeredClasses = new ConcurrentHashSet<>();
private final Map<Pair<Class<?>, String>, Optional<Object>> beanCache = CollectionUtils.newConcurrentHashMap();

public ScopeBeanFactory(ScopeBeanFactory parent, ExtensionAccessor extensionAccessor) {
this.parent = parent;
Expand All @@ -77,7 +80,7 @@ private void initInstantiationStrategy() {
}

public <T> T registerBean(Class<T> bean) throws ScopeBeanException {
return this.getOrRegisterBean(null, bean);
return getOrRegisterBean(null, bean);
}

public <T> T registerBean(String name, Class<T> clazz) throws ScopeBeanException {
Expand All @@ -101,7 +104,7 @@ private <T> T createAndRegisterBean(String name, Class<T> clazz) {
}

public void registerBean(Object bean) {
this.registerBean(null, bean);
registerBean(null, bean);
}

public void registerBean(String name, Object bean) {
Expand All @@ -118,12 +121,14 @@ public void registerBean(String name, Object bean) {
initializeBean(name, bean);

registeredBeanInfos.add(new BeanInfo(name, bean));
beanCache.entrySet().removeIf(e -> e.getKey().getLeft().isAssignableFrom(beanClass));
}

public <T> T getOrRegisterBean(Class<T> type) {
return getOrRegisterBean(null, type);
}

@SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter")
public <T> T getOrRegisterBean(String name, Class<T> type) {
T bean = getBean(name, type);
if (bean == null) {
Expand All @@ -143,6 +148,7 @@ public <T> T getOrRegisterBean(Class<T> type, Function<? super Class<T>, ? exten
return getOrRegisterBean(null, type, mappingFunction);
}

@SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter")
public <T> T getOrRegisterBean(
String name, Class<T> type, Function<? super Class<T>, ? extends T> mappingFunction) {
T bean = getBean(name, type);
Expand Down Expand Up @@ -186,7 +192,8 @@ private boolean containsBean(String name, Object bean) {
}

private int getNextId(Class<?> beanClass) {
return ConcurrentHashMapUtils.computeIfAbsent(beanNameIdCounterMap, beanClass, key -> new AtomicInteger())
return beanNameIdCounterMap
.computeIfAbsent(beanClass, key -> new AtomicInteger())
.incrementAndGet();
}

Expand All @@ -203,20 +210,36 @@ public <T> List<T> getBeansOfType(Class<T> type) {
}

public <T> T getBean(Class<T> type) {
return this.getBean(null, type);
return getBean(null, type);
}

public <T> T getBean(String name, Class<T> type) {
T bean = getBeanInternal(name, type);
T bean = getBeanFromCache(name, type);
if (bean == null && parent != null) {
return parent.getBean(name, type);
}
return bean;
}

@SuppressWarnings("unchecked")
private <T> T getBeanFromCache(String name, Class<T> type) {
Object value = beanCache
.computeIfAbsent(Pair.of(type, name), k -> {
try {
return Optional.ofNullable(getBeanInternal(name, type));
} catch (ScopeBeanException e) {
return Optional.of(e);
}
})
.orElse(null);
if (value instanceof ScopeBeanException) {
throw (ScopeBeanException) value;
}
return (T) value;
}

@SuppressWarnings("unchecked")
private <T> T getBeanInternal(String name, Class<T> type) {
checkDestroyed();
// All classes are derived from java.lang.Object, cannot filter bean by it
if (type == Object.class) {
return null;
Expand Down Expand Up @@ -278,6 +301,7 @@ public void destroy() {
}
}
registeredBeanInfos.clear();
beanCache.clear();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ public void warn(String code, String cause, String extendedInformation, String m

try {
onEvent(code, msg);
if (!getLogger().isWarnEnabled()) {
return;
}
getLogger().warn(appendContextMessageWithInstructions(code, cause, extendedInformation, msg));
} catch (Throwable t) {
// ignored.
Expand All @@ -112,6 +115,9 @@ public void warn(String code, String cause, String extendedInformation, String m

try {
onEvent(code, msg);
if (!getLogger().isWarnEnabled()) {
return;
}
getLogger().warn(appendContextMessageWithInstructions(code, cause, extendedInformation, msg), e);
} catch (Throwable t) {
// ignored.
Expand All @@ -126,6 +132,9 @@ public void error(String code, String cause, String extendedInformation, String

try {
onEvent(code, msg);
if (!getLogger().isErrorEnabled()) {
return;
}
getLogger().error(appendContextMessageWithInstructions(code, cause, extendedInformation, msg));
} catch (Throwable t) {
// ignored.
Expand All @@ -140,6 +149,9 @@ public void error(String code, String cause, String extendedInformation, String

try {
onEvent(code, msg);
if (!getLogger().isErrorEnabled()) {
return;
}
getLogger().error(appendContextMessageWithInstructions(code, cause, extendedInformation, msg), e);
} catch (Throwable t) {
// ignored.
Expand Down
Loading

0 comments on commit 51f4f74

Please sign in to comment.