Commit a69b461e authored by Brian Duff's avatar Brian Duff

Inline unknownFieldData{Equals,HashCode} to generated code.

It turns out dex (apparently) was inlining these protected final
methods from ExtendableMessageNano into every message class. Removing
these methods from the base class and inlining their code reduces
the method count by 2 methods / message when the store_unknown_fields
option is on.

Change-Id: I0aa09f2016d39939c4c8b8219601793b8fab301f
parent dac7e02d
...@@ -160,31 +160,6 @@ public abstract class ExtendableMessageNano<M extends ExtendableMessageNano<M>> ...@@ -160,31 +160,6 @@ public abstract class ExtendableMessageNano<M extends ExtendableMessageNano<M>>
return true; return true;
} }
/**
* Returns whether the stored unknown field data in this message is equivalent to that in the
* other message.
*
* @param other the other message.
* @return whether the two sets of unknown field data are equal.
*/
protected final boolean unknownFieldDataEquals(M other) {
if (unknownFieldData == null || unknownFieldData.isEmpty()) {
return other.unknownFieldData == null || other.unknownFieldData.isEmpty();
} else {
return unknownFieldData.equals(other.unknownFieldData);
}
}
/**
* Computes the hashcode representing the unknown field data stored in this message.
*
* @return the hashcode for the unknown field data.
*/
protected final int unknownFieldDataHashCode() {
return (unknownFieldData == null || unknownFieldData.isEmpty()
? 0 : unknownFieldData.hashCode());
}
@Override @Override
public M clone() throws CloneNotSupportedException { public M clone() throws CloneNotSupportedException {
M cloned = (M) super.clone(); M cloned = (M) super.clone();
......
...@@ -35,9 +35,12 @@ package com.google.protobuf.nano; ...@@ -35,9 +35,12 @@ package com.google.protobuf.nano;
* A custom version of {@link android.util.SparseArray} with the minimal API * A custom version of {@link android.util.SparseArray} with the minimal API
* for storing {@link FieldData} objects. * for storing {@link FieldData} objects.
* *
* <p>This class is an internal implementation detail of nano and should not
* be called directly by clients.
*
* Based on {@link android.support.v4.util.SpareArrayCompat}. * Based on {@link android.support.v4.util.SpareArrayCompat}.
*/ */
class FieldArray implements Cloneable { public final class FieldArray implements Cloneable {
private static final FieldData DELETED = new FieldData(); private static final FieldData DELETED = new FieldData();
private boolean mGarbage = false; private boolean mGarbage = false;
...@@ -48,7 +51,7 @@ class FieldArray implements Cloneable { ...@@ -48,7 +51,7 @@ class FieldArray implements Cloneable {
/** /**
* Creates a new FieldArray containing no fields. * Creates a new FieldArray containing no fields.
*/ */
public FieldArray() { FieldArray() {
this(10); this(10);
} }
...@@ -57,7 +60,7 @@ class FieldArray implements Cloneable { ...@@ -57,7 +60,7 @@ class FieldArray implements Cloneable {
* require any additional memory allocation to store the specified * require any additional memory allocation to store the specified
* number of mappings. * number of mappings.
*/ */
public FieldArray(int initialCapacity) { FieldArray(int initialCapacity) {
initialCapacity = idealIntArraySize(initialCapacity); initialCapacity = idealIntArraySize(initialCapacity);
mFieldNumbers = new int[initialCapacity]; mFieldNumbers = new int[initialCapacity];
mData = new FieldData[initialCapacity]; mData = new FieldData[initialCapacity];
...@@ -68,7 +71,7 @@ class FieldArray implements Cloneable { ...@@ -68,7 +71,7 @@ class FieldArray implements Cloneable {
* Gets the FieldData mapped from the specified fieldNumber, or <code>null</code> * Gets the FieldData mapped from the specified fieldNumber, or <code>null</code>
* if no such mapping has been made. * if no such mapping has been made.
*/ */
public FieldData get(int fieldNumber) { FieldData get(int fieldNumber) {
int i = binarySearch(fieldNumber); int i = binarySearch(fieldNumber);
if (i < 0 || mData[i] == DELETED) { if (i < 0 || mData[i] == DELETED) {
...@@ -81,7 +84,7 @@ class FieldArray implements Cloneable { ...@@ -81,7 +84,7 @@ class FieldArray implements Cloneable {
/** /**
* Removes the data from the specified fieldNumber, if there was any. * Removes the data from the specified fieldNumber, if there was any.
*/ */
public void remove(int fieldNumber) { void remove(int fieldNumber) {
int i = binarySearch(fieldNumber); int i = binarySearch(fieldNumber);
if (i >= 0 && mData[i] != DELETED) { if (i >= 0 && mData[i] != DELETED) {
...@@ -118,7 +121,7 @@ class FieldArray implements Cloneable { ...@@ -118,7 +121,7 @@ class FieldArray implements Cloneable {
* Adds a mapping from the specified fieldNumber to the specified data, * Adds a mapping from the specified fieldNumber to the specified data,
* replacing the previous mapping if there was one. * replacing the previous mapping if there was one.
*/ */
public void put(int fieldNumber, FieldData data) { void put(int fieldNumber, FieldData data) {
int i = binarySearch(fieldNumber); int i = binarySearch(fieldNumber);
if (i >= 0) { if (i >= 0) {
...@@ -167,7 +170,7 @@ class FieldArray implements Cloneable { ...@@ -167,7 +170,7 @@ class FieldArray implements Cloneable {
* Returns the number of key-value mappings that this FieldArray * Returns the number of key-value mappings that this FieldArray
* currently stores. * currently stores.
*/ */
public int size() { int size() {
if (mGarbage) { if (mGarbage) {
gc(); gc();
} }
...@@ -184,7 +187,7 @@ class FieldArray implements Cloneable { ...@@ -184,7 +187,7 @@ class FieldArray implements Cloneable {
* the value from the <code>index</code>th key-value mapping that this * the value from the <code>index</code>th key-value mapping that this
* FieldArray stores. * FieldArray stores.
*/ */
public FieldData dataAt(int index) { FieldData dataAt(int index) {
if (mGarbage) { if (mGarbage) {
gc(); gc();
} }
......
...@@ -612,7 +612,11 @@ void MessageGenerator::GenerateEquals(io::Printer* printer) { ...@@ -612,7 +612,11 @@ void MessageGenerator::GenerateEquals(io::Printer* printer) {
if (params_.store_unknown_fields()) { if (params_.store_unknown_fields()) {
printer->Print( printer->Print(
"return unknownFieldDataEquals(other);\n"); "if (unknownFieldData == null || unknownFieldData.isEmpty()) {\n"
" return other.unknownFieldData == null || other.unknownFieldData.isEmpty();\n"
"} else {\n"
" return unknownFieldData.equals(other.unknownFieldData);\n"
"}");
} else { } else {
printer->Print( printer->Print(
"return true;\n"); "return true;\n");
...@@ -642,7 +646,9 @@ void MessageGenerator::GenerateHashCode(io::Printer* printer) { ...@@ -642,7 +646,9 @@ void MessageGenerator::GenerateHashCode(io::Printer* printer) {
if (params_.store_unknown_fields()) { if (params_.store_unknown_fields()) {
printer->Print( printer->Print(
"result = 31 * result + unknownFieldDataHashCode();\n"); "result = 31 * result + \n"
" (unknownFieldData == null || unknownFieldData.isEmpty() ? 0 : \n"
" unknownFieldData.hashCode());\n");
} }
printer->Print("return result;\n"); printer->Print("return result;\n");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment