You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by "kylixs (via GitHub)" <gi...@apache.org> on 2023/10/28 05:52:04 UTC
[PR] Optimize bytebuddy type description performance [skywalking-java]
kylixs opened a new pull request, #637:
URL: https://github.com/apache/skywalking-java/pull/637
<!--
⚠️ Please make sure to read this template first, pull requests that don't accord with this template
maybe closed without notice.
Texts surrounded by `<` and `>` are meant to be replaced by you, e.g. <framework name>, <issue number>.
Put an `x` in the `[ ]` to mark the item as CHECKED. `[x]`
-->
### Improve the performance of type description of byte-buddy
* SWDescriptionStrategy : use cache `TypeDescription` of the class that has been transformed, then do as `AgentBuilder.DescriptionStrategy.Default.HYBRID`
Relative Issue: https://github.com/apache/skywalking/issues/11460
- [ ] If this pull request closes/resolves/fixes an existing issue, replace the issue number. Closes #<issue number>.
- [ ] Update the [`CHANGES` log](https://github.com/apache/skywalking-java/blob/main/CHANGES.md).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Optimize bytebuddy type description performance [skywalking-java]
Posted by "wu-sheng (via GitHub)" <gi...@apache.org>.
wu-sheng commented on PR #637:
URL: https://github.com/apache/skywalking-java/pull/637#issuecomment-1784393406
We could try, from asking what could be side effect of this.
After all, bytebuddy is a general lib, more corner cases we may not know.
I am going to close mine.
@kylixs Could you polish this and update change log as well?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Optimize bytebuddy type description performance [skywalking-java]
Posted by "kylixs (via GitHub)" <gi...@apache.org>.
kylixs commented on PR #637:
URL: https://github.com/apache/skywalking-java/pull/637#issuecomment-1784645425
@wu-sheng Some test cases failing, may be not caused by this PR, trigger them again?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Optimize bytebuddy type description performance [skywalking-java]
Posted by "wu-sheng (via GitHub)" <gi...@apache.org>.
wu-sheng commented on PR #637:
URL: https://github.com/apache/skywalking-java/pull/637#issuecomment-1786687406
According to the codes of `typePoolCache#locate`, too many instances of `CacheProvider` usually means too many class loaders? This test case doesn't seem so. Could you debug it to see why they are created in a so large number?
```java
protected TypePool.CacheProvider locate(@MaybeNull ClassLoader classLoader) {
classLoader = classLoader == null ? getBootstrapMarkerLoader() : classLoader;
TypePool.CacheProvider cacheProvider = cacheProviders.get(classLoader);
while (cacheProvider == null) {
cacheProvider = TypePool.CacheProvider.Simple.withObjectType();
TypePool.CacheProvider previous = cacheProviders.putIfAbsent(classLoader, cacheProvider);
if (previous != null) {
cacheProvider = previous;
}
}
return cacheProvider;
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Optimize bytebuddy type description performance [skywalking-java]
Posted by "wu-sheng (via GitHub)" <gi...@apache.org>.
wu-sheng commented on code in PR #637:
URL: https://github.com/apache/skywalking-java/pull/637#discussion_r1380339592
##########
apm-sniffer/bytebuddy-patch/src/main/java/net/bytebuddy/agent/builder/SWDescriptionStrategy.java:
##########
@@ -0,0 +1,335 @@
+/*
+ * 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 net.bytebuddy.agent.builder;
+
+import net.bytebuddy.description.annotation.AnnotationList;
+import net.bytebuddy.description.field.FieldDescription;
+import net.bytebuddy.description.field.FieldList;
+import net.bytebuddy.description.method.MethodDescription;
+import net.bytebuddy.description.method.MethodList;
+import net.bytebuddy.description.type.*;
+import net.bytebuddy.dynamic.TargetType;
+import net.bytebuddy.implementation.bytecode.StackSize;
+import net.bytebuddy.pool.TypePool;
+import net.bytebuddy.utility.JavaModule;
+import net.bytebuddy.utility.nullability.MaybeNull;
+import org.apache.skywalking.apm.agent.core.plugin.AbstractClassEnhancePluginDefine;
+import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
+
+import java.io.Serializable;
+import java.lang.annotation.Annotation;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * A DescriptionStrategy to get the original class description by removing dynamic field and method tokens
+ * generated by SkyWalking.
+ */
+public class SWDescriptionStrategy implements AgentBuilder.DescriptionStrategy {
+
+ /**
+ * A cache of type descriptions for commonly used types to avoid unnecessary allocations.
+ */
+ private static final Map<Class<?>, TypeDescription> TYPE_CACHE;
+
+ /*
+ * Initializes the type cache.
+ */
+ static {
+ TYPE_CACHE = new HashMap<Class<?>, TypeDescription>();
+ TYPE_CACHE.put(TargetType.class, new TypeDescription.ForLoadedType(TargetType.class));
+ TYPE_CACHE.put(Class.class, new TypeDescription.ForLoadedType(Class.class));
+ TYPE_CACHE.put(Throwable.class, new TypeDescription.ForLoadedType(Throwable.class));
+ TYPE_CACHE.put(Annotation.class, new TypeDescription.ForLoadedType(Annotation.class));
+ TYPE_CACHE.put(Object.class, new TypeDescription.ForLoadedType(Object.class));
+ TYPE_CACHE.put(String.class, new TypeDescription.ForLoadedType(String.class));
+ TYPE_CACHE.put(Boolean.class, new TypeDescription.ForLoadedType(Boolean.class));
+ TYPE_CACHE.put(Byte.class, new TypeDescription.ForLoadedType(Byte.class));
+ TYPE_CACHE.put(Short.class, new TypeDescription.ForLoadedType(Short.class));
+ TYPE_CACHE.put(Character.class, new TypeDescription.ForLoadedType(Character.class));
+ TYPE_CACHE.put(Integer.class, new TypeDescription.ForLoadedType(Integer.class));
+ TYPE_CACHE.put(Long.class, new TypeDescription.ForLoadedType(Long.class));
+ TYPE_CACHE.put(Float.class, new TypeDescription.ForLoadedType(Float.class));
+ TYPE_CACHE.put(Double.class, new TypeDescription.ForLoadedType(Double.class));
+ TYPE_CACHE.put(void.class, new TypeDescription.ForLoadedType(void.class));
+ TYPE_CACHE.put(boolean.class, new TypeDescription.ForLoadedType(boolean.class));
+ TYPE_CACHE.put(byte.class, new TypeDescription.ForLoadedType(byte.class));
+ TYPE_CACHE.put(short.class, new TypeDescription.ForLoadedType(short.class));
+ TYPE_CACHE.put(char.class, new TypeDescription.ForLoadedType(char.class));
+ TYPE_CACHE.put(int.class, new TypeDescription.ForLoadedType(int.class));
+ TYPE_CACHE.put(long.class, new TypeDescription.ForLoadedType(long.class));
+ TYPE_CACHE.put(float.class, new TypeDescription.ForLoadedType(float.class));
+ TYPE_CACHE.put(double.class, new TypeDescription.ForLoadedType(double.class));
+ }
+
+ private AgentBuilder.DescriptionStrategy delegate = Default.HYBRID;
+
+ private String nameTrait;
+
+ public SWDescriptionStrategy(String nameTrait) {
+ this.nameTrait = nameTrait;
+ }
+
+ @Override
+ public boolean isLoadedFirst() {
+ return true;
+ }
+
+ @Override
+ public TypeDescription apply(String name,
+ @MaybeNull Class<?> type,
+ TypePool typePool,
+ AgentBuilder.CircularityLock circularityLock,
+ @MaybeNull ClassLoader classLoader,
+ @MaybeNull JavaModule module) {
+ // find from type cache
+ if (type != null) {
+ TypeDescription typeDescription = TYPE_CACHE.get(type);
+ if (typeDescription != null) {
+ return typeDescription;
+ }
+ }
+ // wrap result
+ return new SWTypeDescriptionWrapper(delegate.apply(name, type, typePool, circularityLock, classLoader, module), nameTrait);
+ }
+
+ /**
+ * A TypeDescription wrapper to remove fields, methods, interface generated by SkyWalking.
+ */
+ static class SWTypeDescriptionWrapper extends TypeDescription.AbstractBase implements Serializable {
+
+ /**
+ * The class's serial version UID.
+ */
+ private static final long serialVersionUID = 1L;
+
+ private static final List<String> IGNORED_INTERFACES = Arrays.asList(EnhancedInstance.class.getName());
+
+ private static final List<String> IGNORED_FIELDS = Arrays.asList(AbstractClassEnhancePluginDefine.CONTEXT_ATTR_NAME);
+
+ private static final List<String> IGNORED_METHODS = Arrays.asList("getSkyWalkingDynamicField", "setSkyWalkingDynamicField");
+
+ private MethodList<MethodDescription.InDefinedShape> methods;
+
+ private FieldList<FieldDescription.InDefinedShape> fields;
+
+ private final String nameTrait;
+
+ private TypeList.Generic interfaces;
+
+ private TypeDescription delegate;
+
+ public SWTypeDescriptionWrapper(TypeDescription delegate, String nameTrait) {
+ this.delegate = delegate;
+ this.nameTrait = nameTrait;
+ }
+
+ @Override
+ public TypeList.Generic getInterfaces() {
+ if (this.interfaces == null) {
+ TypeList.Generic allInterfaces = delegate.getInterfaces();
+ if (allInterfaces.stream().anyMatch(s -> IGNORED_INTERFACES.contains(s.getTypeName()))) {
+ // remove interfaces added by SkyWalking
+ List<Generic> list = allInterfaces.stream()
+ .filter(s -> !IGNORED_INTERFACES.contains(s.getTypeName()))
+ .collect(Collectors.toList());
+ this.interfaces = new TypeList.Generic.Explicit(list);
+ } else {
+ this.interfaces = allInterfaces;
+ }
+ }
+ return this.interfaces;
+ }
+
+ @Override
+ public FieldList<FieldDescription.InDefinedShape> getDeclaredFields() {
+ if (this.fields == null) {
+ FieldList<FieldDescription.InDefinedShape> declaredFields = delegate.getDeclaredFields();
+ if (declaredFields.stream()
+ .anyMatch(f -> f.getName().contains(nameTrait) || IGNORED_FIELDS.contains(f.getName()))) {
+ // Remove dynamic field tokens generated by SkyWalking
+ fields = new FieldList.Explicit<>(declaredFields.stream()
+ .filter(f -> !f.getName().contains(nameTrait) && !IGNORED_FIELDS.contains(f.getName()))
+ .collect(Collectors.toList()));
+ } else {
+ fields = declaredFields;
+ }
+ }
+ return fields;
+ }
+
+ @Override
+ public MethodList<MethodDescription.InDefinedShape> getDeclaredMethods() {
+ if (this.methods == null) {
+ MethodList<MethodDescription.InDefinedShape> declaredMethods = delegate.getDeclaredMethods();
+ if (declaredMethods.stream()
+ .anyMatch(m -> m.getName().contains(nameTrait) || IGNORED_METHODS.contains(m.getName()))) {
+ // Remove dynamic method tokens generated by SkyWalking
+ methods = new MethodList.Explicit<>(declaredMethods.stream()
+ .filter(m -> !m.getName().contains(nameTrait) && !IGNORED_METHODS.contains(m.getName()))
+ .collect(Collectors.toList()));
+ } else {
+ methods = declaredMethods;
+ }
+ }
+ return methods;
+ }
+
+ @Override
+ public boolean isAssignableTo(Class<?> type) {
+ // ignore interface added by SkyWalking
+ if (IGNORED_INTERFACES.contains(type.getName())) {
+ return false;
+ }
+ return delegate.isAssignableTo(type);
+ }
+
+ @Override
+ public boolean isAccessibleTo(TypeDescription typeDescription) {
+ // ignore interface added by SkyWalking
Review Comment:
```suggestion
// Skip the Interface added by SkyWalking
```
##########
apm-sniffer/bytebuddy-patch/src/main/java/net/bytebuddy/agent/builder/SWDescriptionStrategy.java:
##########
@@ -0,0 +1,335 @@
+/*
+ * 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 net.bytebuddy.agent.builder;
+
+import net.bytebuddy.description.annotation.AnnotationList;
+import net.bytebuddy.description.field.FieldDescription;
+import net.bytebuddy.description.field.FieldList;
+import net.bytebuddy.description.method.MethodDescription;
+import net.bytebuddy.description.method.MethodList;
+import net.bytebuddy.description.type.*;
+import net.bytebuddy.dynamic.TargetType;
+import net.bytebuddy.implementation.bytecode.StackSize;
+import net.bytebuddy.pool.TypePool;
+import net.bytebuddy.utility.JavaModule;
+import net.bytebuddy.utility.nullability.MaybeNull;
+import org.apache.skywalking.apm.agent.core.plugin.AbstractClassEnhancePluginDefine;
+import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
+
+import java.io.Serializable;
+import java.lang.annotation.Annotation;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * A DescriptionStrategy to get the original class description by removing dynamic field and method tokens
+ * generated by SkyWalking.
+ */
+public class SWDescriptionStrategy implements AgentBuilder.DescriptionStrategy {
+
+ /**
+ * A cache of type descriptions for commonly used types to avoid unnecessary allocations.
+ */
+ private static final Map<Class<?>, TypeDescription> TYPE_CACHE;
+
+ /*
+ * Initializes the type cache.
+ */
+ static {
+ TYPE_CACHE = new HashMap<Class<?>, TypeDescription>();
+ TYPE_CACHE.put(TargetType.class, new TypeDescription.ForLoadedType(TargetType.class));
+ TYPE_CACHE.put(Class.class, new TypeDescription.ForLoadedType(Class.class));
+ TYPE_CACHE.put(Throwable.class, new TypeDescription.ForLoadedType(Throwable.class));
+ TYPE_CACHE.put(Annotation.class, new TypeDescription.ForLoadedType(Annotation.class));
+ TYPE_CACHE.put(Object.class, new TypeDescription.ForLoadedType(Object.class));
+ TYPE_CACHE.put(String.class, new TypeDescription.ForLoadedType(String.class));
+ TYPE_CACHE.put(Boolean.class, new TypeDescription.ForLoadedType(Boolean.class));
+ TYPE_CACHE.put(Byte.class, new TypeDescription.ForLoadedType(Byte.class));
+ TYPE_CACHE.put(Short.class, new TypeDescription.ForLoadedType(Short.class));
+ TYPE_CACHE.put(Character.class, new TypeDescription.ForLoadedType(Character.class));
+ TYPE_CACHE.put(Integer.class, new TypeDescription.ForLoadedType(Integer.class));
+ TYPE_CACHE.put(Long.class, new TypeDescription.ForLoadedType(Long.class));
+ TYPE_CACHE.put(Float.class, new TypeDescription.ForLoadedType(Float.class));
+ TYPE_CACHE.put(Double.class, new TypeDescription.ForLoadedType(Double.class));
+ TYPE_CACHE.put(void.class, new TypeDescription.ForLoadedType(void.class));
+ TYPE_CACHE.put(boolean.class, new TypeDescription.ForLoadedType(boolean.class));
+ TYPE_CACHE.put(byte.class, new TypeDescription.ForLoadedType(byte.class));
+ TYPE_CACHE.put(short.class, new TypeDescription.ForLoadedType(short.class));
+ TYPE_CACHE.put(char.class, new TypeDescription.ForLoadedType(char.class));
+ TYPE_CACHE.put(int.class, new TypeDescription.ForLoadedType(int.class));
+ TYPE_CACHE.put(long.class, new TypeDescription.ForLoadedType(long.class));
+ TYPE_CACHE.put(float.class, new TypeDescription.ForLoadedType(float.class));
+ TYPE_CACHE.put(double.class, new TypeDescription.ForLoadedType(double.class));
+ }
+
+ private AgentBuilder.DescriptionStrategy delegate = Default.HYBRID;
+
+ private String nameTrait;
+
+ public SWDescriptionStrategy(String nameTrait) {
+ this.nameTrait = nameTrait;
+ }
+
+ @Override
+ public boolean isLoadedFirst() {
+ return true;
+ }
+
+ @Override
+ public TypeDescription apply(String name,
+ @MaybeNull Class<?> type,
+ TypePool typePool,
+ AgentBuilder.CircularityLock circularityLock,
+ @MaybeNull ClassLoader classLoader,
+ @MaybeNull JavaModule module) {
+ // find from type cache
+ if (type != null) {
+ TypeDescription typeDescription = TYPE_CACHE.get(type);
+ if (typeDescription != null) {
+ return typeDescription;
+ }
+ }
+ // wrap result
+ return new SWTypeDescriptionWrapper(delegate.apply(name, type, typePool, circularityLock, classLoader, module), nameTrait);
+ }
+
+ /**
+ * A TypeDescription wrapper to remove fields, methods, interface generated by SkyWalking.
+ */
+ static class SWTypeDescriptionWrapper extends TypeDescription.AbstractBase implements Serializable {
+
+ /**
+ * The class's serial version UID.
+ */
+ private static final long serialVersionUID = 1L;
+
+ private static final List<String> IGNORED_INTERFACES = Arrays.asList(EnhancedInstance.class.getName());
+
+ private static final List<String> IGNORED_FIELDS = Arrays.asList(AbstractClassEnhancePluginDefine.CONTEXT_ATTR_NAME);
+
+ private static final List<String> IGNORED_METHODS = Arrays.asList("getSkyWalkingDynamicField", "setSkyWalkingDynamicField");
+
+ private MethodList<MethodDescription.InDefinedShape> methods;
+
+ private FieldList<FieldDescription.InDefinedShape> fields;
+
+ private final String nameTrait;
+
+ private TypeList.Generic interfaces;
+
+ private TypeDescription delegate;
+
+ public SWTypeDescriptionWrapper(TypeDescription delegate, String nameTrait) {
+ this.delegate = delegate;
+ this.nameTrait = nameTrait;
+ }
+
+ @Override
+ public TypeList.Generic getInterfaces() {
+ if (this.interfaces == null) {
+ TypeList.Generic allInterfaces = delegate.getInterfaces();
+ if (allInterfaces.stream().anyMatch(s -> IGNORED_INTERFACES.contains(s.getTypeName()))) {
+ // remove interfaces added by SkyWalking
+ List<Generic> list = allInterfaces.stream()
+ .filter(s -> !IGNORED_INTERFACES.contains(s.getTypeName()))
+ .collect(Collectors.toList());
+ this.interfaces = new TypeList.Generic.Explicit(list);
+ } else {
+ this.interfaces = allInterfaces;
+ }
+ }
+ return this.interfaces;
+ }
+
+ @Override
+ public FieldList<FieldDescription.InDefinedShape> getDeclaredFields() {
+ if (this.fields == null) {
+ FieldList<FieldDescription.InDefinedShape> declaredFields = delegate.getDeclaredFields();
+ if (declaredFields.stream()
+ .anyMatch(f -> f.getName().contains(nameTrait) || IGNORED_FIELDS.contains(f.getName()))) {
+ // Remove dynamic field tokens generated by SkyWalking
+ fields = new FieldList.Explicit<>(declaredFields.stream()
+ .filter(f -> !f.getName().contains(nameTrait) && !IGNORED_FIELDS.contains(f.getName()))
+ .collect(Collectors.toList()));
+ } else {
+ fields = declaredFields;
+ }
+ }
+ return fields;
+ }
+
+ @Override
+ public MethodList<MethodDescription.InDefinedShape> getDeclaredMethods() {
+ if (this.methods == null) {
+ MethodList<MethodDescription.InDefinedShape> declaredMethods = delegate.getDeclaredMethods();
+ if (declaredMethods.stream()
+ .anyMatch(m -> m.getName().contains(nameTrait) || IGNORED_METHODS.contains(m.getName()))) {
+ // Remove dynamic method tokens generated by SkyWalking
+ methods = new MethodList.Explicit<>(declaredMethods.stream()
+ .filter(m -> !m.getName().contains(nameTrait) && !IGNORED_METHODS.contains(m.getName()))
+ .collect(Collectors.toList()));
+ } else {
+ methods = declaredMethods;
+ }
+ }
+ return methods;
+ }
+
+ @Override
+ public boolean isAssignableTo(Class<?> type) {
+ // ignore interface added by SkyWalking
Review Comment:
```suggestion
// Skip the interface added by SkyWalking
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Optimize bytebuddy type description performance [skywalking-java]
Posted by "wu-sheng (via GitHub)" <gi...@apache.org>.
wu-sheng commented on PR #637:
URL: https://github.com/apache/skywalking-java/pull/637#issuecomment-1784434790
Update changes.md should be enough.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Optimize bytebuddy type description performance [skywalking-java]
Posted by "wu-sheng (via GitHub)" <gi...@apache.org>.
wu-sheng commented on PR #637:
URL: https://github.com/apache/skywalking-java/pull/637#issuecomment-1784669866
Done, you are a committer now, you could use details->rerun by yourself.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Optimize bytebuddy type description performance [skywalking-java]
Posted by "wu-sheng (via GitHub)" <gi...@apache.org>.
wu-sheng commented on code in PR #637:
URL: https://github.com/apache/skywalking-java/pull/637#discussion_r1380339215
##########
apm-sniffer/bytebuddy-patch/src/main/java/net/bytebuddy/agent/builder/SWDescriptionStrategy.java:
##########
@@ -0,0 +1,335 @@
+/*
+ * 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 net.bytebuddy.agent.builder;
+
+import net.bytebuddy.description.annotation.AnnotationList;
+import net.bytebuddy.description.field.FieldDescription;
+import net.bytebuddy.description.field.FieldList;
+import net.bytebuddy.description.method.MethodDescription;
+import net.bytebuddy.description.method.MethodList;
+import net.bytebuddy.description.type.*;
+import net.bytebuddy.dynamic.TargetType;
+import net.bytebuddy.implementation.bytecode.StackSize;
+import net.bytebuddy.pool.TypePool;
+import net.bytebuddy.utility.JavaModule;
+import net.bytebuddy.utility.nullability.MaybeNull;
+import org.apache.skywalking.apm.agent.core.plugin.AbstractClassEnhancePluginDefine;
+import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
+
+import java.io.Serializable;
+import java.lang.annotation.Annotation;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * A DescriptionStrategy to get the original class description by removing dynamic field and method tokens
+ * generated by SkyWalking.
+ */
+public class SWDescriptionStrategy implements AgentBuilder.DescriptionStrategy {
+
+ /**
+ * A cache of type descriptions for commonly used types to avoid unnecessary allocations.
+ */
+ private static final Map<Class<?>, TypeDescription> TYPE_CACHE;
+
+ /*
+ * Initializes the type cache.
+ */
+ static {
+ TYPE_CACHE = new HashMap<Class<?>, TypeDescription>();
+ TYPE_CACHE.put(TargetType.class, new TypeDescription.ForLoadedType(TargetType.class));
+ TYPE_CACHE.put(Class.class, new TypeDescription.ForLoadedType(Class.class));
+ TYPE_CACHE.put(Throwable.class, new TypeDescription.ForLoadedType(Throwable.class));
+ TYPE_CACHE.put(Annotation.class, new TypeDescription.ForLoadedType(Annotation.class));
+ TYPE_CACHE.put(Object.class, new TypeDescription.ForLoadedType(Object.class));
+ TYPE_CACHE.put(String.class, new TypeDescription.ForLoadedType(String.class));
+ TYPE_CACHE.put(Boolean.class, new TypeDescription.ForLoadedType(Boolean.class));
+ TYPE_CACHE.put(Byte.class, new TypeDescription.ForLoadedType(Byte.class));
+ TYPE_CACHE.put(Short.class, new TypeDescription.ForLoadedType(Short.class));
+ TYPE_CACHE.put(Character.class, new TypeDescription.ForLoadedType(Character.class));
+ TYPE_CACHE.put(Integer.class, new TypeDescription.ForLoadedType(Integer.class));
+ TYPE_CACHE.put(Long.class, new TypeDescription.ForLoadedType(Long.class));
+ TYPE_CACHE.put(Float.class, new TypeDescription.ForLoadedType(Float.class));
+ TYPE_CACHE.put(Double.class, new TypeDescription.ForLoadedType(Double.class));
+ TYPE_CACHE.put(void.class, new TypeDescription.ForLoadedType(void.class));
+ TYPE_CACHE.put(boolean.class, new TypeDescription.ForLoadedType(boolean.class));
+ TYPE_CACHE.put(byte.class, new TypeDescription.ForLoadedType(byte.class));
+ TYPE_CACHE.put(short.class, new TypeDescription.ForLoadedType(short.class));
+ TYPE_CACHE.put(char.class, new TypeDescription.ForLoadedType(char.class));
+ TYPE_CACHE.put(int.class, new TypeDescription.ForLoadedType(int.class));
+ TYPE_CACHE.put(long.class, new TypeDescription.ForLoadedType(long.class));
+ TYPE_CACHE.put(float.class, new TypeDescription.ForLoadedType(float.class));
+ TYPE_CACHE.put(double.class, new TypeDescription.ForLoadedType(double.class));
+ }
+
+ private AgentBuilder.DescriptionStrategy delegate = Default.HYBRID;
+
+ private String nameTrait;
+
+ public SWDescriptionStrategy(String nameTrait) {
+ this.nameTrait = nameTrait;
+ }
+
+ @Override
+ public boolean isLoadedFirst() {
+ return true;
+ }
+
+ @Override
+ public TypeDescription apply(String name,
+ @MaybeNull Class<?> type,
+ TypePool typePool,
+ AgentBuilder.CircularityLock circularityLock,
+ @MaybeNull ClassLoader classLoader,
+ @MaybeNull JavaModule module) {
+ // find from type cache
+ if (type != null) {
+ TypeDescription typeDescription = TYPE_CACHE.get(type);
+ if (typeDescription != null) {
+ return typeDescription;
+ }
+ }
+ // wrap result
+ return new SWTypeDescriptionWrapper(delegate.apply(name, type, typePool, circularityLock, classLoader, module), nameTrait);
+ }
+
+ /**
+ * A TypeDescription wrapper to remove fields, methods, interface generated by SkyWalking.
+ */
+ static class SWTypeDescriptionWrapper extends TypeDescription.AbstractBase implements Serializable {
+
+ /**
+ * The class's serial version UID.
+ */
+ private static final long serialVersionUID = 1L;
+
+ private static final List<String> IGNORED_INTERFACES = Arrays.asList(EnhancedInstance.class.getName());
+
+ private static final List<String> IGNORED_FIELDS = Arrays.asList(AbstractClassEnhancePluginDefine.CONTEXT_ATTR_NAME);
+
+ private static final List<String> IGNORED_METHODS = Arrays.asList("getSkyWalkingDynamicField", "setSkyWalkingDynamicField");
+
+ private MethodList<MethodDescription.InDefinedShape> methods;
+
+ private FieldList<FieldDescription.InDefinedShape> fields;
+
+ private final String nameTrait;
+
+ private TypeList.Generic interfaces;
+
+ private TypeDescription delegate;
+
+ public SWTypeDescriptionWrapper(TypeDescription delegate, String nameTrait) {
+ this.delegate = delegate;
+ this.nameTrait = nameTrait;
+ }
+
+ @Override
+ public TypeList.Generic getInterfaces() {
+ if (this.interfaces == null) {
+ TypeList.Generic allInterfaces = delegate.getInterfaces();
+ if (allInterfaces.stream().anyMatch(s -> IGNORED_INTERFACES.contains(s.getTypeName()))) {
+ // remove interfaces added by SkyWalking
+ List<Generic> list = allInterfaces.stream()
+ .filter(s -> !IGNORED_INTERFACES.contains(s.getTypeName()))
+ .collect(Collectors.toList());
+ this.interfaces = new TypeList.Generic.Explicit(list);
+ } else {
+ this.interfaces = allInterfaces;
+ }
+ }
+ return this.interfaces;
+ }
+
+ @Override
+ public FieldList<FieldDescription.InDefinedShape> getDeclaredFields() {
+ if (this.fields == null) {
+ FieldList<FieldDescription.InDefinedShape> declaredFields = delegate.getDeclaredFields();
+ if (declaredFields.stream()
+ .anyMatch(f -> f.getName().contains(nameTrait) || IGNORED_FIELDS.contains(f.getName()))) {
+ // Remove dynamic field tokens generated by SkyWalking
+ fields = new FieldList.Explicit<>(declaredFields.stream()
+ .filter(f -> !f.getName().contains(nameTrait) && !IGNORED_FIELDS.contains(f.getName()))
+ .collect(Collectors.toList()));
+ } else {
+ fields = declaredFields;
+ }
+ }
+ return fields;
+ }
+
+ @Override
+ public MethodList<MethodDescription.InDefinedShape> getDeclaredMethods() {
+ if (this.methods == null) {
+ MethodList<MethodDescription.InDefinedShape> declaredMethods = delegate.getDeclaredMethods();
+ if (declaredMethods.stream()
+ .anyMatch(m -> m.getName().contains(nameTrait) || IGNORED_METHODS.contains(m.getName()))) {
+ // Remove dynamic method tokens generated by SkyWalking
+ methods = new MethodList.Explicit<>(declaredMethods.stream()
+ .filter(m -> !m.getName().contains(nameTrait) && !IGNORED_METHODS.contains(m.getName()))
+ .collect(Collectors.toList()));
+ } else {
+ methods = declaredMethods;
+ }
+ }
+ return methods;
+ }
+
+ @Override
+ public boolean isAssignableTo(Class<?> type) {
+ // ignore interface added by SkyWalking
Review Comment:
```suggestion
// Ignore the interface added by SkyWalking
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Optimize bytebuddy type description performance [skywalking-java]
Posted by "lujiajing1126 (via GitHub)" <gi...@apache.org>.
lujiajing1126 commented on code in PR #637:
URL: https://github.com/apache/skywalking-java/pull/637#discussion_r1378491612
##########
apm-sniffer/bytebuddy-patch/src/main/java/net/bytebuddy/agent/builder/SWDescriptionStrategy.java:
##########
@@ -0,0 +1,64 @@
+/*
+ * 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 net.bytebuddy.agent.builder;
+
+import net.bytebuddy.agent.builder.AgentBuilder.PoolStrategy.WithTypePoolCache.Simple;
+import net.bytebuddy.description.type.TypeDescription;
+import net.bytebuddy.pool.TypePool;
+import net.bytebuddy.utility.JavaModule;
+import net.bytebuddy.utility.nullability.MaybeNull;
+
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * A DescriptionStrategy with interoperable cache provider.
+ */
+public class SWDescriptionStrategy implements AgentBuilder.DescriptionStrategy {
+
+ private Simple typePoolCache = new Simple(new ConcurrentHashMap<>());
+
+ private AgentBuilder.DescriptionStrategy delegate = AgentBuilder.DescriptionStrategy.Default.HYBRID;
+
+ @Override
+ public boolean isLoadedFirst() {
+ return true;
+ }
+
+ @Override
+ public TypeDescription apply(String name,
+ @MaybeNull Class<?> type,
+ TypePool typePool,
+ AgentBuilder.CircularityLock circularityLock,
+ @MaybeNull ClassLoader classLoader,
+ @MaybeNull JavaModule module) {
+ // use cache first
+ TypePool.CacheProvider cacheProvider = getCacheProvider(classLoader);
+ TypePool.Resolution resolution = cacheProvider.find(name);
+ if (resolution != null && resolution.isResolved()) {
+ return resolution.resolve();
+ }
+ // do as AgentBuilder.DescriptionStrategy.Default.HYBRID
+ return delegate.apply(name, type, typePool, circularityLock, classLoader, module);
Review Comment:
> @lujiajing1126 Can you provide the memory analysis data of these two caching ways?
I will check this issue soon
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Optimize bytebuddy type description performance [skywalking-java]
Posted by "kylixs (via GitHub)" <gi...@apache.org>.
kylixs commented on PR #637:
URL: https://github.com/apache/skywalking-java/pull/637#issuecomment-1790385650
@lujiajing1126 Please check the startup time of current version.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Optimize bytebuddy type description performance [skywalking-java]
Posted by "wu-sheng (via GitHub)" <gi...@apache.org>.
wu-sheng merged PR #637:
URL: https://github.com/apache/skywalking-java/pull/637
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Optimize bytebuddy type description performance [skywalking-java]
Posted by "lujiajing1126 (via GitHub)" <gi...@apache.org>.
lujiajing1126 commented on code in PR #637:
URL: https://github.com/apache/skywalking-java/pull/637#discussion_r1375601589
##########
apm-sniffer/bytebuddy-patch/src/main/java/net/bytebuddy/agent/builder/SWDescriptionStrategy.java:
##########
@@ -0,0 +1,64 @@
+/*
+ * 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 net.bytebuddy.agent.builder;
+
+import net.bytebuddy.agent.builder.AgentBuilder.PoolStrategy.WithTypePoolCache.Simple;
+import net.bytebuddy.description.type.TypeDescription;
+import net.bytebuddy.pool.TypePool;
+import net.bytebuddy.utility.JavaModule;
+import net.bytebuddy.utility.nullability.MaybeNull;
+
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * A DescriptionStrategy with interoperable cache provider.
+ */
+public class SWDescriptionStrategy implements AgentBuilder.DescriptionStrategy {
+
+ private Simple typePoolCache = new Simple(new ConcurrentHashMap<>());
+
+ private AgentBuilder.DescriptionStrategy delegate = AgentBuilder.DescriptionStrategy.Default.HYBRID;
+
+ @Override
+ public boolean isLoadedFirst() {
+ return true;
+ }
+
+ @Override
+ public TypeDescription apply(String name,
+ @MaybeNull Class<?> type,
+ TypePool typePool,
+ AgentBuilder.CircularityLock circularityLock,
+ @MaybeNull ClassLoader classLoader,
+ @MaybeNull JavaModule module) {
+ // use cache first
+ TypePool.CacheProvider cacheProvider = getCacheProvider(classLoader);
+ TypePool.Resolution resolution = cacheProvider.find(name);
+ if (resolution != null && resolution.isResolved()) {
+ return resolution.resolve();
+ }
+ // do as AgentBuilder.DescriptionStrategy.Default.HYBRID
+ return delegate.apply(name, type, typePool, circularityLock, classLoader, module);
Review Comment:
What about moving cache registration here?
```java
@Override
public TypeDescription apply(String name,
@MaybeNull Class<?> type,
TypePool typePool,
AgentBuilder.CircularityLock circularityLock,
@MaybeNull ClassLoader classLoader,
@MaybeNull JavaModule module) {
// use cache first
TypePool.CacheProvider cacheProvider = typePoolCache.locate(classLoader);
TypePool.Resolution resolution = cacheProvider.find(name);
if (resolution != null && resolution.isResolved()) {
return resolution.resolve();
}
// do as AgentBuilder.DescriptionStrategy.Default.HYBRID
return this.delegateResolve(name, type, typePool, circularityLock, classLoader, module);
}
TypeDescription delegateResolve(String name,
@MaybeNull Class<?> type,
TypePool typePool,
AgentBuilder.CircularityLock circularityLock,
@MaybeNull ClassLoader classLoader,
@MaybeNull JavaModule module) {
TypeDescription desc = this.delegate.apply(name, type, typePool, circularityLock, classLoader, module);
// cache it
typePoolCache.locate(classLoader).register(name, new TypePool.Resolution.Simple(desc));
return desc;
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Optimize bytebuddy type description performance [skywalking-java]
Posted by "wu-sheng (via GitHub)" <gi...@apache.org>.
wu-sheng commented on code in PR #637:
URL: https://github.com/apache/skywalking-java/pull/637#discussion_r1380337085
##########
apm-sniffer/bytebuddy-patch/src/main/java/net/bytebuddy/agent/builder/SWDescriptionStrategy.java:
##########
@@ -0,0 +1,335 @@
+/*
+ * 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 net.bytebuddy.agent.builder;
+
+import net.bytebuddy.description.annotation.AnnotationList;
+import net.bytebuddy.description.field.FieldDescription;
+import net.bytebuddy.description.field.FieldList;
+import net.bytebuddy.description.method.MethodDescription;
+import net.bytebuddy.description.method.MethodList;
+import net.bytebuddy.description.type.*;
+import net.bytebuddy.dynamic.TargetType;
+import net.bytebuddy.implementation.bytecode.StackSize;
+import net.bytebuddy.pool.TypePool;
+import net.bytebuddy.utility.JavaModule;
+import net.bytebuddy.utility.nullability.MaybeNull;
+import org.apache.skywalking.apm.agent.core.plugin.AbstractClassEnhancePluginDefine;
+import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
+
+import java.io.Serializable;
+import java.lang.annotation.Annotation;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * A DescriptionStrategy to get the original class description by removing dynamic field and method tokens
+ * generated by SkyWalking.
+ */
+public class SWDescriptionStrategy implements AgentBuilder.DescriptionStrategy {
+
+ /**
+ * A cache of type descriptions for commonly used types to avoid unnecessary allocations.
+ */
+ private static final Map<Class<?>, TypeDescription> TYPE_CACHE;
+
+ /*
+ * Initializes the type cache.
+ */
+ static {
+ TYPE_CACHE = new HashMap<Class<?>, TypeDescription>();
+ TYPE_CACHE.put(TargetType.class, new TypeDescription.ForLoadedType(TargetType.class));
+ TYPE_CACHE.put(Class.class, new TypeDescription.ForLoadedType(Class.class));
+ TYPE_CACHE.put(Throwable.class, new TypeDescription.ForLoadedType(Throwable.class));
+ TYPE_CACHE.put(Annotation.class, new TypeDescription.ForLoadedType(Annotation.class));
+ TYPE_CACHE.put(Object.class, new TypeDescription.ForLoadedType(Object.class));
+ TYPE_CACHE.put(String.class, new TypeDescription.ForLoadedType(String.class));
+ TYPE_CACHE.put(Boolean.class, new TypeDescription.ForLoadedType(Boolean.class));
+ TYPE_CACHE.put(Byte.class, new TypeDescription.ForLoadedType(Byte.class));
+ TYPE_CACHE.put(Short.class, new TypeDescription.ForLoadedType(Short.class));
+ TYPE_CACHE.put(Character.class, new TypeDescription.ForLoadedType(Character.class));
+ TYPE_CACHE.put(Integer.class, new TypeDescription.ForLoadedType(Integer.class));
+ TYPE_CACHE.put(Long.class, new TypeDescription.ForLoadedType(Long.class));
+ TYPE_CACHE.put(Float.class, new TypeDescription.ForLoadedType(Float.class));
+ TYPE_CACHE.put(Double.class, new TypeDescription.ForLoadedType(Double.class));
+ TYPE_CACHE.put(void.class, new TypeDescription.ForLoadedType(void.class));
+ TYPE_CACHE.put(boolean.class, new TypeDescription.ForLoadedType(boolean.class));
+ TYPE_CACHE.put(byte.class, new TypeDescription.ForLoadedType(byte.class));
+ TYPE_CACHE.put(short.class, new TypeDescription.ForLoadedType(short.class));
+ TYPE_CACHE.put(char.class, new TypeDescription.ForLoadedType(char.class));
+ TYPE_CACHE.put(int.class, new TypeDescription.ForLoadedType(int.class));
+ TYPE_CACHE.put(long.class, new TypeDescription.ForLoadedType(long.class));
+ TYPE_CACHE.put(float.class, new TypeDescription.ForLoadedType(float.class));
+ TYPE_CACHE.put(double.class, new TypeDescription.ForLoadedType(double.class));
+ }
+
+ private AgentBuilder.DescriptionStrategy delegate = Default.HYBRID;
+
+ private String nameTrait;
+
+ public SWDescriptionStrategy(String nameTrait) {
+ this.nameTrait = nameTrait;
+ }
+
+ @Override
+ public boolean isLoadedFirst() {
+ return true;
+ }
+
+ @Override
+ public TypeDescription apply(String name,
+ @MaybeNull Class<?> type,
+ TypePool typePool,
+ AgentBuilder.CircularityLock circularityLock,
+ @MaybeNull ClassLoader classLoader,
+ @MaybeNull JavaModule module) {
+ // find from type cache
+ if (type != null) {
+ TypeDescription typeDescription = TYPE_CACHE.get(type);
+ if (typeDescription != null) {
+ return typeDescription;
+ }
+ }
+ // wrap result
+ return new SWTypeDescriptionWrapper(delegate.apply(name, type, typePool, circularityLock, classLoader, module), nameTrait);
+ }
+
+ /**
+ * A TypeDescription wrapper to remove fields, methods, interface generated by SkyWalking.
+ */
+ static class SWTypeDescriptionWrapper extends TypeDescription.AbstractBase implements Serializable {
Review Comment:
You could build a separate default class, rather than `static inner class`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Optimize bytebuddy type description performance [skywalking-java]
Posted by "kylixs (via GitHub)" <gi...@apache.org>.
kylixs commented on PR #637:
URL: https://github.com/apache/skywalking-java/pull/637#issuecomment-1786538960
Found OOM error in asynchttpclient-scenario, may due to caching strategy:
```
ERROR 2023-10-31 06:33:32.351 http-nio-8080-exec-2 SkyWalkingAgent : Enhance class java.util.concurrent.CompletableFuture error.
java.lang.OutOfMemoryError: Java heap space
at org.apache.skywalking.apm.dependencies.net.bytebuddy.pool.TypePool$Default$TypeExtractor$MethodExtractor.<init>(TypePool.java:8707)
at org.apache.skywalking.apm.dependencies.net.bytebuddy.pool.TypePool$Default$TypeExtractor.visitMethod(TypePool.java:8238)
at org.apache.skywalking.apm.dependencies.net.bytebuddy.jar.asm.ClassReader.readMethod(ClassReader.java:1353)
at org.apache.skywalking.apm.dependencies.net.bytebuddy.jar.asm.ClassReader.accept(ClassReader.java:744)
at org.apache.skywalking.apm.dependencies.net.bytebuddy.jar.asm.ClassReader.accept(ClassReader.java:424)
at org.apache.skywalking.apm.dependencies.net.bytebuddy.pool.TypePool$Default.parse(TypePool.java:881)
at org.apache.skywalking.apm.dependencies.net.bytebuddy.pool.TypePool$Default.doDescribe(TypePool.java:865)
at org.apache.skywalking.apm.dependencies.net.bytebuddy.pool.TypePool$Default$WithLazyResolution.access$001(TypePool.java:945)
at org.apache.skywalking.apm.dependencies.net.bytebuddy.pool.TypePool$Default$WithLazyResolution.doResolve(TypePool.java:1043)
at org.apache.skywalking.apm.dependencies.net.bytebuddy.pool.TypePool$Default$WithLazyResolution$LazyTypeDescription.delegate(TypePool.java:1112)
at org.apache.skywalking.apm.dependencies.net.bytebuddy.description.type.TypeDescription$AbstractBase$OfSimpleType$WithDelegation.getModifiers(TypeDescription.java:8535)
at org.apache.skywalking.apm.dependencies.net.bytebuddy.matcher.ModifierMatcher.doMatch(ModifierMatcher.java:60)
at org.apache.skywalking.apm.dependencies.net.bytebuddy.matcher.ModifierMatcher.doMatch(ModifierMatcher.java:27)
at org.apache.skywalking.apm.dependencies.net.bytebuddy.matcher.ElementMatcher$Junction$ForNonNullValues.matches(ElementMatcher.java:249)
at org.apache.skywalking.apm.dependencies.net.bytebuddy.matcher.ElementMatcher$Junction$Disjunction.matches(ElementMatcher.java:214)
at org.apache.skywalking.apm.dependencies.net.bytebuddy.agent.builder.AgentBuilder$RawMatcher$ForElementMatchers.matches(AgentBuilder.java:1880)
at org.apache.skywalking.apm.dependencies.net.bytebuddy.agent.builder.AgentBuilder$Default$ExecutingTransformer.doTransform(AgentBuilder.java:12200)
at org.apache.skywalking.apm.dependencies.net.bytebuddy.agent.builder.AgentBuilder$Default$ExecutingTransformer.transform(AgentBuilder.java:12160)
at org.apache.skywalking.apm.dependencies.net.bytebuddy.agent.builder.AgentBuilder$Default$ExecutingTransformer.access$1800(AgentBuilder.java:11869)
at org.apache.skywalking.apm.dependencies.net.bytebuddy.agent.builder.AgentBuilder$Default$ExecutingTransformer$LegacyVmDispatcher.run(AgentBuilder.java:12567)
at org.apache.skywalking.apm.dependencies.net.bytebuddy.agent.builder.AgentBuilder$Default$ExecutingTransformer$LegacyVmDispatcher.run(AgentBuilder.java:12507)
at java.security.AccessController.doPrivileged(Native Method)
at org.apache.skywalking.apm.dependencies.net.bytebuddy.agent.builder.AgentBuilder$Default$ExecutingTransformer.doPrivileged(AgentBuilder.java)
at org.apache.skywalking.apm.dependencies.net.bytebuddy.agent.builder.AgentBuilder$Default$ExecutingTransformer.transform(AgentBuilder.java:12069)
at sun.instrument.TransformerManager.transform(TransformerManager.java:188)
at sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:428)
at java.lang.ClassLoader.findBootstrapClass(Native Method)
at java.lang.ClassLoader.findBootstrapClassOrNull(ClassLoader.java:1008)
at java.lang.ClassLoader.loadClass(ClassLoader.java:407)
at java.lang.ClassLoader.loadClass(ClassLoader.java:405)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
at java.lang.ClassLoader.loadClass(ClassLoader.java:405)
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Optimize bytebuddy type description performance [skywalking-java]
Posted by "lujiajing1126 (via GitHub)" <gi...@apache.org>.
lujiajing1126 commented on PR #637:
URL: https://github.com/apache/skywalking-java/pull/637#issuecomment-1784381935
And what about raising a PR to the ByteBuddy to add so-called CACHE_FIRST policy?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Optimize bytebuddy type description performance [skywalking-java]
Posted by "kylixs (via GitHub)" <gi...@apache.org>.
kylixs commented on code in PR #637:
URL: https://github.com/apache/skywalking-java/pull/637#discussion_r1375622249
##########
apm-sniffer/bytebuddy-patch/src/main/java/net/bytebuddy/agent/builder/SWDescriptionStrategy.java:
##########
@@ -0,0 +1,64 @@
+/*
+ * 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 net.bytebuddy.agent.builder;
+
+import net.bytebuddy.agent.builder.AgentBuilder.PoolStrategy.WithTypePoolCache.Simple;
+import net.bytebuddy.description.type.TypeDescription;
+import net.bytebuddy.pool.TypePool;
+import net.bytebuddy.utility.JavaModule;
+import net.bytebuddy.utility.nullability.MaybeNull;
+
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * A DescriptionStrategy with interoperable cache provider.
+ */
+public class SWDescriptionStrategy implements AgentBuilder.DescriptionStrategy {
+
+ private Simple typePoolCache = new Simple(new ConcurrentHashMap<>());
+
+ private AgentBuilder.DescriptionStrategy delegate = AgentBuilder.DescriptionStrategy.Default.HYBRID;
+
+ @Override
+ public boolean isLoadedFirst() {
+ return true;
+ }
+
+ @Override
+ public TypeDescription apply(String name,
+ @MaybeNull Class<?> type,
+ TypePool typePool,
+ AgentBuilder.CircularityLock circularityLock,
+ @MaybeNull ClassLoader classLoader,
+ @MaybeNull JavaModule module) {
+ // use cache first
+ TypePool.CacheProvider cacheProvider = getCacheProvider(classLoader);
+ TypePool.Resolution resolution = cacheProvider.find(name);
+ if (resolution != null && resolution.isResolved()) {
+ return resolution.resolve();
+ }
+ // do as AgentBuilder.DescriptionStrategy.Default.HYBRID
+ return delegate.apply(name, type, typePool, circularityLock, classLoader, module);
Review Comment:
@lujiajing1126 Can you provide the memory analysis data of these two caching ways?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Optimize bytebuddy type description performance [skywalking-java]
Posted by "wu-sheng (via GitHub)" <gi...@apache.org>.
wu-sheng commented on PR #637:
URL: https://github.com/apache/skywalking-java/pull/637#issuecomment-1786700660
> Too many classloader instances, every one has a `CacheProvider` : 
>
> The top 2 cache providers occupy 224 MB of memory, this is the key point. 
But, too many classloaders should not be our expected cases. And an async HTTP client should not do this either. That is the thing confusing me.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Optimize bytebuddy type description performance [skywalking-java]
Posted by "wu-sheng (via GitHub)" <gi...@apache.org>.
wu-sheng commented on PR #637:
URL: https://github.com/apache/skywalking-java/pull/637#issuecomment-1790832583
> 3\. SWTypeDescriptionWrapper
@lujiajing1126 Please let us know whether this works.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Optimize bytebuddy type description performance [skywalking-java]
Posted by "wu-sheng (via GitHub)" <gi...@apache.org>.
wu-sheng commented on code in PR #637:
URL: https://github.com/apache/skywalking-java/pull/637#discussion_r1375179962
##########
apm-sniffer/bytebuddy-patch/src/main/java/net/bytebuddy/agent/builder/SWDescriptionStrategy.java:
##########
@@ -0,0 +1,64 @@
+/*
+ * 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 net.bytebuddy.agent.builder;
+
+import net.bytebuddy.agent.builder.AgentBuilder.PoolStrategy.WithTypePoolCache.Simple;
+import net.bytebuddy.description.type.TypeDescription;
+import net.bytebuddy.pool.TypePool;
+import net.bytebuddy.utility.JavaModule;
+import net.bytebuddy.utility.nullability.MaybeNull;
+
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * A DescriptionStrategy with interoperable cache provider.
+ */
+public class SWDescriptionStrategy implements AgentBuilder.DescriptionStrategy {
+
+ private Simple typePoolCache = new Simple(new ConcurrentHashMap<>());
+
+ private AgentBuilder.DescriptionStrategy delegate = AgentBuilder.DescriptionStrategy.Default.HYBRID;
+
+ @Override
+ public boolean isLoadedFirst() {
+ return true;
+ }
+
+ @Override
+ public TypeDescription apply(String name,
+ @MaybeNull Class<?> type,
+ TypePool typePool,
+ AgentBuilder.CircularityLock circularityLock,
+ @MaybeNull ClassLoader classLoader,
+ @MaybeNull JavaModule module) {
+ // use cache first
+ TypePool.CacheProvider cacheProvider = getCacheProvider(classLoader);
+ TypePool.Resolution resolution = cacheProvider.find(name);
+ if (resolution != null && resolution.isResolved()) {
+ return resolution.resolve();
+ }
+ // do as AgentBuilder.DescriptionStrategy.Default.HYBRID
+ return delegate.apply(name, type, typePool, circularityLock, classLoader, module);
+ }
Review Comment:
What is the diff between this and pool first?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Optimize bytebuddy type description performance [skywalking-java]
Posted by "lujiajing1126 (via GitHub)" <gi...@apache.org>.
lujiajing1126 commented on code in PR #637:
URL: https://github.com/apache/skywalking-java/pull/637#discussion_r1375601589
##########
apm-sniffer/bytebuddy-patch/src/main/java/net/bytebuddy/agent/builder/SWDescriptionStrategy.java:
##########
@@ -0,0 +1,64 @@
+/*
+ * 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 net.bytebuddy.agent.builder;
+
+import net.bytebuddy.agent.builder.AgentBuilder.PoolStrategy.WithTypePoolCache.Simple;
+import net.bytebuddy.description.type.TypeDescription;
+import net.bytebuddy.pool.TypePool;
+import net.bytebuddy.utility.JavaModule;
+import net.bytebuddy.utility.nullability.MaybeNull;
+
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * A DescriptionStrategy with interoperable cache provider.
+ */
+public class SWDescriptionStrategy implements AgentBuilder.DescriptionStrategy {
+
+ private Simple typePoolCache = new Simple(new ConcurrentHashMap<>());
+
+ private AgentBuilder.DescriptionStrategy delegate = AgentBuilder.DescriptionStrategy.Default.HYBRID;
+
+ @Override
+ public boolean isLoadedFirst() {
+ return true;
+ }
+
+ @Override
+ public TypeDescription apply(String name,
+ @MaybeNull Class<?> type,
+ TypePool typePool,
+ AgentBuilder.CircularityLock circularityLock,
+ @MaybeNull ClassLoader classLoader,
+ @MaybeNull JavaModule module) {
+ // use cache first
+ TypePool.CacheProvider cacheProvider = getCacheProvider(classLoader);
+ TypePool.Resolution resolution = cacheProvider.find(name);
+ if (resolution != null && resolution.isResolved()) {
+ return resolution.resolve();
+ }
+ // do as AgentBuilder.DescriptionStrategy.Default.HYBRID
+ return delegate.apply(name, type, typePool, circularityLock, classLoader, module);
Review Comment:
What about moving cache registration here?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Optimize bytebuddy type description performance [skywalking-java]
Posted by "kylixs (via GitHub)" <gi...@apache.org>.
kylixs commented on PR #637:
URL: https://github.com/apache/skywalking-java/pull/637#issuecomment-1790604201
> Could you provide docs to descibe your changes?
The PR description has been updated with details.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Optimize bytebuddy type description performance [skywalking-java]
Posted by "kylixs (via GitHub)" <gi...@apache.org>.
kylixs commented on code in PR #637:
URL: https://github.com/apache/skywalking-java/pull/637#discussion_r1375617383
##########
apm-sniffer/bytebuddy-patch/src/main/java/net/bytebuddy/agent/builder/SWDescriptionStrategy.java:
##########
@@ -0,0 +1,64 @@
+/*
+ * 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 net.bytebuddy.agent.builder;
+
+import net.bytebuddy.agent.builder.AgentBuilder.PoolStrategy.WithTypePoolCache.Simple;
+import net.bytebuddy.description.type.TypeDescription;
+import net.bytebuddy.pool.TypePool;
+import net.bytebuddy.utility.JavaModule;
+import net.bytebuddy.utility.nullability.MaybeNull;
+
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * A DescriptionStrategy with interoperable cache provider.
+ */
+public class SWDescriptionStrategy implements AgentBuilder.DescriptionStrategy {
+
+ private Simple typePoolCache = new Simple(new ConcurrentHashMap<>());
+
+ private AgentBuilder.DescriptionStrategy delegate = AgentBuilder.DescriptionStrategy.Default.HYBRID;
+
+ @Override
+ public boolean isLoadedFirst() {
+ return true;
+ }
+
+ @Override
+ public TypeDescription apply(String name,
+ @MaybeNull Class<?> type,
+ TypePool typePool,
+ AgentBuilder.CircularityLock circularityLock,
+ @MaybeNull ClassLoader classLoader,
+ @MaybeNull JavaModule module) {
+ // use cache first
+ TypePool.CacheProvider cacheProvider = getCacheProvider(classLoader);
+ TypePool.Resolution resolution = cacheProvider.find(name);
+ if (resolution != null && resolution.isResolved()) {
+ return resolution.resolve();
+ }
+ // do as AgentBuilder.DescriptionStrategy.Default.HYBRID
+ return delegate.apply(name, type, typePool, circularityLock, classLoader, module);
Review Comment:
Many non-enhanced classes also require type descriptions during the ByteBuddy processing. These classes do not actually require a cache and can save memory. You can compare the heap memory difference between the two caching ways, if the difference is not big, I think we can also put it here, the responsibility is clearer.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Optimize bytebuddy type description performance [skywalking-java]
Posted by "wu-sheng (via GitHub)" <gi...@apache.org>.
wu-sheng commented on PR #637:
URL: https://github.com/apache/skywalking-java/pull/637#issuecomment-1786548943
Is this relative to the latest change? Or the previous commit is the same? Could you explain why OOM?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Optimize bytebuddy type description performance [skywalking-java]
Posted by "wu-sheng (via GitHub)" <gi...@apache.org>.
wu-sheng commented on PR #637:
URL: https://github.com/apache/skywalking-java/pull/637#issuecomment-1786662716
How is this possible to have so many CacheProvider? 8k instances?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Optimize bytebuddy type description performance [skywalking-java]
Posted by "kylixs (via GitHub)" <gi...@apache.org>.
kylixs commented on code in PR #637:
URL: https://github.com/apache/skywalking-java/pull/637#discussion_r1375617383
##########
apm-sniffer/bytebuddy-patch/src/main/java/net/bytebuddy/agent/builder/SWDescriptionStrategy.java:
##########
@@ -0,0 +1,64 @@
+/*
+ * 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 net.bytebuddy.agent.builder;
+
+import net.bytebuddy.agent.builder.AgentBuilder.PoolStrategy.WithTypePoolCache.Simple;
+import net.bytebuddy.description.type.TypeDescription;
+import net.bytebuddy.pool.TypePool;
+import net.bytebuddy.utility.JavaModule;
+import net.bytebuddy.utility.nullability.MaybeNull;
+
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * A DescriptionStrategy with interoperable cache provider.
+ */
+public class SWDescriptionStrategy implements AgentBuilder.DescriptionStrategy {
+
+ private Simple typePoolCache = new Simple(new ConcurrentHashMap<>());
+
+ private AgentBuilder.DescriptionStrategy delegate = AgentBuilder.DescriptionStrategy.Default.HYBRID;
+
+ @Override
+ public boolean isLoadedFirst() {
+ return true;
+ }
+
+ @Override
+ public TypeDescription apply(String name,
+ @MaybeNull Class<?> type,
+ TypePool typePool,
+ AgentBuilder.CircularityLock circularityLock,
+ @MaybeNull ClassLoader classLoader,
+ @MaybeNull JavaModule module) {
+ // use cache first
+ TypePool.CacheProvider cacheProvider = getCacheProvider(classLoader);
+ TypePool.Resolution resolution = cacheProvider.find(name);
+ if (resolution != null && resolution.isResolved()) {
+ return resolution.resolve();
+ }
+ // do as AgentBuilder.DescriptionStrategy.Default.HYBRID
+ return delegate.apply(name, type, typePool, circularityLock, classLoader, module);
Review Comment:
Many non-enhanced classes also require type descriptions during the ByteBuddy processing. These classes do not actually require a cache and can save memory. You can compare the heap memory difference between the two caching methods, if the difference is not big, I think you can also put it here, the responsibility is clearer.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Optimize bytebuddy type description performance [skywalking-java]
Posted by "kylixs (via GitHub)" <gi...@apache.org>.
kylixs commented on PR #637:
URL: https://github.com/apache/skywalking-java/pull/637#issuecomment-1784404582
> And what about raising a PR to the ByteBuddy to add so-called CACHE_FIRST policy?
I think so. It is currently more of a cache delegation and should be modified to avoid relying on other strategies.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Optimize bytebuddy type description performance [skywalking-java]
Posted by "wu-sheng (via GitHub)" <gi...@apache.org>.
wu-sheng commented on PR #637:
URL: https://github.com/apache/skywalking-java/pull/637#issuecomment-1790471069
Could you provide docs to descibe your changes?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Optimize bytebuddy type description performance [skywalking-java]
Posted by "lujiajing1126 (via GitHub)" <gi...@apache.org>.
lujiajing1126 commented on PR #637:
URL: https://github.com/apache/skywalking-java/pull/637#issuecomment-1791787179
> > 3. SWTypeDescriptionWrapper
>
> @lujiajing1126 Please let us know whether this works.
The startup time is almost the same as the latest 8.x version while the heap sizes are also similar.
Thanks for your work @kylixs
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Optimize bytebuddy type description performance [skywalking-java]
Posted by "wu-sheng (via GitHub)" <gi...@apache.org>.
wu-sheng commented on code in PR #637:
URL: https://github.com/apache/skywalking-java/pull/637#discussion_r1380338035
##########
apm-sniffer/bytebuddy-patch/src/main/java/net/bytebuddy/agent/builder/SWDescriptionStrategy.java:
##########
@@ -0,0 +1,335 @@
+/*
+ * 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 net.bytebuddy.agent.builder;
+
+import net.bytebuddy.description.annotation.AnnotationList;
+import net.bytebuddy.description.field.FieldDescription;
+import net.bytebuddy.description.field.FieldList;
+import net.bytebuddy.description.method.MethodDescription;
+import net.bytebuddy.description.method.MethodList;
+import net.bytebuddy.description.type.*;
+import net.bytebuddy.dynamic.TargetType;
+import net.bytebuddy.implementation.bytecode.StackSize;
+import net.bytebuddy.pool.TypePool;
+import net.bytebuddy.utility.JavaModule;
+import net.bytebuddy.utility.nullability.MaybeNull;
+import org.apache.skywalking.apm.agent.core.plugin.AbstractClassEnhancePluginDefine;
+import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
+
+import java.io.Serializable;
+import java.lang.annotation.Annotation;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * A DescriptionStrategy to get the original class description by removing dynamic field and method tokens
+ * generated by SkyWalking.
+ */
+public class SWDescriptionStrategy implements AgentBuilder.DescriptionStrategy {
+
+ /**
+ * A cache of type descriptions for commonly used types to avoid unnecessary allocations.
+ */
+ private static final Map<Class<?>, TypeDescription> TYPE_CACHE;
+
+ /*
+ * Initializes the type cache.
+ */
+ static {
+ TYPE_CACHE = new HashMap<Class<?>, TypeDescription>();
+ TYPE_CACHE.put(TargetType.class, new TypeDescription.ForLoadedType(TargetType.class));
+ TYPE_CACHE.put(Class.class, new TypeDescription.ForLoadedType(Class.class));
+ TYPE_CACHE.put(Throwable.class, new TypeDescription.ForLoadedType(Throwable.class));
+ TYPE_CACHE.put(Annotation.class, new TypeDescription.ForLoadedType(Annotation.class));
+ TYPE_CACHE.put(Object.class, new TypeDescription.ForLoadedType(Object.class));
+ TYPE_CACHE.put(String.class, new TypeDescription.ForLoadedType(String.class));
+ TYPE_CACHE.put(Boolean.class, new TypeDescription.ForLoadedType(Boolean.class));
+ TYPE_CACHE.put(Byte.class, new TypeDescription.ForLoadedType(Byte.class));
+ TYPE_CACHE.put(Short.class, new TypeDescription.ForLoadedType(Short.class));
+ TYPE_CACHE.put(Character.class, new TypeDescription.ForLoadedType(Character.class));
+ TYPE_CACHE.put(Integer.class, new TypeDescription.ForLoadedType(Integer.class));
+ TYPE_CACHE.put(Long.class, new TypeDescription.ForLoadedType(Long.class));
+ TYPE_CACHE.put(Float.class, new TypeDescription.ForLoadedType(Float.class));
+ TYPE_CACHE.put(Double.class, new TypeDescription.ForLoadedType(Double.class));
+ TYPE_CACHE.put(void.class, new TypeDescription.ForLoadedType(void.class));
+ TYPE_CACHE.put(boolean.class, new TypeDescription.ForLoadedType(boolean.class));
+ TYPE_CACHE.put(byte.class, new TypeDescription.ForLoadedType(byte.class));
+ TYPE_CACHE.put(short.class, new TypeDescription.ForLoadedType(short.class));
+ TYPE_CACHE.put(char.class, new TypeDescription.ForLoadedType(char.class));
+ TYPE_CACHE.put(int.class, new TypeDescription.ForLoadedType(int.class));
+ TYPE_CACHE.put(long.class, new TypeDescription.ForLoadedType(long.class));
+ TYPE_CACHE.put(float.class, new TypeDescription.ForLoadedType(float.class));
+ TYPE_CACHE.put(double.class, new TypeDescription.ForLoadedType(double.class));
+ }
+
+ private AgentBuilder.DescriptionStrategy delegate = Default.HYBRID;
+
+ private String nameTrait;
+
+ public SWDescriptionStrategy(String nameTrait) {
+ this.nameTrait = nameTrait;
+ }
+
+ @Override
+ public boolean isLoadedFirst() {
+ return true;
+ }
+
+ @Override
+ public TypeDescription apply(String name,
+ @MaybeNull Class<?> type,
+ TypePool typePool,
+ AgentBuilder.CircularityLock circularityLock,
+ @MaybeNull ClassLoader classLoader,
+ @MaybeNull JavaModule module) {
+ // find from type cache
+ if (type != null) {
+ TypeDescription typeDescription = TYPE_CACHE.get(type);
+ if (typeDescription != null) {
+ return typeDescription;
+ }
+ }
+ // wrap result
+ return new SWTypeDescriptionWrapper(delegate.apply(name, type, typePool, circularityLock, classLoader, module), nameTrait);
+ }
+
+ /**
+ * A TypeDescription wrapper to remove fields, methods, interface generated by SkyWalking.
+ */
+ static class SWTypeDescriptionWrapper extends TypeDescription.AbstractBase implements Serializable {
+
+ /**
+ * The class's serial version UID.
+ */
+ private static final long serialVersionUID = 1L;
Review Comment:
```suggestion
/**
* The class' serial version UID.
*/
private static final long serialVersionUID = 1L;
```
Is the serialVersionUID required by some places?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Optimize bytebuddy type description performance [skywalking-java]
Posted by "wu-sheng (via GitHub)" <gi...@apache.org>.
wu-sheng commented on PR #637:
URL: https://github.com/apache/skywalking-java/pull/637#issuecomment-1790955901
The description is very clean. Thanks. Could you list the new generated class names, field names, and method names styles?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Optimize bytebuddy type description performance [skywalking-java]
Posted by "wu-sheng (via GitHub)" <gi...@apache.org>.
wu-sheng commented on code in PR #637:
URL: https://github.com/apache/skywalking-java/pull/637#discussion_r1375619567
##########
apm-sniffer/bytebuddy-patch/src/main/java/net/bytebuddy/agent/builder/SWDescriptionStrategy.java:
##########
@@ -0,0 +1,64 @@
+/*
+ * 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 net.bytebuddy.agent.builder;
+
+import net.bytebuddy.agent.builder.AgentBuilder.PoolStrategy.WithTypePoolCache.Simple;
+import net.bytebuddy.description.type.TypeDescription;
+import net.bytebuddy.pool.TypePool;
+import net.bytebuddy.utility.JavaModule;
+import net.bytebuddy.utility.nullability.MaybeNull;
+
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * A DescriptionStrategy with interoperable cache provider.
+ */
+public class SWDescriptionStrategy implements AgentBuilder.DescriptionStrategy {
+
+ private Simple typePoolCache = new Simple(new ConcurrentHashMap<>());
+
+ private AgentBuilder.DescriptionStrategy delegate = AgentBuilder.DescriptionStrategy.Default.HYBRID;
+
+ @Override
+ public boolean isLoadedFirst() {
+ return true;
+ }
+
+ @Override
+ public TypeDescription apply(String name,
+ @MaybeNull Class<?> type,
+ TypePool typePool,
+ AgentBuilder.CircularityLock circularityLock,
+ @MaybeNull ClassLoader classLoader,
+ @MaybeNull JavaModule module) {
+ // use cache first
+ TypePool.CacheProvider cacheProvider = getCacheProvider(classLoader);
+ TypePool.Resolution resolution = cacheProvider.find(name);
+ if (resolution != null && resolution.isResolved()) {
+ return resolution.resolve();
+ }
+ // do as AgentBuilder.DescriptionStrategy.Default.HYBRID
+ return delegate.apply(name, type, typePool, circularityLock, classLoader, module);
Review Comment:
I think to control memory cost is more important.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Optimize bytebuddy type description performance [skywalking-java]
Posted by "wu-sheng (via GitHub)" <gi...@apache.org>.
wu-sheng commented on PR #637:
URL: https://github.com/apache/skywalking-java/pull/637#issuecomment-1783723358
I talked with @lujiajing1126 He will check on his local env, and feedback.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Optimize bytebuddy type description performance [skywalking-java]
Posted by "kylixs (via GitHub)" <gi...@apache.org>.
kylixs commented on PR #637:
URL: https://github.com/apache/skywalking-java/pull/637#issuecomment-1786716470
> But, too many classloaders should not be our expected cases. And an async HTTP client should not do this either. That is the thing confusing me.
Every `TypePool$Default$WithLazyResolution` has a cache provider, they weren't created by `SWDescriptionStrategy`!
![Uploading image.png…]()
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Optimize bytebuddy type description performance [skywalking-java]
Posted by "kylixs (via GitHub)" <gi...@apache.org>.
kylixs commented on code in PR #637:
URL: https://github.com/apache/skywalking-java/pull/637#discussion_r1375180943
##########
apm-sniffer/bytebuddy-patch/src/main/java/net/bytebuddy/agent/builder/SWDescriptionStrategy.java:
##########
@@ -0,0 +1,64 @@
+/*
+ * 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 net.bytebuddy.agent.builder;
+
+import net.bytebuddy.agent.builder.AgentBuilder.PoolStrategy.WithTypePoolCache.Simple;
+import net.bytebuddy.description.type.TypeDescription;
+import net.bytebuddy.pool.TypePool;
+import net.bytebuddy.utility.JavaModule;
+import net.bytebuddy.utility.nullability.MaybeNull;
+
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * A DescriptionStrategy with interoperable cache provider.
+ */
+public class SWDescriptionStrategy implements AgentBuilder.DescriptionStrategy {
+
+ private Simple typePoolCache = new Simple(new ConcurrentHashMap<>());
+
+ private AgentBuilder.DescriptionStrategy delegate = AgentBuilder.DescriptionStrategy.Default.HYBRID;
+
+ @Override
+ public boolean isLoadedFirst() {
+ return true;
+ }
+
+ @Override
+ public TypeDescription apply(String name,
+ @MaybeNull Class<?> type,
+ TypePool typePool,
+ AgentBuilder.CircularityLock circularityLock,
+ @MaybeNull ClassLoader classLoader,
+ @MaybeNull JavaModule module) {
+ // use cache first
+ TypePool.CacheProvider cacheProvider = getCacheProvider(classLoader);
+ TypePool.Resolution resolution = cacheProvider.find(name);
+ if (resolution != null && resolution.isResolved()) {
+ return resolution.resolve();
+ }
+ // do as AgentBuilder.DescriptionStrategy.Default.HYBRID
+ return delegate.apply(name, type, typePool, circularityLock, classLoader, module);
+ }
Review Comment:
`POOL_FIRST` strategy call `typePool.describe(..)` first, simply:
* find from cache
* parse bytecode if not found
But SWDescriptionStrategy is:
* find from cache
* do as `HYBRID` strategy, create from type class if not null, then from type pool
```java
HYBRID(true) {
/** {@inheritDoc} */
public TypeDescription apply(String name,
@MaybeNull Class<?> type,
TypePool typePool,
CircularityLock circularityLock,
@MaybeNull ClassLoader classLoader,
@MaybeNull JavaModule module) {
return type == null
? typePool.describe(name).resolve()
: TypeDescription.ForLoadedType.of(type);
}
},
POOL_FIRST(false) {
/** {@inheritDoc} */
public TypeDescription apply(String name,
@MaybeNull Class<?> type,
TypePool typePool,
CircularityLock circularityLock,
@MaybeNull ClassLoader classLoader,
@MaybeNull JavaModule module) {
TypePool.Resolution resolution = typePool.describe(name);
return resolution.isResolved() || type == null
? resolution.resolve()
: TypeDescription.ForLoadedType.of(type);
}
};
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Optimize bytebuddy type description performance [skywalking-java]
Posted by "kylixs (via GitHub)" <gi...@apache.org>.
kylixs commented on PR #637:
URL: https://github.com/apache/skywalking-java/pull/637#issuecomment-1784414710
> @kylixs Could you polish this and update change log as well?
Not quite sure what to do, more? Add `changes-9.1.0.md` and copy all changes from https://github.com/apache/skywalking-java/milestone/11?closed=1 ?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Optimize bytebuddy type description performance [skywalking-java]
Posted by "wu-sheng (via GitHub)" <gi...@apache.org>.
wu-sheng commented on PR #637:
URL: https://github.com/apache/skywalking-java/pull/637#issuecomment-1784671318
It seems there are some issues, they are failing again
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Optimize bytebuddy type description performance [skywalking-java]
Posted by "kylixs (via GitHub)" <gi...@apache.org>.
kylixs commented on PR #637:
URL: https://github.com/apache/skywalking-java/pull/637#issuecomment-1786697466
Too many classloader instances, every one has a `CacheProvider` :

The top 2 cache providers occupy 224 MB of memory, this is the key point.

--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Optimize bytebuddy type description performance [skywalking-java]
Posted by "kylixs (via GitHub)" <gi...@apache.org>.
kylixs commented on PR #637:
URL: https://github.com/apache/skywalking-java/pull/637#issuecomment-1786642163
Too many `CacheProvider` and tokens:

Heavily `TypeDescription` , method token:
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org