Hoist SmallSortedMap.getNumArrayEntries() outside loops over array entries

This should slightly speed things up.

Should be safe: these operations don't change the number of entries inside the loop.

PiperOrigin-RevId: 650087172
pull/17336/head
Mark Hansen 8 months ago committed by Copybara-Service
parent cbb3abfc4b
commit 229c958a5c
  1. 36
      java/core/src/main/java/com/google/protobuf/FieldSet.java

@ -99,7 +99,8 @@ final class FieldSet<T extends FieldSet.FieldDescriptorLite<T>> {
if (isImmutable) {
return;
}
for (int i = 0; i < fields.getNumArrayEntries(); ++i) {
int n = fields.getNumArrayEntries(); // Optimisation: hoist out of hot loop.
for (int i = 0; i < n; ++i) {
Entry<T, Object> entry = fields.getArrayEntryAt(i);
if (entry.getValue() instanceof GeneratedMessageLite) {
((GeneratedMessageLite<?, ?>) entry.getValue()).makeImmutable();
@ -149,7 +150,8 @@ final class FieldSet<T extends FieldSet.FieldDescriptorLite<T>> {
// We can't just call fields.clone because List objects in the map
// should not be shared.
FieldSet<T> clone = FieldSet.newFieldSet();
for (int i = 0; i < fields.getNumArrayEntries(); i++) {
int n = fields.getNumArrayEntries(); // Optimisation: hoist out of hot loop.
for (int i = 0; i < n; i++) {
Map.Entry<T, Object> entry = fields.getArrayEntryAt(i);
clone.setField(entry.getKey(), entry.getValue());
}
@ -184,7 +186,8 @@ final class FieldSet<T extends FieldSet.FieldDescriptorLite<T>> {
private static <T extends FieldDescriptorLite<T>> SmallSortedMap<T, Object> cloneAllFieldsMap(
SmallSortedMap<T, Object> fields, boolean copyList, boolean resolveLazyFields) {
SmallSortedMap<T, Object> result = SmallSortedMap.newFieldMap();
for (int i = 0; i < fields.getNumArrayEntries(); i++) {
int n = fields.getNumArrayEntries(); // Optimisation: hoist out of hot loop.
for (int i = 0; i < n; i++) {
cloneFieldEntry(result, fields.getArrayEntryAt(i), copyList, resolveLazyFields);
}
for (Map.Entry<T, Object> entry : fields.getOverflowEntries()) {
@ -426,7 +429,8 @@ final class FieldSet<T extends FieldSet.FieldDescriptorLite<T>> {
* caller to check that all required fields are present.
*/
public boolean isInitialized() {
for (int i = 0; i < fields.getNumArrayEntries(); i++) {
int n = fields.getNumArrayEntries(); // Optimisation: hoist out of hot loop.
for (int i = 0; i < n; i++) {
if (!isInitialized(fields.getArrayEntryAt(i))) {
return false;
}
@ -484,7 +488,8 @@ final class FieldSet<T extends FieldSet.FieldDescriptorLite<T>> {
/** Like {@link Message.Builder#mergeFrom(Message)}, but merges from another {@link FieldSet}. */
public void mergeFrom(final FieldSet<T> other) {
for (int i = 0; i < other.fields.getNumArrayEntries(); i++) {
int n = other.fields.getNumArrayEntries(); // Optimisation: hoist out of hot loop.
for (int i = 0; i < n; i++) {
mergeFromField(other.fields.getArrayEntryAt(i));
}
for (final Map.Entry<T, Object> entry : other.fields.getOverflowEntries()) {
@ -571,7 +576,8 @@ final class FieldSet<T extends FieldSet.FieldDescriptorLite<T>> {
/** See {@link Message#writeTo(CodedOutputStream)}. */
public void writeTo(final CodedOutputStream output) throws IOException {
for (int i = 0; i < fields.getNumArrayEntries(); i++) {
int n = fields.getNumArrayEntries(); // Optimisation: hoist out of hot loop.
for (int i = 0; i < n; i++) {
final Map.Entry<T, Object> entry = fields.getArrayEntryAt(i);
writeField(entry.getKey(), entry.getValue(), output);
}
@ -582,7 +588,8 @@ final class FieldSet<T extends FieldSet.FieldDescriptorLite<T>> {
/** Like {@link #writeTo} but uses MessageSet wire format. */
public void writeMessageSetTo(final CodedOutputStream output) throws IOException {
for (int i = 0; i < fields.getNumArrayEntries(); i++) {
int n = fields.getNumArrayEntries(); // Optimisation: hoist out of hot loop.
for (int i = 0; i < n; i++) {
writeMessageSetTo(fields.getArrayEntryAt(i), output);
}
for (final Map.Entry<T, Object> entry : fields.getOverflowEntries()) {
@ -759,7 +766,8 @@ final class FieldSet<T extends FieldSet.FieldDescriptorLite<T>> {
*/
public int getSerializedSize() {
int size = 0;
for (int i = 0; i < fields.getNumArrayEntries(); i++) {
int n = fields.getNumArrayEntries(); // Optimisation: hoist out of hot loop.
for (int i = 0; i < n; i++) {
final Map.Entry<T, Object> entry = fields.getArrayEntryAt(i);
size += computeFieldSize(entry.getKey(), entry.getValue());
}
@ -772,7 +780,8 @@ final class FieldSet<T extends FieldSet.FieldDescriptorLite<T>> {
/** Like {@link #getSerializedSize} but uses MessageSet wire format. */
public int getMessageSetSerializedSize() {
int size = 0;
for (int i = 0; i < fields.getNumArrayEntries(); i++) {
int n = fields.getNumArrayEntries(); // Optimisation: hoist out of hot loop.
for (int i = 0; i < n; i++) {
size += getMessageSetSerializedSize(fields.getArrayEntryAt(i));
}
for (final Map.Entry<T, Object> entry : fields.getOverflowEntries()) {
@ -976,7 +985,8 @@ final class FieldSet<T extends FieldSet.FieldDescriptorLite<T>> {
private static <T extends FieldDescriptorLite<T>> void replaceBuilders(
SmallSortedMap<T, Object> fieldMap, boolean partial) {
for (int i = 0; i < fieldMap.getNumArrayEntries(); i++) {
int n = fieldMap.getNumArrayEntries(); // Optimisation: hoist out of hot loop.
for (int i = 0; i < n; i++) {
replaceBuilders(fieldMap.getArrayEntryAt(i), partial);
}
for (Map.Entry<T, Object> entry : fieldMap.getOverflowEntries()) {
@ -1268,7 +1278,8 @@ final class FieldSet<T extends FieldSet.FieldDescriptorLite<T>> {
* caller to check that all required fields are present.
*/
public boolean isInitialized() {
for (int i = 0; i < fields.getNumArrayEntries(); i++) {
int n = fields.getNumArrayEntries(); // Optimisation: hoist out of hot loop.
for (int i = 0; i < n; i++) {
if (!FieldSet.isInitialized(fields.getArrayEntryAt(i))) {
return false;
}
@ -1286,7 +1297,8 @@ final class FieldSet<T extends FieldSet.FieldDescriptorLite<T>> {
*/
public void mergeFrom(final FieldSet<T> other) {
ensureIsMutable();
for (int i = 0; i < other.fields.getNumArrayEntries(); i++) {
int n = other.fields.getNumArrayEntries(); // Optimisation: hoist out of hot loop.
for (int i = 0; i < n; i++) {
mergeFromField(other.fields.getArrayEntryAt(i));
}
for (final Map.Entry<T, Object> entry : other.fields.getOverflowEntries()) {

Loading…
Cancel
Save