From 44cd2e0e73593eb7b97c4854c14f854a20a5d8d0 Mon Sep 17 00:00:00 2001 From: Denis Koroskin Date: Tue, 12 Jan 2016 11:51:43 -0800 Subject: [PATCH] In ReactPropertyProcessor, use switch over key String instead of HashMap lookup followed by a switch over int. Summary: public Switch over a String is essentially a switch(string.hashCode()) and it is faster than a HashMap lookup. It also doesn't use extra memory, and doesn't require static initialization (startup penalty). Plus I believe this way the generated code looks more readable. Reviewed By: lexs Differential Revision: D2818223 fb-gh-sync-id: d9100ec0f8ad556c347681cbe1433d066076d3e5 --- .../processing/ReactPropertyProcessor.java | 33 ++----------------- 1 file changed, 3 insertions(+), 30 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/processing/ReactPropertyProcessor.java b/ReactAndroid/src/main/java/com/facebook/react/processing/ReactPropertyProcessor.java index bee08f387..19641f633 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/processing/ReactPropertyProcessor.java +++ b/ReactAndroid/src/main/java/com/facebook/react/processing/ReactPropertyProcessor.java @@ -89,11 +89,6 @@ public class ReactPropertyProcessor extends AbstractProcessor { private static final TypeName CONCRETE_PROPERTY_MAP_TYPE = ParameterizedTypeName.get(HashMap.class, String.class, String.class); - private static final TypeName MAPPINGS_MAP_TYPE = - ParameterizedTypeName.get(Map.class, String.class, Integer.class); - private static final TypeName CONCRETE_MAPPINGS_MAP_TYPE = - ParameterizedTypeName.get(HashMap.class, String.class, Integer.class); - private final Map mClasses; @SuppressFieldNotInitialized @@ -242,8 +237,6 @@ public class ReactPropertyProcessor extends AbstractProcessor { TypeSpec holderClass = TypeSpec.classBuilder(holderClassName) .addSuperinterface(superType) .addModifiers(PUBLIC) - .addField(MAPPINGS_MAP_TYPE, "mappings", PRIVATE, STATIC, FINAL) - .addStaticBlock(generatePropertyMappings(properties)) .addMethod(generateSetPropertySpec(classInfo, properties)) .addMethod(getMethods) .build(); @@ -274,24 +267,6 @@ public class ReactPropertyProcessor extends AbstractProcessor { } } - private static CodeBlock generatePropertyMappings(List properties) { - if (properties.isEmpty()) { - return CodeBlock.builder() - .addStatement("mappings = $T.emptyMap()", Collections.class) - .build(); - } - - CodeBlock.Builder builder = CodeBlock.builder() - .addStatement("mappings = new $T($L)", CONCRETE_MAPPINGS_MAP_TYPE, properties.size()); - - for (int i = 0, size = properties.size(); i < size; i++) { - PropertyInfo propertyInfo = properties.get(i); - builder.addStatement("mappings.put($S, $L)", propertyInfo.mProperty.name(), i); - } - - return builder.build(); - } - private static MethodSpec generateSetPropertySpec( ClassInfo classInfo, List properties) { @@ -324,15 +299,13 @@ public class ReactPropertyProcessor extends AbstractProcessor { return CodeBlock.builder().build(); } - CodeBlock.Builder builder = CodeBlock.builder() - .addStatement("Integer id = mappings.get(name)") - .addStatement("if (id == null) return"); + CodeBlock.Builder builder = CodeBlock.builder(); - builder.add("switch (id) {\n").indent(); + builder.add("switch (name) {\n").indent(); for (int i = 0, size = properties.size(); i < size; i++) { PropertyInfo propertyInfo = properties.get(i); builder - .add("case $L:\n", i) + .add("case \"$L\":\n", propertyInfo.mProperty.name()) .indent(); switch (info.getType()) {