Skip to content

Commit

Permalink
8177107: Reduce memory footprint of java.lang.reflect.Constructor/Method
Browse files Browse the repository at this point in the history
Reviewed-by: darcy, shade, coleenp
  • Loading branch information
cl4es committed Apr 6, 2022
1 parent ec205f6 commit a385142
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
13 changes: 10 additions & 3 deletions src/hotspot/share/runtime/reflection.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 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
Expand Down Expand Up @@ -728,8 +728,15 @@ static objArrayHandle get_parameter_types(const methodHandle& method,
int parameter_count,
oop* return_type,
TRAPS) {
// Allocate array holding parameter types (java.lang.Class instances)
objArrayOop m = oopFactory::new_objArray(vmClasses::Class_klass(), parameter_count, CHECK_(objArrayHandle()));
objArrayOop m;
if (parameter_count == 0) {
// Avoid allocating an array for the empty case
// Still need to parse the signature for the return type below
m = Universe::the_empty_class_array();
} else {
// Allocate array holding parameter types (java.lang.Class instances)
m = oopFactory::new_objArray(vmClasses::Class_klass(), parameter_count, CHECK_(objArrayHandle()));
}
objArrayHandle mirrors(THREAD, m);
int index = 0;
// Collect parameter types
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -35,6 +35,7 @@
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;
Expand Down Expand Up @@ -244,7 +245,7 @@ public TypeVariable<Constructor<T>>[] getTypeParameters() {
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;
}


Expand Down
5 changes: 3 additions & 2 deletions src/java.base/share/classes/java/lang/reflect/Method.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -36,6 +36,7 @@
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;
Expand Down Expand Up @@ -254,7 +255,7 @@ public TypeVariable<Method>[] getTypeParameters() {
if (getGenericSignature() != null)
return (TypeVariable<Method>[])getGenericInfo().getTypeParameters();
else
return (TypeVariable<Method>[])new TypeVariable[0];
return (TypeVariable<Method>[])GenericDeclRepository.EMPTY_TYPE_VARS;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -42,6 +42,8 @@
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;

Expand Down Expand Up @@ -77,6 +79,9 @@ private TypeVariable<?>[] computeTypeParameters() {
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++) {
Expand Down

0 comments on commit a385142

Please sign in to comment.