This is an automated email from the ASF dual-hosted git repository.

psalagnac pushed a commit to branch branch_10x
in repository https://gitbox.apache.org/repos/asf/solr.git


The following commit(s) were added to refs/heads/branch_10x by this push:
     new 82cebbf93c8 SOLR-18306: Optimize IndexSchema.getDynamicFieldType() by 
checking cached fields before matching dynamic patterns. (#4630)
82cebbf93c8 is described below

commit 82cebbf93c8a2410ee7fb41ae1bbace9cc1b004e
Author: Pierre Salagnac <[email protected]>
AuthorDate: Mon Jul 13 12:24:05 2026 +0200

    SOLR-18306: Optimize IndexSchema.getDynamicFieldType() by checking cached 
fields before matching dynamic patterns. (#4630)
    
    This fixes a CPU hotspot in IndexSchema.getDynamicFieldType() by using the 
cache of dynamic fields before matching all the patterns of dynamic field names.
    
    (cherry picked from commit 514e15b46673e68e61c2505ce1dd7aad686835c1)
---
 changelog/unreleased/SOLR-18306-optimize-schema.yml |  8 ++++++++
 .../java/org/apache/solr/schema/IndexSchema.java    | 21 +++++++++++++++++++--
 2 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/changelog/unreleased/SOLR-18306-optimize-schema.yml 
b/changelog/unreleased/SOLR-18306-optimize-schema.yml
new file mode 100644
index 00000000000..b410be27676
--- /dev/null
+++ b/changelog/unreleased/SOLR-18306-optimize-schema.yml
@@ -0,0 +1,8 @@
+title: Optimize IndexSchema.getDynamicFieldType() by checking cached fields 
before matching dynamic patterns.
+type: changed
+authors:
+  - name: Pierre Salagnac
+links:
+  - name: SOLR-18306
+    url: https://issues.apache.org/jira/browse/SOLR-18306
+
diff --git a/solr/core/src/java/org/apache/solr/schema/IndexSchema.java 
b/solr/core/src/java/org/apache/solr/schema/IndexSchema.java
index 9710876c045..05ee9e22869 100644
--- a/solr/core/src/java/org/apache/solr/schema/IndexSchema.java
+++ b/solr/core/src/java/org/apache/solr/schema/IndexSchema.java
@@ -1368,6 +1368,12 @@ public class IndexSchema {
       return false;
     }
 
+    // If a field with same name exists in the cache, don't match
+    // all dynamic field patterns.
+    if (dynamicFieldCache.getIfPresent(fieldName) != null) {
+      return true;
+    }
+
     for (DynamicField df : dynamicFields) {
       if (df.matches(fieldName)) return true;
     }
@@ -1477,13 +1483,24 @@ public class IndexSchema {
    * @see #getFieldTypeNoEx
    */
   public FieldType getDynamicFieldType(String fieldName) {
-    for (DynamicField df : dynamicFields) {
-      if (df.matches(fieldName)) return df.prototype.getType();
+    FieldType type = dynFieldType(fieldName);
+    if (type != null) {
+      return type;
     }
+
     throw new SolrException(ErrorCode.BAD_REQUEST, "undefined field " + 
fieldName);
   }
 
   private FieldType dynFieldType(String fieldName) {
+
+    // First, lookup for the field name in the dynamic field cache. In case it
+    // is available there, we save matching all the patterns by retrieving the
+    // type from it.
+    SchemaField field = dynamicFieldCache.getIfPresent(fieldName);
+    if (field != null) {
+      return field.getType();
+    }
+
     for (DynamicField df : dynamicFields) {
       if (df.matches(fieldName)) return df.prototype.getType();
     }

Reply via email to