8177107: Reduce memory footprint of java.lang.reflect.Constructor/Method

Reviewed-by: darcy, shade, coleenp
This commit is contained in:
Claes Redestad 2022-04-06 14:26:58 +00:00
parent ec205f68a8
commit a385142398
4 changed files with 22 additions and 8 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -35,6 +35,7 @@ import jdk.internal.vm.annotation.Stable;
import sun.reflect.annotation.TypeAnnotation;
import sun.reflect.annotation.TypeAnnotationParser;
import sun.reflect.generics.repository.ConstructorRepository;
import sun.reflect.generics.repository.GenericDeclRepository;
import sun.reflect.generics.factory.CoreReflectionFactory;
import sun.reflect.generics.factory.GenericsFactory;
import sun.reflect.generics.scope.ConstructorScope;
@ -244,7 +245,7 @@ public final class Constructor<T> extends Executable {
if (getSignature() != null) {
return (TypeVariable<Constructor<T>>[])getGenericInfo().getTypeParameters();
} else
return (TypeVariable<Constructor<T>>[])new TypeVariable[0];
return (TypeVariable<Constructor<T>>[])GenericDeclRepository.EMPTY_TYPE_VARS;
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -36,6 +36,7 @@ import jdk.internal.vm.annotation.IntrinsicCandidate;
import jdk.internal.vm.annotation.Stable;
import sun.reflect.annotation.ExceptionProxy;
import sun.reflect.annotation.TypeNotPresentExceptionProxy;
import sun.reflect.generics.repository.GenericDeclRepository;
import sun.reflect.generics.repository.MethodRepository;
import sun.reflect.generics.factory.CoreReflectionFactory;
import sun.reflect.generics.factory.GenericsFactory;
@ -254,7 +255,7 @@ public final class Method extends Executable {
if (getGenericSignature() != null)
return (TypeVariable<Method>[])getGenericInfo().getTypeParameters();
else
return (TypeVariable<Method>[])new TypeVariable[0];
return (TypeVariable<Method>[])GenericDeclRepository.EMPTY_TYPE_VARS;
}
/**

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -42,6 +42,8 @@ import sun.reflect.generics.visitor.Reifier;
public abstract class GenericDeclRepository<S extends Signature>
extends AbstractRepository<S> {
public static final TypeVariable<?>[] EMPTY_TYPE_VARS = new TypeVariable<?>[0];
/** The formal type parameters. Lazily initialized. */
private volatile TypeVariable<?>[] typeParameters;
@ -77,6 +79,9 @@ public abstract class GenericDeclRepository<S extends Signature>
FormalTypeParameter[] ftps = getTree().getFormalTypeParameters();
// create array to store reified subtree(s)
int length = ftps.length;
if (length == 0) {
return EMPTY_TYPE_VARS;
}
TypeVariable<?>[] typeParameters = new TypeVariable<?>[length];
// reify all subtrees
for (int i = 0; i < length; i++) {