1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-19 19:25:48 +00:00

Performance improvements for cargo (when ChestTerminals is installed)

This commit is contained in:
TheBusyBiscuit 2020-07-16 22:41:36 +02:00
parent a42c62dd04
commit c78e3c3959
7 changed files with 55 additions and 15 deletions

View File

@ -25,6 +25,8 @@
#### Changes
* Refactored and reworked the Generator API
* Small performance improvements to Energy networks
* Big performance improvements to Cargo networks when using ChestTerminal
* Slight changes to /sf timings
## Release Candidate 14 (12 Jul 2020)

View File

@ -96,10 +96,9 @@ abstract class ChestTerminalNetwork extends Network {
Iterator<ItemRequest> iterator = itemRequests.iterator();
while (iterator.hasNext()) {
ItemRequest request = iterator.next();
BlockMenu menu = BlockStorage.getInventory(request.getTerminal());
if (terminals.contains(request.getTerminal()) || imports.contains(request.getTerminal()) || exports.contains(request.getTerminal())) {
BlockMenu menu = BlockStorage.getInventory(request.getTerminal());
if (menu != null) {
switch (request.getDirection()) {
case INSERT:
distributeInsertionRequest(inventories, request, menu, iterator, destinations);
@ -281,6 +280,12 @@ abstract class ChestTerminalNetwork extends Network {
* A {@link Set} of providers to this {@link ChestTerminalNetwork}
*/
protected void updateTerminals(Set<Location> providers) {
if (terminals.isEmpty()) {
// Performance improvement - We don't need to compute items for
// Cargo networks without any Chest Terminals
return;
}
List<ItemStackAndInteger> items = findAvailableItems(providers);
for (Location l : terminals) {
@ -396,7 +401,7 @@ abstract class ChestTerminalNetwork extends Network {
boolean add = true;
for (ItemStackAndInteger item : items) {
if (SlimefunUtils.isItemSimilar(is, item.getItem(), true)) {
if (SlimefunUtils.isItemSimilar(is, item.getItemStackWrapper(), true, false)) {
add = false;
item.add(is.getAmount() + stored);
}
@ -420,7 +425,7 @@ abstract class ChestTerminalNetwork extends Network {
boolean add = true;
for (ItemStackAndInteger item : items) {
if (SlimefunUtils.isItemSimilar(stack, item.getItem(), true)) {
if (SlimefunUtils.isItemSimilar(stack, item.getItemStackWrapper(), true)) {
add = false;
item.add(stack.getAmount());
}

View File

@ -2,9 +2,12 @@ package io.github.thebusybiscuit.slimefun4.core.networks.cargo;
import org.bukkit.inventory.ItemStack;
import io.github.thebusybiscuit.slimefun4.utils.itemstack.ItemStackWrapper;
class ItemStackAndInteger {
private final ItemStack item;
private ItemStackWrapper wrapper;
private int number;
ItemStackAndInteger(ItemStack item, int amount) {
@ -20,6 +23,14 @@ class ItemStackAndInteger {
return item;
}
public ItemStackWrapper getItemStackWrapper() {
if (wrapper == null && item != null) {
wrapper = new ItemStackWrapper(item);
}
return wrapper;
}
public void add(int amount) {
number += amount;
}

View File

@ -23,15 +23,16 @@ import net.md_5.bungee.api.chat.hover.content.Text;
class PerformanceSummary {
// The threshold at which a Block or Chunk is significant enough to appear in /sf timings
private static final int VISIBILITY_THRESHOLD = 280_000;
private static final int MIN_ITEMS = 3;
private static final int MAX_ITEMS = 10;
private static final int VISIBILITY_THRESHOLD = 300_000;
private static final int MIN_ITEMS = 4;
private static final int MAX_ITEMS = 12;
private final SlimefunProfiler profiler;
private final PerformanceRating rating;
private final long totalElapsedTime;
private final int totalTickedBlocks;
private final float percentage;
private final int tickRate;
private final Map<String, Long> chunks;
private final Map<String, Long> plugins;
@ -43,6 +44,7 @@ class PerformanceSummary {
this.percentage = profiler.getPercentageOfTick();
this.totalElapsedTime = totalElapsedTime;
this.totalTickedBlocks = totalTickedBlocks;
this.tickRate = profiler.getTickRate();
chunks = profiler.getByChunk();
plugins = profiler.getByPlugin();
@ -52,7 +54,8 @@ class PerformanceSummary {
public void send(CommandSender sender) {
sender.sendMessage("");
sender.sendMessage(ChatColor.GREEN + "===== Slimefun Lag Profiler =====");
sender.sendMessage(ChatColor.GOLD + "Total: " + ChatColor.YELLOW + NumberUtils.getAsMillis(totalElapsedTime));
sender.sendMessage(ChatColor.GOLD + "Total time: " + ChatColor.YELLOW + NumberUtils.getAsMillis(totalElapsedTime));
sender.sendMessage(ChatColor.GOLD + "Running every: " + ChatColor.YELLOW + NumberUtils.roundDecimalNumber(tickRate / 20.0) + "s (" + tickRate + " ticks)");
sender.sendMessage(ChatColor.GOLD + "Performance: " + getPerformanceRating());
sender.sendMessage("");

View File

@ -1,5 +1,6 @@
package io.github.thebusybiscuit.slimefun4.core.services.profiler;
import org.bukkit.Location;
import org.bukkit.block.Block;
import io.github.thebusybiscuit.cscorelib2.blocks.BlockPosition;
@ -11,6 +12,11 @@ class ProfiledBlock {
private final BlockPosition position;
private final SlimefunItem item;
ProfiledBlock(Location l, SlimefunItem item) {
this.position = new BlockPosition(l);
this.item = item;
}
ProfiledBlock(BlockPosition position, SlimefunItem item) {
this.position = position;
this.item = item;

View File

@ -18,7 +18,6 @@ import org.bukkit.Server;
import org.bukkit.block.Block;
import org.bukkit.command.CommandSender;
import io.github.thebusybiscuit.cscorelib2.blocks.BlockPosition;
import io.github.thebusybiscuit.slimefun4.api.SlimefunAddon;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import io.github.thebusybiscuit.slimefun4.implementation.tasks.TickerTask;
@ -80,8 +79,10 @@ public class SlimefunProfiler {
* Be careful to {@link #closeEntry(Location, SlimefunItem, long)} all of them again!
* No {@link PerformanceSummary} will be sent until all entires were closed.
*
* If the specified amount is negative, scheduled entries will be removed
*
* @param amount
* The amount of entries that should be scheduled.
* The amount of entries that should be scheduled. Can be negative
*/
public void scheduleEntries(int amount) {
if (running.get()) {
@ -113,8 +114,9 @@ public class SlimefunProfiler {
long elapsedTime = System.nanoTime() - timestamp;
executor.execute(() -> {
ProfiledBlock block = new ProfiledBlock(new BlockPosition(l), item);
timings.put(block, elapsedTime);
ProfiledBlock block = new ProfiledBlock(l, item);
timings.putIfAbsent(block, elapsedTime);
queued.decrementAndGet();
});
@ -136,7 +138,7 @@ public class SlimefunProfiler {
executor.execute(() -> {
// Wait for all timing results to come in
while (queued.get() > 0 && !running.get()) {
while (!running.get() && queued.get() > 0) {
// Ideally we would wait some time here but the ticker task may be faster
// than 1ms, so it would halt this summary for up to 7 minutes
// Not perfect performance-wise but this is a seperate Thread anyway
@ -276,6 +278,10 @@ public class SlimefunProfiler {
return NumberUtils.getAsMillis(totalElapsedTime);
}
public int getTickRate() {
return SlimefunPlugin.getTickerTask().getTickRate();
}
/**
* This method checks whether the {@link SlimefunProfiler} has collected timings on
* the given {@link Block}

View File

@ -32,6 +32,7 @@ public class TickerTask implements Runnable {
private final Map<Location, Boolean> deletionQueue = new ConcurrentHashMap<>();
private final Map<BlockPosition, Integer> bugs = new ConcurrentHashMap<>();
private int tickRate;
private boolean halted = false;
private boolean running = false;
@ -179,6 +180,8 @@ public class TickerTask implements Runnable {
}
public void start(SlimefunPlugin plugin) {
this.tickRate = SlimefunPlugin.getCfg().getInt("URID.custom-ticker-delay");
plugin.getServer().getScheduler().runTaskTimerAsynchronously(plugin, () -> {
try {
run();
@ -187,7 +190,11 @@ public class TickerTask implements Runnable {
plugin.getLogger().log(Level.SEVERE, x, () -> "An Exception was caught while ticking the Block Tickers Task for Slimefun v" + SlimefunPlugin.getVersion());
abortTick();
}
}, 100L, SlimefunPlugin.getCfg().getInt("URID.custom-ticker-delay"));
}, 100L, tickRate);
}
public int getTickRate() {
return tickRate;
}
}