[ASTERIXDB-3511][COMP] Avoid creating DataOutputStream twice for closed fields

- user model changes: no
- storage format changes: no
- interface changes: no

Ext-ref: MB-61971
Change-Id: I5f1c6936c6601000de5faad496ee6a765cbb14d9
Reviewed-on: https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/18923
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Ali Alsuliman <ali.al.solaiman@gmail.com>
diff --git a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/pointables/ARecordVisitablePointable.java b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/pointables/ARecordVisitablePointable.java
index e418542..440fd6f 100644
--- a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/pointables/ARecordVisitablePointable.java
+++ b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/pointables/ARecordVisitablePointable.java
@@ -100,6 +100,8 @@
             LinkedHashSet<String> allOrderedFields = inputType.getAllOrderedFields();
             if (allOrderedFields != null) {
                 numFields = allOrderedFields.size();
+                int index = 0;
+                int nameInClosedField = 0;
                 for (String field : allOrderedFields) {
                     int nameStart = typeBos.size();
                     typeDos.writeByte(ATypeTag.SERIALIZED_STRING_TYPE_TAG);
@@ -109,29 +111,24 @@
                     typeNameReference.set(typeBos.getByteArray(), nameStart, nameEnd - nameStart);
                     fieldNames.add(typeNameReference);
                     fieldValues.add(missingReference);
-                }
-            }
-
-            int index = 0;
-            for (int i = 0; i < numberOfSchemaFields; i++) {
-                // add type name Reference (including a string type tag)
-                int nameStart = typeBos.size();
-                typeDos.writeByte(ATypeTag.SERIALIZED_STRING_TYPE_TAG);
-                utf8Writer.writeUTF8(fieldNameStrs[i], typeDos);
-                int nameEnd = typeBos.size();
-                IVisitablePointable typeNameReference = AFlatValuePointable.FACTORY.create(null);
-                typeNameReference.set(typeBos.getByteArray(), nameStart, nameEnd - nameStart);
-                for (; index < numFields; index++) {
-                    if (fieldNames.get(index).equals(typeNameReference)) {
-                        break;
+                    if (nameInClosedField < numberOfSchemaFields && field.equals(fieldNameStrs[nameInClosedField])) {
+                        reverseLookupClosedFields.add(index);
+                        nameInClosedField++;
                     }
+                    index++;
                 }
-                if (index == numFields) {
+            } else {
+                for (int i = 0; i < numberOfSchemaFields; i++) {
+                    // add type name Reference (including a string type tag)
+                    int nameStart = typeBos.size();
+                    typeDos.writeByte(ATypeTag.SERIALIZED_STRING_TYPE_TAG);
+                    utf8Writer.writeUTF8(fieldNameStrs[i], typeDos);
+                    int nameEnd = typeBos.size();
+                    IVisitablePointable typeNameReference = AFlatValuePointable.FACTORY.create(null);
+                    typeNameReference.set(typeBos.getByteArray(), nameStart, nameEnd - nameStart);
                     fieldNames.add(typeNameReference);
                     reverseLookupClosedFields.add(fieldNames.size() - 1);
-                    continue;
                 }
-                reverseLookupClosedFields.add(index);
             }
 
             // initialize a constant: null value bytes reference