You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by gu...@apache.org on 2023/10/25 02:42:33 UTC

[lucene] branch branch_9x updated: Use Arrays#mismatch for Outputs#common operations (#12710)

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

guofeng pushed a commit to branch branch_9x
in repository https://gitbox.apache.org/repos/asf/lucene.git


The following commit(s) were added to refs/heads/branch_9x by this push:
     new 8b38b734cc4 Use Arrays#mismatch for Outputs#common operations (#12710)
8b38b734cc4 is described below

commit 8b38b734cc4798fc5b6c7d1130771a6c7e23357b
Author: gf2121 <52...@users.noreply.github.com>
AuthorDate: Wed Oct 25 10:25:10 2023 +0800

    Use Arrays#mismatch for Outputs#common operations (#12710)
---
 lucene/CHANGES.txt                                 |  2 ++
 .../lucene/util/fst/ByteSequenceOutputs.java       | 30 ++++++++++++----------
 .../lucene/util/fst/CharSequenceOutputs.java       | 30 ++++++++++++----------
 .../apache/lucene/util/fst/IntSequenceOutputs.java | 30 ++++++++++++----------
 4 files changed, 50 insertions(+), 42 deletions(-)

diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 7bf82bf3761..d1b61f71339 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -227,6 +227,8 @@ Optimizations
 * GITHUB#12589: Disjunctions now sometimes run as conjunctions when the minimum
   competitive score requires multiple clauses to match. (Adrien Grand)
 
+* GITHUB#12710: Use Arrays#mismatch for Outputs#common operations. (Guo Feng)
+
 Changes in runtime behavior
 ---------------------
 
diff --git a/lucene/core/src/java/org/apache/lucene/util/fst/ByteSequenceOutputs.java b/lucene/core/src/java/org/apache/lucene/util/fst/ByteSequenceOutputs.java
index 261b5e9bd43..dcfc552dd4b 100644
--- a/lucene/core/src/java/org/apache/lucene/util/fst/ByteSequenceOutputs.java
+++ b/lucene/core/src/java/org/apache/lucene/util/fst/ByteSequenceOutputs.java
@@ -17,6 +17,7 @@
 package org.apache.lucene.util.fst;
 
 import java.io.IOException;
+import java.util.Arrays;
 import org.apache.lucene.store.DataInput;
 import org.apache.lucene.store.DataOutput;
 import org.apache.lucene.util.BytesRef;
@@ -44,28 +45,29 @@ public final class ByteSequenceOutputs extends Outputs<BytesRef> {
     assert output1 != null;
     assert output2 != null;
 
-    int pos1 = output1.offset;
-    int pos2 = output2.offset;
-    int stopAt1 = pos1 + Math.min(output1.length, output2.length);
-    while (pos1 < stopAt1) {
-      if (output1.bytes[pos1] != output2.bytes[pos2]) {
-        break;
-      }
-      pos1++;
-      pos2++;
-    }
+    int mismatchPos =
+        Arrays.mismatch(
+            output1.bytes,
+            output1.offset,
+            output1.offset + output1.length,
+            output2.bytes,
+            output2.offset,
+            output2.offset + output2.length);
 
-    if (pos1 == output1.offset) {
+    if (mismatchPos == 0) {
       // no common prefix
       return NO_OUTPUT;
-    } else if (pos1 == output1.offset + output1.length) {
+    } else if (mismatchPos == -1) {
+      // exactly equals
+      return output1;
+    } else if (mismatchPos == output1.length) {
       // output1 is a prefix of output2
       return output1;
-    } else if (pos2 == output2.offset + output2.length) {
+    } else if (mismatchPos == output2.length) {
       // output2 is a prefix of output1
       return output2;
     } else {
-      return new BytesRef(output1.bytes, output1.offset, pos1 - output1.offset);
+      return new BytesRef(output1.bytes, output1.offset, mismatchPos);
     }
   }
 
diff --git a/lucene/core/src/java/org/apache/lucene/util/fst/CharSequenceOutputs.java b/lucene/core/src/java/org/apache/lucene/util/fst/CharSequenceOutputs.java
index 707b5f9973e..f3655fdd284 100644
--- a/lucene/core/src/java/org/apache/lucene/util/fst/CharSequenceOutputs.java
+++ b/lucene/core/src/java/org/apache/lucene/util/fst/CharSequenceOutputs.java
@@ -17,6 +17,7 @@
 package org.apache.lucene.util.fst;
 
 import java.io.IOException;
+import java.util.Arrays;
 import org.apache.lucene.store.DataInput;
 import org.apache.lucene.store.DataOutput;
 import org.apache.lucene.util.CharsRef;
@@ -43,28 +44,29 @@ public final class CharSequenceOutputs extends Outputs<CharsRef> {
     assert output1 != null;
     assert output2 != null;
 
-    int pos1 = output1.offset;
-    int pos2 = output2.offset;
-    int stopAt1 = pos1 + Math.min(output1.length, output2.length);
-    while (pos1 < stopAt1) {
-      if (output1.chars[pos1] != output2.chars[pos2]) {
-        break;
-      }
-      pos1++;
-      pos2++;
-    }
+    int mismatchPos =
+        Arrays.mismatch(
+            output1.chars,
+            output1.offset,
+            output1.offset + output1.length,
+            output2.chars,
+            output2.offset,
+            output2.offset + output2.length);
 
-    if (pos1 == output1.offset) {
+    if (mismatchPos == 0) {
       // no common prefix
       return NO_OUTPUT;
-    } else if (pos1 == output1.offset + output1.length) {
+    } else if (mismatchPos == -1) {
+      // exactly equals
+      return output1;
+    } else if (mismatchPos == output1.length) {
       // output1 is a prefix of output2
       return output1;
-    } else if (pos2 == output2.offset + output2.length) {
+    } else if (mismatchPos == output2.length) {
       // output2 is a prefix of output1
       return output2;
     } else {
-      return new CharsRef(output1.chars, output1.offset, pos1 - output1.offset);
+      return new CharsRef(output1.chars, output1.offset, mismatchPos);
     }
   }
 
diff --git a/lucene/core/src/java/org/apache/lucene/util/fst/IntSequenceOutputs.java b/lucene/core/src/java/org/apache/lucene/util/fst/IntSequenceOutputs.java
index e81d13ee46e..2dcf37bc887 100644
--- a/lucene/core/src/java/org/apache/lucene/util/fst/IntSequenceOutputs.java
+++ b/lucene/core/src/java/org/apache/lucene/util/fst/IntSequenceOutputs.java
@@ -17,6 +17,7 @@
 package org.apache.lucene.util.fst;
 
 import java.io.IOException;
+import java.util.Arrays;
 import org.apache.lucene.store.DataInput;
 import org.apache.lucene.store.DataOutput;
 import org.apache.lucene.util.IntsRef;
@@ -43,28 +44,29 @@ public final class IntSequenceOutputs extends Outputs<IntsRef> {
     assert output1 != null;
     assert output2 != null;
 
-    int pos1 = output1.offset;
-    int pos2 = output2.offset;
-    int stopAt1 = pos1 + Math.min(output1.length, output2.length);
-    while (pos1 < stopAt1) {
-      if (output1.ints[pos1] != output2.ints[pos2]) {
-        break;
-      }
-      pos1++;
-      pos2++;
-    }
+    int mismatchPos =
+        Arrays.mismatch(
+            output1.ints,
+            output1.offset,
+            output1.offset + output1.length,
+            output2.ints,
+            output2.offset,
+            output2.offset + output2.length);
 
-    if (pos1 == output1.offset) {
+    if (mismatchPos == 0) {
       // no common prefix
       return NO_OUTPUT;
-    } else if (pos1 == output1.offset + output1.length) {
+    } else if (mismatchPos == -1) {
+      // exactly equals
+      return output1;
+    } else if (mismatchPos == output1.length) {
       // output1 is a prefix of output2
       return output1;
-    } else if (pos2 == output2.offset + output2.length) {
+    } else if (mismatchPos == output2.length) {
       // output2 is a prefix of output1
       return output2;
     } else {
-      return new IntsRef(output1.ints, output1.offset, pos1 - output1.offset);
+      return new IntsRef(output1.ints, output1.offset, mismatchPos);
     }
   }