Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ abstract class AbstractBase<S extends ParameterDescription> extends FilterableLi
* {@inheritDoc}
*/
public boolean hasExplicitMetaData() {
for (ParameterDescription parameterDescription : this) {
// cache the size and make sure to avoid iterators here
// this pattern reduces the number of allocations and also the CPU usage
int size = size();
for (int i = 0; i < size; i++) {
ParameterDescription parameterDescription = get(i);
if (!parameterDescription.isNamed() || !parameterDescription.hasModifiers()) {
return false;
}
Expand All @@ -91,9 +95,12 @@ public boolean hasExplicitMetaData() {
* {@inheritDoc}
*/
public ByteCodeElement.Token.TokenList<ParameterDescription.Token> asTokenList(ElementMatcher<? super TypeDescription> matcher) {
List<ParameterDescription.Token> tokens = new ArrayList<ParameterDescription.Token>(size());
for (ParameterDescription parameterDescription : this) {
tokens.add(parameterDescription.asToken(matcher));
// cache the size and make sure to avoid iterators here
// this pattern reduces the number of allocations and also the CPU usage
int size = size();
List<ParameterDescription.Token> tokens = new ArrayList<ParameterDescription.Token>(size);
for (int i = 0; i < size; i++) {
tokens.add(get(i).asToken(matcher));
}
return new ByteCodeElement.Token.TokenList<ParameterDescription.Token>(tokens);
}
Expand All @@ -102,9 +109,12 @@ public ByteCodeElement.Token.TokenList<ParameterDescription.Token> asTokenList(E
* {@inheritDoc}
*/
public TypeList.Generic asTypeList() {
List<TypeDescription.Generic> types = new ArrayList<TypeDescription.Generic>(size());
for (ParameterDescription parameterDescription : this) {
types.add(parameterDescription.getType());
// cache the size and make sure to avoid iterators here
// this pattern reduces the number of allocations and also the CPU usage
int size = size();
List<TypeDescription.Generic> types = new ArrayList<TypeDescription.Generic>(size);
for (int i = 0; i < size; i++) {
types.add(get(i).getType());
}
return new TypeList.Generic.Explicit(types);
}
Expand All @@ -113,9 +123,12 @@ public TypeList.Generic asTypeList() {
* {@inheritDoc}
*/
public ParameterList<ParameterDescription.InDefinedShape> asDefined() {
List<ParameterDescription.InDefinedShape> declaredForms = new ArrayList<ParameterDescription.InDefinedShape>(size());
for (ParameterDescription parameterDescription : this) {
declaredForms.add(parameterDescription.asDefined());
// cache the size and make sure to avoid iterators here
// this pattern reduces the number of allocations and also the CPU usage
int size = size();
List<ParameterDescription.InDefinedShape> declaredForms = new ArrayList<ParameterDescription.InDefinedShape>(size);
for (int i = 0; i < size; i++) {
declaredForms.add(get(i).asDefined());
}
return new Explicit<ParameterDescription.InDefinedShape>(declaredForms);
}
Expand Down Expand Up @@ -143,6 +156,13 @@ abstract class ForLoadedExecutable<T> extends AbstractBase<ParameterDescription.
*/
protected final T executable;

/**
* The number of parameters of this executable.
* <p>
* It is important to cache it as calling getParameterCount() via the dispatcher has a high cost.
*/
protected final int size;

/**
* The parameter annotation source to query.
*/
Expand All @@ -156,6 +176,7 @@ abstract class ForLoadedExecutable<T> extends AbstractBase<ParameterDescription.
*/
protected ForLoadedExecutable(T executable, ParameterDescription.ForLoadedParameter.ParameterAnnotationSource parameterAnnotationSource) {
this.executable = executable;
this.size = EXECUTABLE.getParameterCount(executable);
this.parameterAnnotationSource = parameterAnnotationSource;
}

Expand Down Expand Up @@ -223,7 +244,7 @@ public static ParameterList<ParameterDescription.InDefinedShape> of(Method metho
* {@inheritDoc}
*/
public int size() {
return EXECUTABLE.getParameterCount(executable);
return size;
}

/**
Expand Down Expand Up @@ -562,6 +583,11 @@ class TypeSubstituting extends AbstractBase<ParameterDescription.InGenericShape>
*/
private final List<? extends ParameterDescription> parameterDescriptions;

/**
* The number of parameters.
*/
private final int size;

/**
* The visitor to apply to the parameter types before returning them.
*/
Expand All @@ -579,6 +605,7 @@ public TypeSubstituting(MethodDescription.InGenericShape declaringMethod,
TypeDescription.Generic.Visitor<? extends TypeDescription.Generic> visitor) {
this.declaringMethod = declaringMethod;
this.parameterDescriptions = parameterDescriptions;
this.size = parameterDescriptions.size();
this.visitor = visitor;
}

Expand All @@ -593,7 +620,7 @@ public ParameterDescription.InGenericShape get(int index) {
* {@inheritDoc}
*/
public int size() {
return parameterDescriptions.size();
return size;
}
}

Expand Down