Kaynağa Gözat

[ML] Use map and filter instead of flatMap in find_file_structure (#42534)

Using map and filter avoids the garbage from all the
Stream.of calls that flatMap necessitated. Performance
is better when there are masses of fields.
David Roberts 6 yıl önce
ebeveyn
işleme
5720a329ad

+ 2 - 5
x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/filestructurefinder/FileStructureUtils.java

@@ -187,11 +187,8 @@ public final class FileStructureUtils {
 
         for (String fieldName : uniqueFieldNames) {
 
-            List<Object> fieldValues = sampleRecords.stream().flatMap(record -> {
-                    Object fieldValue = record.get(fieldName);
-                    return (fieldValue == null) ? Stream.empty() : Stream.of(fieldValue);
-                }
-            ).collect(Collectors.toList());
+            List<Object> fieldValues = sampleRecords.stream().map(record -> record.get(fieldName)).filter(fieldValue -> fieldValue != null)
+                .collect(Collectors.toList());
 
             Tuple<Map<String, String>, FieldStats> mappingAndFieldStats =
                 guessMappingAndCalculateFieldStats(explanation, fieldName, fieldValues, timeoutChecker);