Skip to content

Commit

Permalink
Performance improvements (#43)
Browse files Browse the repository at this point in the history
* Fix locking of the main thread

* Reduce calls to get time

* Fix repair manager
  • Loading branch information
TylerS1066 authored Jul 16, 2024
1 parent f010b0a commit 00f8556
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
29 changes: 18 additions & 11 deletions src/main/java/net/countercraft/movecraft/repair/RepairManager.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package net.countercraft.movecraft.repair;

import java.util.HashSet;
import java.util.Queue;
import java.util.Set;
import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue;

import org.bukkit.Bukkit;
Expand All @@ -21,20 +19,29 @@ public void run() {
long start = System.currentTimeMillis();

Set<Repair> completed = new HashSet<>();
Set<Repair> executed = new HashSet<>();
while (System.currentTimeMillis() - start < Config.RepairMaxTickTime) {
Repair repair = repairs.peek();
List<Repair> executed = new ArrayList<>(repairs.size());
List<Repair> waiting = new ArrayList<>(repairs.size());
long time = System.currentTimeMillis();
while (time - start < Config.RepairMaxTickTime) {
Repair repair = repairs.poll();
if (repair == null)
break; // No repairs, jump out

if (repair.run()) {
// Repair placed at least a block, return to back of queue
executed.add(repairs.poll());
} // Else leave at top of queue
if (repair.run(time)) {
// Repair placed at least a block, put it back at the end
executed.add(repair);
} else {
// Put back at the top of the queue
waiting.add(repair);
}

if (repair.isDone())
if (repair.isDone()) {
completed.add(repair);
}

time = System.currentTimeMillis();
}
repairs.addAll(waiting);
repairs.addAll(executed);

for (Repair repair : completed) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public int remaining() {
return queue.size();
}

public boolean run() {
double elapsedTicks = (System.currentTimeMillis() - lastExecution) * 20.0 / 1000;
public boolean run(long time) {
double elapsedTicks = (time - lastExecution) * 20.0 / 1000;
int placedBlocks = 0;

while (elapsedTicks > Config.RepairTicksPerBlock && placedBlocks <= Config.RepairMaxBlocksPerTick) {
Expand All @@ -67,7 +67,7 @@ public boolean run() {
}

if (placedBlocks > 0) {
lastExecution = System.currentTimeMillis();
lastExecution = time;
return true;
}

Expand Down

0 comments on commit 00f8556

Please sign in to comment.