@@ -99,7 +99,8 @@ public void makeImmutable() {
99
99
if (isImmutable ) {
100
100
return ;
101
101
}
102
- for (int i = 0 ; i < fields .getNumArrayEntries (); ++i ) {
102
+ int n = fields .getNumArrayEntries (); // Optimisation: hoist out of hot loop.
103
+ for (int i = 0 ; i < n ; ++i ) {
103
104
Entry <T , Object > entry = fields .getArrayEntryAt (i );
104
105
if (entry .getValue () instanceof GeneratedMessageLite ) {
105
106
((GeneratedMessageLite <?, ?>) entry .getValue ()).makeImmutable ();
@@ -149,7 +150,8 @@ public FieldSet<T> clone() {
149
150
// We can't just call fields.clone because List objects in the map
150
151
// should not be shared.
151
152
FieldSet <T > clone = FieldSet .newFieldSet ();
152
- for (int i = 0 ; i < fields .getNumArrayEntries (); i ++) {
153
+ int n = fields .getNumArrayEntries (); // Optimisation: hoist out of hot loop.
154
+ for (int i = 0 ; i < n ; i ++) {
153
155
Map .Entry <T , Object > entry = fields .getArrayEntryAt (i );
154
156
clone .setField (entry .getKey (), entry .getValue ());
155
157
}
@@ -184,7 +186,8 @@ public Map<T, Object> getAllFields() {
184
186
private static <T extends FieldDescriptorLite <T >> SmallSortedMap <T , Object > cloneAllFieldsMap (
185
187
SmallSortedMap <T , Object > fields , boolean copyList , boolean resolveLazyFields ) {
186
188
SmallSortedMap <T , Object > result = SmallSortedMap .newFieldMap ();
187
- for (int i = 0 ; i < fields .getNumArrayEntries (); i ++) {
189
+ int n = fields .getNumArrayEntries (); // Optimisation: hoist out of hot loop.
190
+ for (int i = 0 ; i < n ; i ++) {
188
191
cloneFieldEntry (result , fields .getArrayEntryAt (i ), copyList , resolveLazyFields );
189
192
}
190
193
for (Map .Entry <T , Object > entry : fields .getOverflowEntries ()) {
@@ -426,7 +429,8 @@ private static boolean isValidType(final WireFormat.FieldType type, final Object
426
429
* caller to check that all required fields are present.
427
430
*/
428
431
public boolean isInitialized () {
429
- for (int i = 0 ; i < fields .getNumArrayEntries (); i ++) {
432
+ int n = fields .getNumArrayEntries (); // Optimisation: hoist out of hot loop.
433
+ for (int i = 0 ; i < n ; i ++) {
430
434
if (!isInitialized (fields .getArrayEntryAt (i ))) {
431
435
return false ;
432
436
}
@@ -484,7 +488,8 @@ static int getWireFormatForFieldType(final WireFormat.FieldType type, boolean is
484
488
485
489
/** Like {@link Message.Builder#mergeFrom(Message)}, but merges from another {@link FieldSet}. */
486
490
public void mergeFrom (final FieldSet <T > other ) {
487
- for (int i = 0 ; i < other .fields .getNumArrayEntries (); i ++) {
491
+ int n = other .fields .getNumArrayEntries (); // Optimisation: hoist out of hot loop.
492
+ for (int i = 0 ; i < n ; i ++) {
488
493
mergeFromField (other .fields .getArrayEntryAt (i ));
489
494
}
490
495
for (final Map .Entry <T , Object > entry : other .fields .getOverflowEntries ()) {
@@ -571,7 +576,8 @@ public static Object readPrimitiveField(
571
576
572
577
/** See {@link Message#writeTo(CodedOutputStream)}. */
573
578
public void writeTo (final CodedOutputStream output ) throws IOException {
574
- for (int i = 0 ; i < fields .getNumArrayEntries (); i ++) {
579
+ int n = fields .getNumArrayEntries (); // Optimisation: hoist out of hot loop.
580
+ for (int i = 0 ; i < n ; i ++) {
575
581
final Map .Entry <T , Object > entry = fields .getArrayEntryAt (i );
576
582
writeField (entry .getKey (), entry .getValue (), output );
577
583
}
@@ -582,7 +588,8 @@ public void writeTo(final CodedOutputStream output) throws IOException {
582
588
583
589
/** Like {@link #writeTo} but uses MessageSet wire format. */
584
590
public void writeMessageSetTo (final CodedOutputStream output ) throws IOException {
585
- for (int i = 0 ; i < fields .getNumArrayEntries (); i ++) {
591
+ int n = fields .getNumArrayEntries (); // Optimisation: hoist out of hot loop.
592
+ for (int i = 0 ; i < n ; i ++) {
586
593
writeMessageSetTo (fields .getArrayEntryAt (i ), output );
587
594
}
588
595
for (final Map .Entry <T , Object > entry : fields .getOverflowEntries ()) {
@@ -759,7 +766,8 @@ public static void writeField(
759
766
*/
760
767
public int getSerializedSize () {
761
768
int size = 0 ;
762
- for (int i = 0 ; i < fields .getNumArrayEntries (); i ++) {
769
+ int n = fields .getNumArrayEntries (); // Optimisation: hoist out of hot loop.
770
+ for (int i = 0 ; i < n ; i ++) {
763
771
final Map .Entry <T , Object > entry = fields .getArrayEntryAt (i );
764
772
size += computeFieldSize (entry .getKey (), entry .getValue ());
765
773
}
@@ -772,7 +780,8 @@ public int getSerializedSize() {
772
780
/** Like {@link #getSerializedSize} but uses MessageSet wire format. */
773
781
public int getMessageSetSerializedSize () {
774
782
int size = 0 ;
775
- for (int i = 0 ; i < fields .getNumArrayEntries (); i ++) {
783
+ int n = fields .getNumArrayEntries (); // Optimisation: hoist out of hot loop.
784
+ for (int i = 0 ; i < n ; i ++) {
776
785
size += getMessageSetSerializedSize (fields .getArrayEntryAt (i ));
777
786
}
778
787
for (final Map .Entry <T , Object > entry : fields .getOverflowEntries ()) {
@@ -976,7 +985,8 @@ private FieldSet<T> buildImpl(boolean partial) {
976
985
977
986
private static <T extends FieldDescriptorLite <T >> void replaceBuilders (
978
987
SmallSortedMap <T , Object > fieldMap , boolean partial ) {
979
- for (int i = 0 ; i < fieldMap .getNumArrayEntries (); i ++) {
988
+ int n = fieldMap .getNumArrayEntries (); // Optimisation: hoist out of hot loop.
989
+ for (int i = 0 ; i < n ; i ++) {
980
990
replaceBuilders (fieldMap .getArrayEntryAt (i ), partial );
981
991
}
982
992
for (Map .Entry <T , Object > entry : fieldMap .getOverflowEntries ()) {
@@ -1268,7 +1278,8 @@ private void verifyType(final T descriptor, final Object value) {
1268
1278
* caller to check that all required fields are present.
1269
1279
*/
1270
1280
public boolean isInitialized () {
1271
- for (int i = 0 ; i < fields .getNumArrayEntries (); i ++) {
1281
+ int n = fields .getNumArrayEntries (); // Optimisation: hoist out of hot loop.
1282
+ for (int i = 0 ; i < n ; i ++) {
1272
1283
if (!FieldSet .isInitialized (fields .getArrayEntryAt (i ))) {
1273
1284
return false ;
1274
1285
}
@@ -1286,7 +1297,8 @@ public boolean isInitialized() {
1286
1297
*/
1287
1298
public void mergeFrom (final FieldSet <T > other ) {
1288
1299
ensureIsMutable ();
1289
- for (int i = 0 ; i < other .fields .getNumArrayEntries (); i ++) {
1300
+ int n = other .fields .getNumArrayEntries (); // Optimisation: hoist out of hot loop.
1301
+ for (int i = 0 ; i < n ; i ++) {
1290
1302
mergeFromField (other .fields .getArrayEntryAt (i ));
1291
1303
}
1292
1304
for (final Map .Entry <T , Object > entry : other .fields .getOverflowEntries ()) {
0 commit comments