In ReactPropertyProcessor, use switch over key String instead of HashMap<String,Integer> 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
This commit is contained in:
Denis Koroskin
2016-01-12 11:51:43 -08:00
committed by facebook-github-bot-0
parent 888749220d
commit 44cd2e0e73

View File

@@ -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<ClassName, ClassInfo> 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<PropertyInfo> 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<PropertyInfo> 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()) {