Skip to content

Commit

Permalink
Avoid unnecessary creation of new empty arrays
Browse files Browse the repository at this point in the history
Summary: $title

Reviewed By: zielinskimz

Differential Revision: D63393025

fbshipit-source-id: 4228c448e6300ff3c4b178e8c77a3f048da4e8bd
  • Loading branch information
kingsleyadio authored and facebook-github-bot committed Sep 26, 2024
1 parent d005f45 commit 3304452
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
2 changes: 2 additions & 0 deletions litho-core/src/main/java/com/facebook/litho/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ public abstract class Component implements Cloneable, Equivalence<Component>, At
private static final String NULL_KEY_SET = "Component:NullKeySet";
private static final AtomicInteger sIdGenerator = new AtomicInteger(1);

protected static final Object[] EMPTY_ARRAY = new Object[0];

/**
* @return the globally unique ID associated with {@param type}, creating one if necessary.
* Allocated IDs map 1-to-1 with objects passed to this method.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class ComponentBodyGeneratorTest {
"""
@java.lang.Override
protected final java.lang.Object[] getProps() {
return new java.lang.Object[] {};
return com.facebook.litho.Component.EMPTY_ARRAY;
}
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -579,13 +579,18 @@ static MethodSpec generateGetPropsMethod(SpecModel specModel) {
sb.append(prop.getName());
}
}
return MethodSpec.methodBuilder("getProps")
.addAnnotation(Override.class)
.addModifiers(Modifier.PROTECTED)
.addModifiers(Modifier.FINAL)
.returns(ArrayTypeName.of(Object.class))
.addStatement("return new $T[] {$L}", Object.class, sb.toString())
.build();
MethodSpec.Builder builder =
MethodSpec.methodBuilder("getProps")
.addAnnotation(Override.class)
.addModifiers(Modifier.PROTECTED)
.addModifiers(Modifier.FINAL)
.returns(ArrayTypeName.of(Object.class));
if (sb.length() == 0) {
builder.addStatement("return $T.EMPTY_ARRAY", ClassNames.COMPONENT);
} else {
builder.addStatement("return new $T[] {$L}", Object.class, sb.toString());
}
return builder.build();
}

static MethodSpec generateIsEquivalentPropsMethod(SpecModel specModel, EnumSet<RunMode> runMode) {
Expand Down

0 comments on commit 3304452

Please sign in to comment.