RFR: 8312019: Simplify and modernize java.util.BitSet.equals

Pavel Rappo prappo at openjdk.org
Wed Jul 19 20:50:41 UTC 2023


On Thu, 13 Jul 2023 14:50:55 GMT, Roger Riggs <rriggs at openjdk.org> wrote:

>> Please review this PR to use modern APIs and language features to simplify equals for BitSet. 
>> 
>> I couldn't see how to refactor hashCode using Arrays utility methods in a way that preserves its specification. So, aside from refactoring its doc comment and annotating it with `@Overrides`, I left it intact.
>
> LGTM

@RogerRiggs, @Martin-Buchholz, are you okay with a few redundant range checks?

`Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex)` performs two range checks, which aren't needed here. However, `Arrays.equals` is readable; a less readable but leaner alternative would be `ArraysSupport.mismatch(a, aFromIndex, b, bFromIndex, length)`. Here's a diff from this PR's version, to consider:


diff --git a/src/java.base/share/classes/java/util/BitSet.java b/src/java.base/share/classes/java/util/BitSet.java
index 7189dc49228..fea9693d7ed 100644
--- a/src/java.base/share/classes/java/util/BitSet.java
+++ b/src/java.base/share/classes/java/util/BitSet.java
@@ -33,6 +33,8 @@ import java.util.function.IntConsumer;
 import java.util.stream.IntStream;
 import java.util.stream.StreamSupport;
 
+import jdk.internal.util.ArraysSupport;
+
 /**
  * This class implements a vector of bits that grows as needed. Each
  * component of the bit set has a {@code boolean} value. The
@@ -1076,8 +1078,11 @@ public class BitSet implements Cloneable, java.io.Serializable {
         checkInvariants();
         set.checkInvariants();
 
+        if (wordsInUse != set.wordsInUse)
+            return false;
+
         // Check words in use by both BitSets
-        return Arrays.equals(words, 0, wordsInUse, set.words, 0, set.wordsInUse);
+        return ArraysSupport.mismatch(words, 0, set.words, 0, wordsInUse) == -1;
     }
 
     /**

-------------

PR Comment: https://git.openjdk.org/jdk/pull/14868#issuecomment-1642734404


More information about the core-libs-dev mailing list