feat[buffer]: 使用primitive type的高性能set

This commit is contained in:
godotg
2022-09-24 18:15:11 +08:00
parent 18652f366d
commit 438d4894ca
2 changed files with 14 additions and 15 deletions
@@ -844,9 +844,9 @@ public abstract class ByteBufUtils {
public static Set<Byte> readByteSet(ByteBuf byteBuf) {
var length = readInt(byteBuf);
var set = (Set<Byte>) CollectionUtils.newFixedSet(length);
var set = new HashByteSet(length);
for (var i = 0; i < length; i++) {
set.add(readByteBox(byteBuf));
set.add(readByte(byteBuf));
}
return set;
}
@@ -930,9 +930,9 @@ public abstract class ByteBufUtils {
public static Set<Short> readShortSet(ByteBuf byteBuf) {
var length = readInt(byteBuf);
var set = (Set<Short>) CollectionUtils.newFixedSet(length);
var set = new HashShortSet(length);
for (var i = 0; i < length; i++) {
set.add(readShortBox(byteBuf));
set.add(readShort(byteBuf));
}
return set;
}
@@ -1009,9 +1009,9 @@ public abstract class ByteBufUtils {
public static Set<Integer> readIntSet(ByteBuf byteBuf) {
var length = readInt(byteBuf);
var set = (Set<Integer>) CollectionUtils.newFixedSet(length);
var set = new HashIntSet(length);
for (var i = 0; i < length; i++) {
set.add(readIntBox(byteBuf));
set.add(readInt(byteBuf));
}
return set;
}
@@ -1088,9 +1088,9 @@ public abstract class ByteBufUtils {
public static Set<Long> readLongSet(ByteBuf byteBuf) {
var length = readInt(byteBuf);
var set = (Set<Long>) CollectionUtils.newFixedSet(length);
var set = new HashLongSet(length);
for (var i = 0; i < length; i++) {
set.add(readLongBox(byteBuf));
set.add(readLong(byteBuf));
}
return set;
}
@@ -49,19 +49,18 @@ public class FixedCollectionTest {
@Test
public void testFixedHashInt() {
var list = new FixedSizeListInt(3);
list.set(0, 1);
list.set(1, 2);
list.set(2, 4);
var set = new FixedHashSetInt(list);
public void testHashIntSet() {
var set = new HashIntSet(3);
set.add(1);
set.add(2);
set.add(3);
for (int i = 0; i < set.size(); i++) {
for (var ele : set) {
// test iterator
}
}
Assert.assertTrue(set.contains(4));
Assert.assertTrue(set.contains(3));
}
}