dsmiley commented on code in PR #13327:
URL: https://github.com/apache/lucene/pull/13327#discussion_r1584952051


##########
lucene/core/src/java/org/apache/lucene/index/FieldInfos.java:
##########
@@ -81,30 +81,13 @@ public FieldInfos(FieldInfo[] infos) {
     String softDeletesField = null;
     String parentField = null;
 
-    int size = 0; // number of elements in byNumberTemp, number of used array 
slots
-    FieldInfo[] byNumberTemp = new FieldInfo[10]; // initial array capacity of 
10
+    byName = new HashMap<>((int) (infos.length / 0.75f) + 1);

Review Comment:
   see `org.apache.lucene.util.CollectionUtil#newHashMap` instead



##########
lucene/core/src/java/org/apache/lucene/index/FieldInfos.java:
##########
@@ -821,4 +803,64 @@ FieldInfos finish() {
       return new FieldInfos(byName.values().toArray(new 
FieldInfo[byName.size()]));
     }
   }
+
+  private interface FieldInfoByNumber {

Review Comment:
   or just use `IntFunction` ?



##########
lucene/core/src/java/org/apache/lucene/index/FieldInfos.java:
##########
@@ -156,15 +139,17 @@ public FieldInfos(FieldInfo[] infos) {
     this.softDeletesField = softDeletesField;
     this.parentField = parentField;
 
-    List<FieldInfo> valuesTemp = new ArrayList<>(infos.length);
-    byNumber = new FieldInfo[size];
-    for (int i = 0; i < size; i++) {
-      byNumber[i] = byNumberTemp[i];
-      if (byNumberTemp[i] != null) {
-        valuesTemp.add(byNumberTemp[i]);
-      }
-    }
-    values = Collections.unmodifiableCollection(valuesTemp);
+    FieldInfo[] sortedFieldInfos = ArrayUtil.copyOfSubArray(infos, 0, 
infos.length);
+    Arrays.sort(sortedFieldInfos, (fi1, fi2) -> Integer.compare(fi1.number, 
fi2.number));
+    int maxFieldNumber = infos.length == 0 ? -1 : 
sortedFieldInfos[infos.length - 1].number;
+    // If there are many fields and the max field number is greater than twice 
the number
+    // of fields, then a map structure is more compact to store the by-number 
mapping.
+    byNumber =
+        maxFieldNumber >= 2 * infos.length && maxFieldNumber >= 32
+            ? new MapFieldInfoByNumber(infos)
+            : new ArrayFieldInfoByNumber(infos, maxFieldNumber);
+    // The iteration of FieldInfo is ordered by ascending field number.
+    values = 
Collections.unmodifiableCollection(Arrays.asList(sortedFieldInfos));

Review Comment:
   nowadays, do `List.of(sortedFieldInfos)` rather than this double-layer 
referring to two other classes



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org
For additional commands, e-mail: issues-h...@lucene.apache.org

Reply via email to