Skip to content

Commit babeefa

Browse files
committed
Merge branch '1.20.1/dev' into 1.21.1/dev
2 parents 0031c48 + 610b168 commit babeefa

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

common/src/backend/java/dev/engine_room/flywheel/backend/util/AtomicBitSet.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -361,10 +361,6 @@ public int cardinality() {
361361
public void forEachSetSpan(BitSpanConsumer consumer) {
362362
AtomicBitSetSegments segments = this.segments.get();
363363

364-
if (segments.cardinality() == 0) {
365-
return;
366-
}
367-
368364
int start = -1;
369365
int end = -1;
370366

@@ -415,7 +411,8 @@ public int currentCapacity() {
415411
}
416412

417413
public boolean isEmpty() {
418-
return cardinality() == 0;
414+
return segments.get()
415+
.isEmpty();
419416
}
420417

421418
/**
@@ -545,6 +542,20 @@ private int cardinality() {
545542
return numSetBits;
546543
}
547544

545+
private boolean isEmpty() {
546+
// No need to count all set bits to just check if it's empty.
547+
// As soon as we encounter a set bit we can early out.
548+
for (int i = 0; i < numSegments(); i++) {
549+
long[] segment = getSegment(i);
550+
for (int j = 0; j < segment.length; j++) {
551+
if ((long) AA.getAcquire(segment, j) != 0) {
552+
return false;
553+
}
554+
}
555+
}
556+
return true;
557+
}
558+
548559
public int numSegments() {
549560
return segments.length;
550561
}

common/src/test/java/dev/engine_room/flywheel/backend/util/TestAtomicBitSet.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ void testNextClearBit() {
2929

3030
Assertions.assertEquals(5000, bs.nextClearBit(5000));
3131

32+
Assertions.assertTrue(bs.isEmpty());
3233
bs.set(16);
34+
Assertions.assertFalse(bs.isEmpty());
3335

3436
Assertions.assertEquals(0, bs.nextClearBit(0));
3537
Assertions.assertEquals(17, bs.nextClearBit(16));
@@ -57,10 +59,12 @@ void testSetRange() {
5759
var bs = new AtomicBitSet(log2SegmentSize, CAPACITY);
5860

5961
Assertions.assertEquals(0, bs.cardinality(), "BitSet should be empty initially");
62+
Assertions.assertTrue(bs.isEmpty());
6063

6164
bs.set(0, NUM_BITS_TO_SET);
6265

6366
Assertions.assertEquals(NUM_BITS_TO_SET, bs.cardinality(), "BitSet should have " + NUM_BITS_TO_SET + " bits set");
67+
Assertions.assertFalse(bs.isEmpty());
6468

6569
for (int i = 0; i < NUM_BITS_TO_SET; i++) {
6670
Assertions.assertTrue(bs.get(i), "Bit " + i + " should be set");
@@ -74,6 +78,7 @@ void testClearRange() {
7478
var bs = new AtomicBitSet(log2SegmentSize, CAPACITY);
7579

7680
Assertions.assertEquals(0, bs.cardinality(), "BitSet should be empty initially");
81+
Assertions.assertTrue(bs.isEmpty());
7782

7883
// Fill it halfway.
7984
bs.set(0, NUM_BITS_TO_SET);
@@ -105,10 +110,12 @@ void testSmallRange() {
105110
// Just clear everything.
106111
bs.clear();
107112
Assertions.assertEquals(0, bs.cardinality(), "BitSet should be empty after clearing");
113+
Assertions.assertTrue(bs.isEmpty());
108114

109115
// Should only fill the single long.
110116
bs.set(0, 64);
111117
Assertions.assertEquals(64, bs.cardinality(), "BitSet should have 64 bits set after setting all");
118+
Assertions.assertFalse(bs.isEmpty());
112119

113120
bs.clear(fromIndex, toIndex);
114121

@@ -124,16 +131,19 @@ void testBadInputs() {
124131

125132
Assertions.assertEquals(0, bs.cardinality(), "set with negative should have no effect");
126133
Assertions.assertEquals(64, bs.currentCapacity(), "set with negative should have no effect");
134+
Assertions.assertTrue(bs.isEmpty());
127135

128136
bs.clear(Integer.MAX_VALUE);
129137

130138
Assertions.assertEquals(0, bs.cardinality(), "clear with out of bounds index should have no effect");
131139
Assertions.assertEquals(64, bs.currentCapacity(), "clear with out of bounds index should have no effect");
140+
Assertions.assertTrue(bs.isEmpty());
132141

133142
bs.clear(-5);
134143

135144
Assertions.assertEquals(0, bs.cardinality(), "clear with negative index should have no effect");
136145
Assertions.assertEquals(64, bs.currentCapacity(), "clear with negative index should have no effect");
146+
Assertions.assertTrue(bs.isEmpty());
137147
}
138148

139149
@Test
@@ -147,13 +157,16 @@ void testQuadraticInputs() {
147157
for (int toIndex = 0; toIndex < maxIndex; toIndex++) {
148158
bs.clear();
149159

160+
Assertions.assertTrue(bs.isEmpty());
150161
Assertions.assertEquals(0, bs.cardinality());
151162

152163
bs.set(fromIndex, toIndex);
153164

154165
if (toIndex <= fromIndex) {
155166
Assertions.assertEquals(0, bs.cardinality(), "Setting range with toIndex < fromIndex should not set any bits");
167+
Assertions.assertTrue(bs.isEmpty());
156168
} else {
169+
Assertions.assertFalse(bs.isEmpty());
157170
assertRangeSet(bs, fromIndex, toIndex);
158171

159172
Assertions.assertEquals(toIndex - 1, bs.maxSetBit());
@@ -163,6 +176,7 @@ void testQuadraticInputs() {
163176

164177
// Now fill it completely and clear the range.
165178
bs.set(0, maxIndex);
179+
Assertions.assertFalse(bs.isEmpty());
166180

167181
assertRangeSet(bs, 0, maxIndex);
168182

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ mod_homepage = https://github.com/Engine-Room/Flywheel
88
# Flywheel metadata
99
flywheel_id=flywheel
1010
flywheel_name=Flywheel
11-
flywheel_version=1.0.5-beta
11+
flywheel_version=1.0.5
1212
flywheel_description=An overhauled entity and block entity rendering API.
1313
# Vanillin metadata
1414
vanillin_id=vanillin

0 commit comments

Comments
 (0)