Skip to content

Commit 96d8109

Browse files
committed
Remove redundant object allocation in cglib proxy method calls
* Fixes gh-35542 Signed-off-by: Nurlan Turdaliev <nurlan0000@gmail.com>
1 parent 5df0821 commit 96d8109

File tree

1 file changed

+61
-18
lines changed

1 file changed

+61
-18
lines changed

spring-core/src/main/java/org/springframework/cglib/core/CodeEmitter.java

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,22 @@
2929
public class CodeEmitter extends LocalVariablesSorter {
3030
private static final Signature BOOLEAN_VALUE =
3131
TypeUtils.parseSignature("boolean booleanValue()");
32+
private static final Signature BOOLEAN_VALUE_OF =
33+
TypeUtils.parseSignature("Boolean valueOf(boolean)");
34+
private static final Signature INTEGER_VALUE_OF =
35+
TypeUtils.parseSignature("Integer valueOf(int)");
36+
private static final Signature BYTE_VALUE_OF =
37+
TypeUtils.parseSignature("Byte valueOf(byte)");
38+
private static final Signature SHORT_VALUE_OF =
39+
TypeUtils.parseSignature("Short valueOf(short)");
40+
private static final Signature LONG_VALUE_OF =
41+
TypeUtils.parseSignature("Long valueOf(long)");
42+
private static final Signature FLOAT_VALUE_OF =
43+
TypeUtils.parseSignature("Float valueOf(float)");
44+
private static final Signature DOUBLE_VALUE_OF =
45+
TypeUtils.parseSignature("Double valueOf(double)");
46+
private static final Signature CHARACTER_VALUE_OF =
47+
TypeUtils.parseSignature("Character valueOf(char)");
3248
private static final Signature CHAR_VALUE =
3349
TypeUtils.parseSignature("char charValue()");
3450
private static final Signature LONG_VALUE =
@@ -705,29 +721,56 @@ public void throw_exception(Type type, String msg) {
705721

706722
/**
707723
* If the argument is a primitive class, replaces the primitive value
708-
* on the top of the stack with the wrapped (Object) equivalent. For
709-
* example, char -> Character.
724+
* on the top of the stack with the wrapped (Object) equivalent using valueOf() methods.
725+
* For example, char -> Character.
710726
* If the class is Void, a null is pushed onto the stack instead.
711727
* @param type the class indicating the current type of the top stack value
712728
*/
713729
public void box(Type type) {
714730
if (TypeUtils.isPrimitive(type)) {
715-
if (type == Type.VOID_TYPE) {
716-
aconst_null();
717-
} else {
718-
Type boxed = TypeUtils.getBoxedType(type);
719-
new_instance(boxed);
720-
if (type.getSize() == 2) {
721-
// Pp -> Ppo -> oPpo -> ooPpo -> ooPp -> o
722-
dup_x2();
723-
dup_x2();
724-
pop();
725-
} else {
726-
// p -> po -> opo -> oop -> o
727-
dup_x1();
728-
swap();
729-
}
730-
invoke_constructor(boxed, new Signature(Constants.CONSTRUCTOR_NAME, Type.VOID_TYPE, new Type[]{ type }));
731+
switch (type.getSort()) {
732+
case Type.VOID:
733+
aconst_null();
734+
break;
735+
case Type.BOOLEAN:
736+
invoke_static(Constants.TYPE_BOOLEAN, BOOLEAN_VALUE_OF);
737+
break;
738+
case Type.INT:
739+
invoke_static(Constants.TYPE_INTEGER, INTEGER_VALUE_OF);
740+
break;
741+
case Type.BYTE:
742+
invoke_static(Constants.TYPE_BYTE, BYTE_VALUE_OF);
743+
break;
744+
case Type.SHORT:
745+
invoke_static(Constants.TYPE_SHORT, SHORT_VALUE_OF);
746+
break;
747+
case Type.LONG:
748+
invoke_static(Constants.TYPE_LONG, LONG_VALUE_OF);
749+
break;
750+
case Type.FLOAT:
751+
invoke_static(Constants.TYPE_FLOAT, FLOAT_VALUE_OF);
752+
break;
753+
case Type.DOUBLE:
754+
invoke_static(Constants.TYPE_DOUBLE, DOUBLE_VALUE_OF);
755+
break;
756+
case Type.CHAR:
757+
invoke_static(Constants.TYPE_CHARACTER, CHARACTER_VALUE_OF);
758+
break;
759+
760+
default:
761+
Type boxed = TypeUtils.getBoxedType(type);
762+
new_instance(boxed);
763+
if (type.getSize() == 2) {
764+
// Pp -> Ppo -> oPpo -> ooPpo -> ooPp -> o
765+
dup_x2();
766+
dup_x2();
767+
pop();
768+
} else {
769+
// p -> po -> opo -> oop -> o
770+
dup_x1();
771+
swap();
772+
}
773+
invoke_constructor(boxed, new Signature(Constants.CONSTRUCTOR_NAME, Type.VOID_TYPE, new Type[]{ type }));
731774
}
732775
}
733776
}

0 commit comments

Comments
 (0)