Skip to content

Commit 926f571

Browse files
author
James Bodkin
committed
Reduce the number of memory allocations for empty argument values
Signed-off-by: James Bodkin <[email protected]>
1 parent 48c8885 commit 926f571

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

spring-graphql/src/main/java/org/springframework/graphql/data/ArgumentValue.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
*/
5050
public final class ArgumentValue<T> {
5151

52+
private static final ArgumentValue<?> EMPTY = new ArgumentValue<>(null, false);
5253
private static final ArgumentValue<?> OMITTED = new ArgumentValue<>(null, true);
5354

5455

@@ -118,7 +119,7 @@ public void ifPresent(Consumer<? super T> action) {
118119

119120
@Override
120121
public boolean equals(Object other) {
121-
// This covers OMITTED constant
122+
// This covers EMPTY and OMITTED constant
122123
if (this == other) {
123124
return true;
124125
}
@@ -142,8 +143,9 @@ public int hashCode() {
142143
* @param <T> the type of value
143144
* @param value the value to hold in the instance
144145
*/
146+
@SuppressWarnings("unchecked")
145147
public static <T> ArgumentValue<T> ofNullable(@Nullable T value) {
146-
return new ArgumentValue<>(value, false);
148+
return value == null ? (ArgumentValue<T>) EMPTY : new ArgumentValue<>(value, false);
147149
}
148150

149151
/**

spring-graphql/src/test/java/org/springframework/graphql/data/ArgumentValueTests.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ void ifPresentShouldSkipWhenNull() {
7474
}
7575

7676
@Test
77-
7877
void ifPresentShouldSkipWhenOmitted() {
7978
AtomicBoolean called = new AtomicBoolean();
8079
ArgumentValue.omitted().ifPresent(value -> called.set(true));

0 commit comments

Comments
 (0)