optimize getNativeProps method

Summary:
The original method getNativeProps in ViewManagerPropertyUpdater.java create more HashMaps and putAll method need to re-hash the key again to avoid conflicts. This pull request pass the map as params to avoid the problem and update ReactPropertyProcessor.java to adapt the change.
Closes https://github.com/facebook/react-native/pull/9916

Differential Revision: D3873152

fbshipit-source-id: 089840e5272265662cdbf58d88580f9203153b69
This commit is contained in:
ransj 2016-09-15 15:31:40 -07:00 committed by Facebook Github Bot 3
parent 3b5e4cc593
commit 44b0e6b91f
2 changed files with 11 additions and 28 deletions

View File

@ -245,7 +245,8 @@ public class ReactPropertyProcessor extends AbstractProcessor {
MethodSpec getMethods = MethodSpec.methodBuilder("getProperties") MethodSpec getMethods = MethodSpec.methodBuilder("getProperties")
.addModifiers(PUBLIC) .addModifiers(PUBLIC)
.addAnnotation(Override.class) .addAnnotation(Override.class)
.returns(PROPERTY_MAP_TYPE) .addParameter(PROPERTY_MAP_TYPE, "props")
.returns(TypeName.VOID)
.addCode(generateGetProperties(properties)) .addCode(generateGetProperties(properties))
.build(); .build();
@ -397,19 +398,7 @@ public class ReactPropertyProcessor extends AbstractProcessor {
private static CodeBlock generateGetProperties(List<PropertyInfo> properties) private static CodeBlock generateGetProperties(List<PropertyInfo> properties)
throws ReactPropertyException { throws ReactPropertyException {
if (properties.isEmpty()) { CodeBlock.Builder builder = CodeBlock.builder();
return CodeBlock.builder()
.addStatement("return $T.emptyMap()", Collections.class)
.build();
}
CodeBlock.Builder builder = CodeBlock.builder()
.addStatement(
"$T props = new $T($L)",
PROPERTY_MAP_TYPE,
CONCRETE_PROPERTY_MAP_TYPE,
properties.size());
for (PropertyInfo propertyInfo : properties) { for (PropertyInfo propertyInfo : properties) {
try { try {
String typeName = getPropertypTypeName(propertyInfo.mProperty, propertyInfo.propertyType); String typeName = getPropertypTypeName(propertyInfo.mProperty, propertyInfo.propertyType);
@ -419,9 +408,7 @@ public class ReactPropertyProcessor extends AbstractProcessor {
} }
} }
return builder return builder.build();
.addStatement("return props")
.build();
} }
private static String getPropertypTypeName(Property property, TypeName propertyType) { private static String getPropertypTypeName(Property property, TypeName propertyType) {

View File

@ -13,7 +13,7 @@ import com.facebook.react.bridge.ReadableMapKeySetIterator;
public class ViewManagerPropertyUpdater { public class ViewManagerPropertyUpdater {
public interface Settable { public interface Settable {
Map<String, String> getProperties(); void getProperties(Map<String, String> props);
} }
public interface ViewManagerSetter<T extends ViewManager, V extends View> extends Settable { public interface ViewManagerSetter<T extends ViewManager, V extends View> extends Settable {
@ -57,8 +57,8 @@ public class ViewManagerPropertyUpdater {
Class<? extends ViewManager> viewManagerTopClass, Class<? extends ViewManager> viewManagerTopClass,
Class<? extends ReactShadowNode> shadowNodeTopClass) { Class<? extends ReactShadowNode> shadowNodeTopClass) {
Map<String, String> props = new HashMap<>(); Map<String, String> props = new HashMap<>();
props.putAll(findManagerSetter(viewManagerTopClass).getProperties()); findManagerSetter(viewManagerTopClass).getProperties(props);
props.putAll(findNodeSetter(shadowNodeTopClass).getProperties()); findNodeSetter(shadowNodeTopClass).getProperties(props);
return props; return props;
} }
@ -125,12 +125,10 @@ public class ViewManagerPropertyUpdater {
} }
@Override @Override
public Map<String, String> getProperties() { public void getProperties(Map<String, String> props) {
Map<String, String> nativeProps = new HashMap<>();
for (ViewManagersPropertyCache.PropSetter setter : mPropSetters.values()) { for (ViewManagersPropertyCache.PropSetter setter : mPropSetters.values()) {
nativeProps.put(setter.getPropName(), setter.getPropType()); props.put(setter.getPropName(), setter.getPropType());
} }
return nativeProps;
} }
} }
@ -152,12 +150,10 @@ public class ViewManagerPropertyUpdater {
} }
@Override @Override
public Map<String, String> getProperties() { public void getProperties(Map<String, String> props) {
Map<String, String> nativeProps = new HashMap<>();
for (ViewManagersPropertyCache.PropSetter setter : mPropSetters.values()) { for (ViewManagersPropertyCache.PropSetter setter : mPropSetters.values()) {
nativeProps.put(setter.getPropName(), setter.getPropType()); props.put(setter.getPropName(), setter.getPropType());
} }
return nativeProps;
} }
} }
} }