On Wed, 11 Jan 2023 12:57:47 GMT, Jim Laskey <jlas...@openjdk.org> wrote:
>> The enanchment is useful for applications that make heavy use of BitSet >> objects as sets of integers, and therefore they need to make a lot of calls >> to cardinality() method, which actually require linear time in the number of >> words in use by the bit set. >> This optimization reduces the cost of calling cardinality() to constant >> time, as it simply returns the value of the field, and it also try to make >> as little effort as possible to update the field, when needed. >> >> Moreover, it has been implemented a new method for testing wheter a bit set >> includes another bit set (i.e., the set of true bits of the parameter is a >> subset of the true bits of the instance). > > src/java.base/share/classes/java/util/BitSet.java line 185: > >> 183: private BitSet(long[] words) { >> 184: this.words = words; >> 185: wordsInUse = words.length; > > Why did you drop the `this.` in front of wordsInUse? (non-standard) Simply an oversight. > src/java.base/share/classes/java/util/BitSet.java line 421: > >> 419: final long bitMask = 1L << bitIndex; >> 420: words[wordIndex] ^= bitMask; >> 421: cardinality += (words[wordIndex] & bitMask) != 0 ? 1 : -1; > > Isn't there a race condition here? > > I'm wondering if it would not be better to cache the last cardinality and > invalidate (negative cardinality) when the bit set is changed. Less intrusive > and potentially quicker. I agree with the fact that is less intrusive, but certainly not potentially quicker, since the complete recalculation of cardinality requires linear time in the value of wordsInUse ------------- PR: https://git.openjdk.org/jdk/pull/11837