Microoptimization cleanup

Just some small microoptimizations including preferring fastutils, Removing unnecessary checks already dealt with using @NotNull, all since I had extra unplanned free time.
This commit is contained in:
LeoDog896 2021-01-15 11:08:11 -05:00
parent 238ea649ab
commit 3536bdc856
21 changed files with 14 additions and 44 deletions

3
.gitignore vendored
View File

@ -53,3 +53,6 @@ gradle-app.setting
# When running the demo we generate the extensions folder # When running the demo we generate the extensions folder
/extensions/ /extensions/
# When compiling we get a docs folder
/docs

View File

@ -1,5 +1,8 @@
package net.minestom.server.map; package net.minestom.server.map;
import it.unimi.dsi.fastutil.ints.Int2IntMap;
import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.File; import java.io.File;
@ -10,7 +13,7 @@ import java.util.Map;
public class PaletteGenerator { public class PaletteGenerator {
public static void main(String[] args) { public static void main(String[] args) {
Map<Integer, Integer> colors = new HashMap<>(); Int2IntMap colors = new Int2IntOpenHashMap();
int highestIndex = 0; int highestIndex = 0;
for(MapColors c : MapColors.values()) { for(MapColors c : MapColors.values()) {
if (c == MapColors.NONE) if (c == MapColors.NONE)

View File

@ -217,7 +217,6 @@ public final class MinecraftServer {
* @throws NullPointerException if {@code brandName} is null * @throws NullPointerException if {@code brandName} is null
*/ */
public static void setBrandName(@NotNull String brandName) { public static void setBrandName(@NotNull String brandName) {
Check.notNull(brandName, "The brand name cannot be null");
MinecraftServer.brandName = brandName; MinecraftServer.brandName = brandName;
PacketUtils.sendGroupedPacket(connectionManager.getOnlinePlayers(), PluginMessagePacket.getBrandPacket()); PacketUtils.sendGroupedPacket(connectionManager.getOnlinePlayers(), PluginMessagePacket.getBrandPacket());
@ -275,7 +274,6 @@ public final class MinecraftServer {
* @param difficulty the new server difficulty * @param difficulty the new server difficulty
*/ */
public static void setDifficulty(@NotNull Difficulty difficulty) { public static void setDifficulty(@NotNull Difficulty difficulty) {
Check.notNull(difficulty, "The server difficulty cannot be null.");
MinecraftServer.difficulty = difficulty; MinecraftServer.difficulty = difficulty;
// Send the packet to all online players // Send the packet to all online players

View File

@ -140,7 +140,6 @@ public final class UpdateManager {
* @throws NullPointerException if <code>threadProvider</code> is null * @throws NullPointerException if <code>threadProvider</code> is null
*/ */
public synchronized void setThreadProvider(ThreadProvider threadProvider) { public synchronized void setThreadProvider(ThreadProvider threadProvider) {
Check.notNull(threadProvider, "The thread provider cannot be null");
this.threadProvider = threadProvider; this.threadProvider = threadProvider;
} }

View File

@ -73,7 +73,6 @@ public class AdvancementTab implements Viewable {
* @param parent the parent of this advancement, it cannot be null * @param parent the parent of this advancement, it cannot be null
*/ */
public void createAdvancement(@NotNull String identifier, @NotNull Advancement advancement, @NotNull Advancement parent) { public void createAdvancement(@NotNull String identifier, @NotNull Advancement advancement, @NotNull Advancement parent) {
Check.argCondition(identifier == null, "the advancement identifier cannot be null");
Check.stateCondition(!advancementMap.containsKey(parent), Check.stateCondition(!advancementMap.containsKey(parent),
"You tried to set a parent which doesn't exist or isn't registered"); "You tried to set a parent which doesn't exist or isn't registered");
cacheAdvancement(identifier, advancement, parent); cacheAdvancement(identifier, advancement, parent);

View File

@ -37,7 +37,6 @@ public class RichMessage extends JsonMessage {
* @return the created rich message object * @return the created rich message object
*/ */
public static RichMessage of(@NotNull ColoredText coloredText) { public static RichMessage of(@NotNull ColoredText coloredText) {
Check.notNull(coloredText, "ColoredText cannot be null");
RichMessage richMessage = new RichMessage(); RichMessage richMessage = new RichMessage();
appendText(richMessage, coloredText); appendText(richMessage, coloredText);
@ -58,7 +57,6 @@ public class RichMessage extends JsonMessage {
* @return the rich message * @return the rich message
*/ */
public RichMessage append(@NotNull ColoredText coloredText) { public RichMessage append(@NotNull ColoredText coloredText) {
Check.notNull(coloredText, "ColoredText cannot be null");
appendText(this, coloredText); appendText(this, coloredText);
return this; return this;

View File

@ -185,8 +185,6 @@ public final class CommandManager {
* @return true if the command hadn't been cancelled and has been successful * @return true if the command hadn't been cancelled and has been successful
*/ */
public boolean execute(@NotNull CommandSender sender, @NotNull String command) { public boolean execute(@NotNull CommandSender sender, @NotNull String command) {
Check.notNull(sender, "Source cannot be null");
Check.notNull(command, "Command string cannot be null");
// Command event // Command event
if (sender instanceof Player) { if (sender instanceof Player) {

View File

@ -242,7 +242,6 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P
* @throws IllegalStateException if you try to teleport an entity before settings its instance * @throws IllegalStateException if you try to teleport an entity before settings its instance
*/ */
public void teleport(@NotNull Position position, @Nullable long[] chunks, @Nullable Runnable callback) { public void teleport(@NotNull Position position, @Nullable long[] chunks, @Nullable Runnable callback) {
Check.notNull(position, "Teleport position cannot be null");
Check.stateCondition(instance == null, "You need to use Entity#setInstance before teleporting an entity!"); Check.stateCondition(instance == null, "You need to use Entity#setInstance before teleporting an entity!");
final ChunkCallback endCallback = (chunk) -> { final ChunkCallback endCallback = (chunk) -> {
@ -329,7 +328,6 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P
@Override @Override
public boolean addViewer(@NotNull Player player) { public boolean addViewer(@NotNull Player player) {
Check.notNull(player, "Viewer cannot be null");
boolean result = this.viewers.add(player); boolean result = this.viewers.add(player);
if (!result) if (!result)
return false; return false;
@ -339,7 +337,6 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P
@Override @Override
public boolean removeViewer(@NotNull Player player) { public boolean removeViewer(@NotNull Player player) {
Check.notNull(player, "Viewer cannot be null");
if (!viewers.remove(player)) if (!viewers.remove(player))
return false; return false;
@ -757,7 +754,6 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P
* @throws IllegalStateException if {@code instance} has not been registered in {@link InstanceManager} * @throws IllegalStateException if {@code instance} has not been registered in {@link InstanceManager}
*/ */
public void setInstance(@NotNull Instance instance) { public void setInstance(@NotNull Instance instance) {
Check.notNull(instance, "instance cannot be null!");
Check.stateCondition(!instance.isRegistered(), Check.stateCondition(!instance.isRegistered(),
"Instances need to be registered, please use InstanceManager#registerInstance or InstanceManager#registerSharedInstance"); "Instances need to be registered, please use InstanceManager#registerInstance or InstanceManager#registerSharedInstance");
@ -866,7 +862,6 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P
* @return the distance between this and {@code entity} * @return the distance between this and {@code entity}
*/ */
public float getDistance(@NotNull Entity entity) { public float getDistance(@NotNull Entity entity) {
Check.notNull(entity, "Entity cannot be null");
return getPosition().getDistance(entity.getPosition()); return getPosition().getDistance(entity.getPosition());
} }
@ -888,7 +883,6 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P
* @throws IllegalStateException if {@link #getInstance()} returns null * @throws IllegalStateException if {@link #getInstance()} returns null
*/ */
public void addPassenger(@NotNull Entity entity) { public void addPassenger(@NotNull Entity entity) {
Check.notNull(entity, "Passenger cannot be null");
Check.stateCondition(instance == null, "You need to set an instance using Entity#setInstance"); Check.stateCondition(instance == null, "You need to set an instance using Entity#setInstance");
if (entity.getVehicle() != null) { if (entity.getVehicle() != null) {
@ -909,7 +903,6 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P
* @throws IllegalStateException if {@link #getInstance()} returns null * @throws IllegalStateException if {@link #getInstance()} returns null
*/ */
public void removePassenger(@NotNull Entity entity) { public void removePassenger(@NotNull Entity entity) {
Check.notNull(entity, "Passenger cannot be null");
Check.stateCondition(instance == null, "You need to set an instance using Entity#setInstance"); Check.stateCondition(instance == null, "You need to set an instance using Entity#setInstance");
if (!passengers.remove(entity)) if (!passengers.remove(entity))

View File

@ -272,7 +272,6 @@ public abstract class LivingEntity extends Entity implements EquipmentHandler {
* @return true if damage has been applied, false if it didn't * @return true if damage has been applied, false if it didn't
*/ */
public boolean damage(@NotNull DamageType type, float value) { public boolean damage(@NotNull DamageType type, float value) {
Check.notNull(type, "The damage type cannot be null!");
if (isDead()) if (isDead())
return false; return false;
if (isInvulnerable() || isImmune(type)) { if (isInvulnerable() || isImmune(type)) {

View File

@ -670,7 +670,6 @@ public class Player extends LivingEntity implements CommandSender {
* @param spawnPosition the new position of the player * @param spawnPosition the new position of the player
*/ */
public void setInstance(@NotNull Instance instance, @NotNull Position spawnPosition) { public void setInstance(@NotNull Instance instance, @NotNull Position spawnPosition) {
Check.notNull(instance, "instance cannot be null!");
Check.argCondition(this.instance == instance, "Instance should be different than the current one"); Check.argCondition(this.instance == instance, "Instance should be different than the current one");
// true if the chunks need to be sent to the client, can be false if the instances share the same chunks (eg SharedInstance) // true if the chunks need to be sent to the client, can be false if the instances share the same chunks (eg SharedInstance)
@ -1364,7 +1363,6 @@ public class Player extends LivingEntity implements CommandSender {
* @param resourcePack the resource pack * @param resourcePack the resource pack
*/ */
public void setResourcePack(@NotNull ResourcePack resourcePack) { public void setResourcePack(@NotNull ResourcePack resourcePack) {
Check.notNull(resourcePack, "The resource pack cannot be null");
final String url = resourcePack.getUrl(); final String url = resourcePack.getUrl();
final String hash = resourcePack.getHash(); final String hash = resourcePack.getHash();
@ -1737,7 +1735,6 @@ public class Player extends LivingEntity implements CommandSender {
* @param gameMode the new player GameMode * @param gameMode the new player GameMode
*/ */
public void setGameMode(@NotNull GameMode gameMode) { public void setGameMode(@NotNull GameMode gameMode) {
Check.notNull(gameMode, "GameMode cannot be null");
this.gameMode = gameMode; this.gameMode = gameMode;
// Condition to prevent sending the packets before spawning the player // Condition to prevent sending the packets before spawning the player
@ -1766,7 +1763,6 @@ public class Player extends LivingEntity implements CommandSender {
* @param dimensionType the new player dimension * @param dimensionType the new player dimension
*/ */
protected void sendDimension(@NotNull DimensionType dimensionType) { protected void sendDimension(@NotNull DimensionType dimensionType) {
Check.notNull(dimensionType, "Dimension cannot be null!");
Check.argCondition(dimensionType.equals(getDimensionType()), "The dimension needs to be different than the current one!"); Check.argCondition(dimensionType.equals(getDimensionType()), "The dimension needs to be different than the current one!");
this.dimensionType = dimensionType; this.dimensionType = dimensionType;
@ -1900,7 +1896,6 @@ public class Player extends LivingEntity implements CommandSender {
* @return true if the inventory has been opened/sent to the player, false otherwise (cancelled by event) * @return true if the inventory has been opened/sent to the player, false otherwise (cancelled by event)
*/ */
public boolean openInventory(@NotNull Inventory inventory) { public boolean openInventory(@NotNull Inventory inventory) {
Check.notNull(inventory, "Inventory cannot be null, use Player#closeInventory() to close current");
InventoryOpenEvent inventoryOpenEvent = new InventoryOpenEvent(inventory, this); InventoryOpenEvent inventoryOpenEvent = new InventoryOpenEvent(inventory, this);

View File

@ -30,7 +30,6 @@ public interface NavigableEntity {
* @param speed define how far the entity will move * @param speed define how far the entity will move
*/ */
default void moveTowards(@NotNull Position direction, float speed) { default void moveTowards(@NotNull Position direction, float speed) {
Check.notNull(direction, "The direction cannot be null");
final Position position = getNavigableEntity().getPosition(); final Position position = getNavigableEntity().getPosition();

View File

@ -37,8 +37,6 @@ public interface EventHandler {
* @return true if the callback collection changed as a result of the call * @return true if the callback collection changed as a result of the call
*/ */
default <E extends Event> boolean addEventCallback(@NotNull Class<E> eventClass, @NotNull EventCallback<E> eventCallback) { default <E extends Event> boolean addEventCallback(@NotNull Class<E> eventClass, @NotNull EventCallback<E> eventCallback) {
Check.notNull(eventClass, "Event class cannot be null");
Check.notNull(eventCallback, "Event callback cannot be null");
Collection<EventCallback> callbacks = getEventCallbacks(eventClass); Collection<EventCallback> callbacks = getEventCallbacks(eventClass);
return callbacks.add(eventCallback); return callbacks.add(eventCallback);
} }
@ -52,8 +50,6 @@ public interface EventHandler {
* @return true if the callback was removed as a result of this call * @return true if the callback was removed as a result of this call
*/ */
default <E extends Event> boolean removeEventCallback(@NotNull Class<E> eventClass, @NotNull EventCallback<E> eventCallback) { default <E extends Event> boolean removeEventCallback(@NotNull Class<E> eventClass, @NotNull EventCallback<E> eventCallback) {
Check.notNull(eventClass, "Event class cannot be null");
Check.notNull(eventCallback, "Event callback cannot be null");
Collection<EventCallback> callbacks = getEventCallbacks(eventClass); Collection<EventCallback> callbacks = getEventCallbacks(eventClass);
return callbacks.remove(eventCallback); return callbacks.remove(eventCallback);
} }
@ -67,7 +63,6 @@ public interface EventHandler {
*/ */
@NotNull @NotNull
default <E extends Event> Collection<EventCallback> getEventCallbacks(@NotNull Class<E> eventClass) { default <E extends Event> Collection<EventCallback> getEventCallbacks(@NotNull Class<E> eventClass) {
Check.notNull(eventClass, "Event class cannot be null");
return getEventCallbacksMap().computeIfAbsent(eventClass, clazz -> new CopyOnWriteArraySet<>()); return getEventCallbacksMap().computeIfAbsent(eventClass, clazz -> new CopyOnWriteArraySet<>());
} }

View File

@ -71,13 +71,10 @@ public interface BlockModifier {
} }
default void setBlock(@NotNull BlockPosition blockPosition, @NotNull Block block) { default void setBlock(@NotNull BlockPosition blockPosition, @NotNull Block block) {
Check.notNull(blockPosition, "The block position cannot be null");
Check.notNull(block, "The block cannot be null");
setBlock(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ(), block); setBlock(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ(), block);
} }
default void setBlockStateId(@NotNull BlockPosition blockPosition, short blockStateId) { default void setBlockStateId(@NotNull BlockPosition blockPosition, short blockStateId) {
Check.notNull(blockPosition, "The block position cannot be null");
setBlockStateId(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ(), blockStateId); setBlockStateId(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ(), blockStateId);
} }
@ -97,7 +94,6 @@ public interface BlockModifier {
} }
default void setCustomBlock(@NotNull BlockPosition blockPosition, @NotNull String customBlockId) { default void setCustomBlock(@NotNull BlockPosition blockPosition, @NotNull String customBlockId) {
Check.notNull(blockPosition, "The block position cannot be null");
setCustomBlock(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ(), customBlockId); setCustomBlock(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ(), customBlockId);
} }
@ -106,7 +102,6 @@ public interface BlockModifier {
} }
default void setSeparateBlocks(@NotNull BlockPosition blockPosition, short blockStateId, short customBlockId) { default void setSeparateBlocks(@NotNull BlockPosition blockPosition, short blockStateId, short customBlockId) {
Check.notNull(blockPosition, "The block position cannot be null");
setSeparateBlocks(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ(), blockStateId, customBlockId, null); setSeparateBlocks(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ(), blockStateId, customBlockId, null);
} }

View File

@ -1,5 +1,6 @@
package net.minestom.server.instance; package net.minestom.server.instance;
import it.unimi.dsi.fastutil.ints.IntSet;
import net.minestom.server.MinecraftServer; import net.minestom.server.MinecraftServer;
import net.minestom.server.Viewable; import net.minestom.server.Viewable;
import net.minestom.server.data.Data; import net.minestom.server.data.Data;
@ -196,7 +197,7 @@ public abstract class Chunk implements Viewable, DataContainer {
* @return the block entities in this chunk * @return the block entities in this chunk
*/ */
@NotNull @NotNull
public abstract Set<Integer> getBlockEntities(); public abstract IntSet getBlockEntities();
/** /**
* Gets the last time that this chunk changed. * Gets the last time that this chunk changed.

View File

@ -193,7 +193,7 @@ public class DynamicChunk extends Chunk {
@NotNull @NotNull
@Override @Override
public Set<Integer> getBlockEntities() { public IntSet getBlockEntities() {
return blockEntities; return blockEntities;
} }

View File

@ -508,7 +508,6 @@ public class InstanceContainer extends Instance {
@Override @Override
public ChunkBatch createChunkBatch(@NotNull Chunk chunk) { public ChunkBatch createChunkBatch(@NotNull Chunk chunk) {
Check.notNull(chunk, "The chunk of a ChunkBatch cannot be null");
return new ChunkBatch(this, chunk, false); return new ChunkBatch(this, chunk, false);
} }
@ -587,7 +586,6 @@ public class InstanceContainer extends Instance {
* @throws NullPointerException if {@code chunkSupplier} is null * @throws NullPointerException if {@code chunkSupplier} is null
*/ */
public void setChunkSupplier(@NotNull ChunkSupplier chunkSupplier) { public void setChunkSupplier(@NotNull ChunkSupplier chunkSupplier) {
Check.notNull(chunkSupplier, "The chunk supplier cannot be null!");
this.chunkSupplier = chunkSupplier; this.chunkSupplier = chunkSupplier;
} }

View File

@ -322,12 +322,12 @@ public class InventoryClickProcessor {
// End right // End right
if (!rightDraggingMap.containsKey(player)) if (!rightDraggingMap.containsKey(player))
return null; return null;
final Set<Integer> slots = rightDraggingMap.get(player); final IntSet slots = rightDraggingMap.get(player);
final int size = slots.size(); final int size = slots.size();
int cursorAmount = stackingRule.getAmount(cursor); int cursorAmount = stackingRule.getAmount(cursor);
if (size > cursorAmount) if (size > cursorAmount)
return null; return null;
for (Integer s : slots) { for (int s : slots) {
ItemStack draggedItem = cursor.clone(); ItemStack draggedItem = cursor.clone();
ItemStack slotItem = itemGetter.apply(s); ItemStack slotItem = itemGetter.apply(s);

View File

@ -117,7 +117,6 @@ public class ItemStack implements DataContainer, PublicCloneable<ItemStack> {
* @throws NullPointerException if {@code defaultStackingRule} is null * @throws NullPointerException if {@code defaultStackingRule} is null
*/ */
public static void setDefaultStackingRule(@NotNull StackingRule defaultStackingRule) { public static void setDefaultStackingRule(@NotNull StackingRule defaultStackingRule) {
Check.notNull(defaultStackingRule, "StackingRule cannot be null!");
ItemStack.defaultStackingRule = defaultStackingRule; ItemStack.defaultStackingRule = defaultStackingRule;
} }
@ -635,7 +634,6 @@ public class ItemStack implements DataContainer, PublicCloneable<ItemStack> {
* @throws NullPointerException if {@code stackingRule} is null * @throws NullPointerException if {@code stackingRule} is null
*/ */
public void setStackingRule(@NotNull StackingRule stackingRule) { public void setStackingRule(@NotNull StackingRule stackingRule) {
Check.notNull(stackingRule, "The stacking rule cannot be null!");
this.stackingRule = stackingRule; this.stackingRule = stackingRule;
} }

View File

@ -4,6 +4,7 @@ import com.google.gson.Gson;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import it.unimi.dsi.fastutil.io.FastBufferedInputStream;
import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -169,7 +170,7 @@ public class ResourceGatherer {
private static File download(@NotNull String version, @NotNull String url, @NotNull String sha1Source) throws IOException { private static File download(@NotNull String version, @NotNull String url, @NotNull String sha1Source) throws IOException {
File target = new File(TMP_FOLDER, "server_" + version + ".jar"); File target = new File(TMP_FOLDER, "server_" + version + ".jar");
// Download // Download
try (BufferedInputStream in = new BufferedInputStream(new URL(url).openStream())) { try (FastBufferedInputStream in = new FastBufferedInputStream(new URL(url).openStream())) {
Files.copy(in, target.toPath(), StandardCopyOption.REPLACE_EXISTING); Files.copy(in, target.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) { } catch (IOException e) {
throw new IOException("Failed to download Minecraft server jar.", e); throw new IOException("Failed to download Minecraft server jar.", e);

View File

@ -14,7 +14,6 @@ public class ResourcePack {
private final String hash; private final String hash;
public ResourcePack(@NotNull String url, @Nullable String hash) { public ResourcePack(@NotNull String url, @Nullable String hash) {
Check.notNull(url, "The resource pack url cannot be null");
this.url = url; this.url = url;
// Optional, set to empty if null // Optional, set to empty if null
this.hash = hash == null ? "" : hash; this.hash = hash == null ? "" : hash;

View File

@ -36,7 +36,6 @@ public final class StorageManager {
* @return the specified {@link StorageLocation} * @return the specified {@link StorageLocation}
*/ */
public StorageLocation getLocation(@NotNull String location, @NotNull StorageOptions storageOptions, @NotNull StorageSystem storageSystem) { public StorageLocation getLocation(@NotNull String location, @NotNull StorageOptions storageOptions, @NotNull StorageSystem storageSystem) {
Check.notNull(storageOptions, "The storage option cannot be null");
return locationMap.computeIfAbsent(location, return locationMap.computeIfAbsent(location,
s -> new StorageLocation(storageSystem, location, storageOptions)); s -> new StorageLocation(storageSystem, location, storageOptions));
} }