Eliminating code overhead

* calculating size of the list once in case of loops, avoided creation of string builder object if the length type argument is 0
* replaced null check boilerplate code with nullSafe()
This commit is contained in:
Anirudh Ramanan 2017-03-01 21:43:56 +05:30 committed by Jake Wharton
parent e0f0854ac2
commit 9e44d60b83
5 changed files with 26 additions and 42 deletions

View File

@ -122,7 +122,7 @@ public enum FieldNamingPolicy implements FieldNamingStrategy {
*/ */
static String separateCamelCase(String name, String separator) { static String separateCamelCase(String name, String separator) {
StringBuilder translation = new StringBuilder(); StringBuilder translation = new StringBuilder();
for (int i = 0; i < name.length(); i++) { for (int i = 0, length = name.length(); i < length; i++) {
char character = name.charAt(i); char character = name.charAt(i);
if (Character.isUpperCase(character) && translation.length() != 0) { if (Character.isUpperCase(character) && translation.length() != 0) {
translation.append(separator); translation.append(separator);
@ -139,8 +139,9 @@ public enum FieldNamingPolicy implements FieldNamingStrategy {
StringBuilder fieldNameBuilder = new StringBuilder(); StringBuilder fieldNameBuilder = new StringBuilder();
int index = 0; int index = 0;
char firstCharacter = name.charAt(index); char firstCharacter = name.charAt(index);
int length = name.length();
while (index < name.length() - 1) { while (index < length - 1) {
if (Character.isLetter(firstCharacter)) { if (Character.isLetter(firstCharacter)) {
break; break;
} }
@ -149,7 +150,7 @@ public enum FieldNamingPolicy implements FieldNamingStrategy {
firstCharacter = name.charAt(++index); firstCharacter = name.charAt(++index);
} }
if (index == name.length()) { if (index == length) {
return fieldNameBuilder.toString(); return fieldNameBuilder.toString();
} }

View File

@ -411,7 +411,7 @@ public final class $Gson$Types {
} }
private static int indexOf(Object[] array, Object toFind) { private static int indexOf(Object[] array, Object toFind) {
for (int i = 0; i < array.length; i++) { for (int i = 0, length = array.length; i < length; i++) {
if (toFind.equals(array[i])) { if (toFind.equals(array[i])) {
return i; return i;
} }
@ -451,7 +451,7 @@ public final class $Gson$Types {
this.ownerType = ownerType == null ? null : canonicalize(ownerType); this.ownerType = ownerType == null ? null : canonicalize(ownerType);
this.rawType = canonicalize(rawType); this.rawType = canonicalize(rawType);
this.typeArguments = typeArguments.clone(); this.typeArguments = typeArguments.clone();
for (int t = 0; t < this.typeArguments.length; t++) { for (int t = 0, length = this.typeArguments.length; t < length; t++) {
checkNotNull(this.typeArguments[t]); checkNotNull(this.typeArguments[t]);
checkNotPrimitive(this.typeArguments[t]); checkNotPrimitive(this.typeArguments[t]);
this.typeArguments[t] = canonicalize(this.typeArguments[t]); this.typeArguments[t] = canonicalize(this.typeArguments[t]);
@ -482,15 +482,14 @@ public final class $Gson$Types {
} }
@Override public String toString() { @Override public String toString() {
StringBuilder stringBuilder = new StringBuilder(30 * (typeArguments.length + 1)); int length = typeArguments.length;
stringBuilder.append(typeToString(rawType)); if (length == 0) {
return typeToString(rawType);
if (typeArguments.length == 0) {
return stringBuilder.toString();
} }
stringBuilder.append("<").append(typeToString(typeArguments[0])); StringBuilder stringBuilder = new StringBuilder(30 * (length + 1));
for (int i = 1; i < typeArguments.length; i++) { stringBuilder.append(typeToString(rawType)).append("<").append(typeToString(typeArguments[0]));
for (int i = 1; i < length; i++) {
stringBuilder.append(", ").append(typeToString(typeArguments[i])); stringBuilder.append(", ").append(typeToString(typeArguments[i]));
} }
return stringBuilder.append(">").toString(); return stringBuilder.append(">").toString();

View File

@ -73,8 +73,10 @@ public final class ArrayTypeAdapter<E> extends TypeAdapter<Object> {
list.add(instance); list.add(instance);
} }
in.endArray(); in.endArray();
Object array = Array.newInstance(componentType, list.size());
for (int i = 0; i < list.size(); i++) { int size = list.size();
Object array = Array.newInstance(componentType, size);
for (int i = 0; i < size; i++) {
Array.set(array, i, list.get(i)); Array.set(array, i, list.get(i));
} }
return array; return array;

View File

@ -158,7 +158,7 @@ public final class ReflectiveTypeAdapterFactory implements TypeAdapterFactory {
Type fieldType = $Gson$Types.resolve(type.getType(), raw, field.getGenericType()); Type fieldType = $Gson$Types.resolve(type.getType(), raw, field.getGenericType());
List<String> fieldNames = getFieldNames(field); List<String> fieldNames = getFieldNames(field);
BoundField previous = null; BoundField previous = null;
for (int i = 0; i < fieldNames.size(); ++i) { for (int i = 0, size = fieldNames.size(); i < size; ++i) {
String name = fieldNames.get(i); String name = fieldNames.get(i);
if (i != 0) serialize = false; // only serialize the default name if (i != 0) serialize = false; // only serialize the default name
BoundField boundField = createBoundField(context, field, name, BoundField boundField = createBoundField(context, field, name,

View File

@ -69,33 +69,20 @@ public final class TypeAdapters {
public static final TypeAdapter<Class> CLASS = new TypeAdapter<Class>() { public static final TypeAdapter<Class> CLASS = new TypeAdapter<Class>() {
@Override @Override
public void write(JsonWriter out, Class value) throws IOException { public void write(JsonWriter out, Class value) throws IOException {
if (value == null) {
out.nullValue();
} else {
throw new UnsupportedOperationException("Attempted to serialize java.lang.Class: " throw new UnsupportedOperationException("Attempted to serialize java.lang.Class: "
+ value.getName() + ". Forgot to register a type adapter?"); + value.getName() + ". Forgot to register a type adapter?");
} }
}
@Override @Override
public Class read(JsonReader in) throws IOException { public Class read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();
return null;
} else {
throw new UnsupportedOperationException( throw new UnsupportedOperationException(
"Attempted to deserialize a java.lang.Class. Forgot to register a type adapter?"); "Attempted to deserialize a java.lang.Class. Forgot to register a type adapter?");
} }
} }.nullSafe();
};
public static final TypeAdapterFactory CLASS_FACTORY = newFactory(Class.class, CLASS); public static final TypeAdapterFactory CLASS_FACTORY = newFactory(Class.class, CLASS);
public static final TypeAdapter<BitSet> BIT_SET = new TypeAdapter<BitSet>() { public static final TypeAdapter<BitSet> BIT_SET = new TypeAdapter<BitSet>() {
@Override public BitSet read(JsonReader in) throws IOException { @Override public BitSet read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();
return null;
}
BitSet bitset = new BitSet(); BitSet bitset = new BitSet();
in.beginArray(); in.beginArray();
int i = 0; int i = 0;
@ -132,19 +119,14 @@ public final class TypeAdapters {
} }
@Override public void write(JsonWriter out, BitSet src) throws IOException { @Override public void write(JsonWriter out, BitSet src) throws IOException {
if (src == null) {
out.nullValue();
return;
}
out.beginArray(); out.beginArray();
for (int i = 0; i < src.length(); i++) { for (int i = 0, length = src.length(); i < length; i++) {
int value = (src.get(i)) ? 1 : 0; int value = (src.get(i)) ? 1 : 0;
out.value(value); out.value(value);
} }
out.endArray(); out.endArray();
} }
}; }.nullSafe();
public static final TypeAdapterFactory BIT_SET_FACTORY = newFactory(BitSet.class, BIT_SET); public static final TypeAdapterFactory BIT_SET_FACTORY = newFactory(BitSet.class, BIT_SET);