nzw921rx commented on code in PR #12525:
URL: https://github.com/apache/seatunnel/pull/12525#discussion_r4695075990


##########
seatunnel-api/src/main/java/org/apache/seatunnel/api/table/catalog/AbstractSchema.java:
##########
@@ -69,18 +86,41 @@ public String[] getFieldNames() {
     }
 
     public int indexOf(String columnName) {
-        return columnNames.indexOf(columnName);
+        Integer index = getColumnIndexCache().get(columnName);
+        return index == null ? -1 : index;
     }
 
     public Column getColumn(String columnName) {
         return columns.get(indexOf(columnName));
     }
 
     public boolean contains(String columnName) {
-        return columnNames.contains(columnName);
+        return indexOf(columnName) != -1;
     }
 
     public List<Column> getColumns() {
         return Collections.unmodifiableList(columns);
     }
+
+    /**
+     * Builds the name to index cache only after the whole map is ready. 
Duplicate names keep the
+     * first column so lookups match the former linear scan semantics.
+     */
+    private Map<String, Integer> getColumnIndexCache() {
+        Map<String, Integer> cache = columnIndexCache;
+        if (cache == null) {
+            synchronized (this) {
+                cache = columnIndexCache;
+                if (cache == null) {
+                    Map<String, Integer> indexes = new 
HashMap<>(columnNames.size());
+                    for (int i = 0; i < columnNames.size(); i++) {
+                        indexes.putIfAbsent(columnNames.get(i), i);
+                    }
+                    cache = indexes;
+                    columnIndexCache = cache;
+                }
+            }
+        }
+        return cache;
+    }

Review Comment:
   The approach here has minimal performance loss, but it strictly ensures that 
`cache` is initialized only once. I support adding this lock as it is a safe 
practice!



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to