Skip to content

Commit 1fddb83

Browse files
authored
Improve BiomeMask performance (#3082)
* Improve BiomeMask performance * comments
1 parent 5772290 commit 1fddb83

File tree

8 files changed

+72
-37
lines changed

8 files changed

+72
-37
lines changed

worldedit-bukkit/adapters/adapter-1_20_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R2/PaperweightPlatformAdapter.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -647,11 +647,12 @@ public static void clearCounts(final LevelChunkSection section) throws IllegalAc
647647

648648
public static BiomeType adapt(Holder<Biome> biome, LevelAccessor levelAccessor) {
649649
final Registry<Biome> biomeRegistry = levelAccessor.registryAccess().registryOrThrow(BIOME);
650-
if (biomeRegistry.getKey(biome.value()) == null) {
651-
return biomeRegistry.asHolderIdMap().getId(biome) == -1 ? BiomeTypes.OCEAN
652-
: null;
650+
final int id = biomeRegistry.getId(biome.value());
651+
if (id < 0) {
652+
// this shouldn't be the case, but other plugins can be weird
653+
return BiomeTypes.OCEAN;
653654
}
654-
return BiomeTypes.get(biome.unwrapKey().orElseThrow().location().toString());
655+
return BiomeTypes.getLegacy(id);
655656
}
656657

657658
static void removeBeacon(BlockEntity beacon, LevelChunk levelChunk) {

worldedit-bukkit/adapters/adapter-1_20_4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R3/PaperweightPlatformAdapter.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -647,11 +647,12 @@ public static void clearCounts(final LevelChunkSection section) throws IllegalAc
647647

648648
public static BiomeType adapt(Holder<Biome> biome, LevelAccessor levelAccessor) {
649649
final Registry<Biome> biomeRegistry = levelAccessor.registryAccess().registryOrThrow(BIOME);
650-
if (biomeRegistry.getKey(biome.value()) == null) {
651-
return biomeRegistry.asHolderIdMap().getId(biome) == -1 ? BiomeTypes.OCEAN
652-
: null;
650+
final int id = biomeRegistry.getId(biome.value());
651+
if (id < 0) {
652+
// this shouldn't be the case, but other plugins can be weird
653+
return BiomeTypes.OCEAN;
653654
}
654-
return BiomeTypes.get(biome.unwrapKey().orElseThrow().location().toString());
655+
return BiomeTypes.getLegacy(id);
655656
}
656657

657658
static void removeBeacon(BlockEntity beacon, LevelChunk levelChunk) {

worldedit-bukkit/adapters/adapter-1_20_5/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R4/PaperweightPlatformAdapter.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -640,11 +640,12 @@ public static void clearCounts(final LevelChunkSection section) throws IllegalAc
640640

641641
public static BiomeType adapt(Holder<Biome> biome, LevelAccessor levelAccessor) {
642642
final Registry<Biome> biomeRegistry = levelAccessor.registryAccess().registryOrThrow(BIOME);
643-
if (biomeRegistry.getKey(biome.value()) == null) {
644-
return biomeRegistry.asHolderIdMap().getId(biome) == -1 ? BiomeTypes.OCEAN
645-
: null;
643+
final int id = biomeRegistry.getId(biome.value());
644+
if (id < 0) {
645+
// this shouldn't be the case, but other plugins can be weird
646+
return BiomeTypes.OCEAN;
646647
}
647-
return BiomeTypes.get(biome.unwrapKey().orElseThrow().location().toString());
648+
return BiomeTypes.getLegacy(id);
648649
}
649650

650651
static void removeBeacon(BlockEntity beacon, LevelChunk levelChunk) {

worldedit-bukkit/adapters/adapter-1_21/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_R1/PaperweightPlatformAdapter.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -624,11 +624,12 @@ public static void clearCounts(final LevelChunkSection section) throws IllegalAc
624624

625625
public static BiomeType adapt(Holder<Biome> biome, LevelAccessor levelAccessor) {
626626
final Registry<Biome> biomeRegistry = levelAccessor.registryAccess().registryOrThrow(BIOME);
627-
if (biomeRegistry.getKey(biome.value()) == null) {
628-
return biomeRegistry.asHolderIdMap().getId(biome) == -1 ? BiomeTypes.OCEAN
629-
: null;
627+
final int id = biomeRegistry.getId(biome.value());
628+
if (id < 0) {
629+
// this shouldn't be the case, but other plugins can be weird
630+
return BiomeTypes.OCEAN;
630631
}
631-
return BiomeTypes.get(biome.unwrapKey().orElseThrow().location().toString());
632+
return BiomeTypes.getLegacy(id);
632633
}
633634

634635
static void removeBeacon(BlockEntity beacon, LevelChunk levelChunk) {

worldedit-bukkit/adapters/adapter-1_21_3/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_3/PaperweightPlatformAdapter.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -631,11 +631,12 @@ public static void clearCounts(final LevelChunkSection section) throws IllegalAc
631631

632632
public static BiomeType adapt(Holder<Biome> biome, LevelAccessor levelAccessor) {
633633
final Registry<Biome> biomeRegistry = levelAccessor.registryAccess().lookupOrThrow(BIOME);
634-
if (biomeRegistry.getKey(biome.value()) == null) {
635-
return biomeRegistry.asHolderIdMap().getId(biome) == -1 ? BiomeTypes.OCEAN
636-
: null;
634+
final int id = biomeRegistry.getId(biome.value());
635+
if (id < 0) {
636+
// this shouldn't be the case, but other plugins can be weird
637+
return BiomeTypes.OCEAN;
637638
}
638-
return BiomeTypes.get(biome.unwrapKey().orElseThrow().location().toString());
639+
return BiomeTypes.getLegacy(id);
639640
}
640641

641642
static void removeBeacon(BlockEntity beacon, LevelChunk levelChunk) {

worldedit-bukkit/adapters/adapter-1_21_4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_4/PaperweightPlatformAdapter.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -626,11 +626,12 @@ public static void clearCounts(final LevelChunkSection section) throws IllegalAc
626626

627627
public static BiomeType adapt(Holder<Biome> biome, LevelAccessor levelAccessor) {
628628
final Registry<Biome> biomeRegistry = levelAccessor.registryAccess().lookupOrThrow(BIOME);
629-
if (biomeRegistry.getKey(biome.value()) == null) {
630-
return biomeRegistry.asHolderIdMap().getId(biome) == -1 ? BiomeTypes.OCEAN
631-
: null;
629+
final int id = biomeRegistry.getId(biome.value());
630+
if (id < 0) {
631+
// this shouldn't be the case, but other plugins can be weird
632+
return BiomeTypes.OCEAN;
632633
}
633-
return BiomeTypes.get(biome.unwrapKey().orElseThrow().location().toString());
634+
return BiomeTypes.getLegacy(id);
634635
}
635636

636637
static void removeBeacon(BlockEntity beacon, LevelChunk levelChunk) {

worldedit-core/src/main/java/com/sk89q/worldedit/function/mask/BiomeMask.java

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
import javax.annotation.Nullable;
2727
import java.util.Arrays;
2828
import java.util.Collection;
29-
import java.util.HashSet;
30-
import java.util.Set;
3129

3230
import static com.google.common.base.Preconditions.checkNotNull;
3331

@@ -38,7 +36,9 @@
3836
public class BiomeMask extends AbstractExtentMask {
3937
//FAWE end
4038

41-
private final Set<BiomeType> biomes = new HashSet<>();
39+
//FAWE start - avoid HashSet usage
40+
private final boolean[] biomes;
41+
//FAWE end
4242

4343
/**
4444
* Create a new biome mask.
@@ -51,7 +51,17 @@ public BiomeMask(Extent extent, Collection<BiomeType> biomes) {
5151
super(extent);
5252
//FAWE end
5353
checkNotNull(biomes);
54-
this.biomes.addAll(biomes);
54+
//FAWE start - avoid HashSet usage
55+
this.biomes = new boolean[BiomeType.REGISTRY.size()];
56+
for (final BiomeType biome : biomes) {
57+
this.biomes[biome.getInternalId()] = true;
58+
}
59+
//FAWE end
60+
}
61+
62+
private BiomeMask(Extent extent, boolean[] biomes) {
63+
super(extent);
64+
this.biomes = biomes;
5565
}
5666

5767
/**
@@ -71,7 +81,11 @@ public BiomeMask(Extent extent, BiomeType... biome) {
7181
*/
7282
public void add(Collection<BiomeType> biomes) {
7383
checkNotNull(biomes);
74-
this.biomes.addAll(biomes);
84+
//FAWE start - avoid HashSet usage
85+
for (final BiomeType biome : biomes) {
86+
this.biomes[biome.getInternalId()] = true;
87+
}
88+
//FAWE end
7589
}
7690

7791
/**
@@ -89,13 +103,17 @@ public void add(BiomeType... biome) {
89103
* @return a list of biomes
90104
*/
91105
public Collection<BiomeType> getBiomes() {
92-
return biomes;
106+
//FAWE start - avoid HashSet usage
107+
return BiomeType.REGISTRY.values().stream().filter(type -> biomes[type.getInternalId()]).toList();
108+
//FAWE end
93109
}
94110

95111
@Override
96112
public boolean test(BlockVector3 vector) {
113+
//FAWE start - avoid HashSet usage
97114
BiomeType biome = vector.getBiome(getExtent());
98-
return biomes.contains(biome);
115+
return biomes[biome.getInternalId()];
116+
//FAWE end
99117
}
100118

101119
@Nullable
@@ -107,14 +125,14 @@ public Mask2D toMask2D() {
107125
//FAWE start
108126
@Override
109127
public Mask copy() {
110-
return new BiomeMask(getExtent(), new HashSet<>(biomes));
128+
return new BiomeMask(getExtent(), this.biomes.clone());
111129
}
112-
//FAWE end
113130

114131
@Override
115132
public boolean test(Extent extent, BlockVector3 position) {
116133
BiomeType biome = getExtent().getBiome(position);
117-
return biomes.contains(biome);
134+
return biomes[biome.getInternalId()];
118135
}
136+
//FAWE end
119137

120138
}

worldedit-core/src/main/java/com/sk89q/worldedit/world/biome/BiomeTypes.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
package com.sk89q.worldedit.world.biome;
2121

2222
import javax.annotation.Nullable;
23+
import java.util.Arrays;
2324
import java.util.Collection;
25+
import java.util.Comparator;
26+
import java.util.List;
2427

2528
/**
2629
* Stores a list of common {@link BiomeType BiomeTypes}.
@@ -297,10 +300,18 @@ public static BiomeType register(final BiomeType biome) {
297300
}
298301

299302
public static BiomeType getLegacy(int legacyId) {
300-
for (BiomeType type : values()) {
301-
if (type.getLegacyId() == legacyId) {
302-
return type;
303+
class BiomeTypeIdCache {
304+
private static final List<BiomeType> byId = create();
305+
306+
private static List<BiomeType> create() {
307+
BiomeType[] array = values().toArray(new BiomeType[0]);
308+
Arrays.sort(array, Comparator.comparing(BiomeType::getLegacyId));
309+
return List.of(array);
303310
}
311+
312+
}
313+
if (legacyId >= 0 && legacyId < BiomeTypeIdCache.byId.size()) {
314+
return BiomeTypeIdCache.byId.get(legacyId);
304315
}
305316
return null;
306317
}

0 commit comments

Comments
 (0)