Skip to content

Commit 00f8556

Browse files
authored
Performance improvements (#43)
* Fix locking of the main thread * Reduce calls to get time * Fix repair manager
1 parent f010b0a commit 00f8556

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

src/main/java/net/countercraft/movecraft/repair/RepairManager.java

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package net.countercraft.movecraft.repair;
22

3-
import java.util.HashSet;
4-
import java.util.Queue;
5-
import java.util.Set;
3+
import java.util.*;
64
import java.util.concurrent.ConcurrentLinkedQueue;
75

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

2321
Set<Repair> completed = new HashSet<>();
24-
Set<Repair> executed = new HashSet<>();
25-
while (System.currentTimeMillis() - start < Config.RepairMaxTickTime) {
26-
Repair repair = repairs.peek();
22+
List<Repair> executed = new ArrayList<>(repairs.size());
23+
List<Repair> waiting = new ArrayList<>(repairs.size());
24+
long time = System.currentTimeMillis();
25+
while (time - start < Config.RepairMaxTickTime) {
26+
Repair repair = repairs.poll();
2727
if (repair == null)
2828
break; // No repairs, jump out
2929

30-
if (repair.run()) {
31-
// Repair placed at least a block, return to back of queue
32-
executed.add(repairs.poll());
33-
} // Else leave at top of queue
30+
if (repair.run(time)) {
31+
// Repair placed at least a block, put it back at the end
32+
executed.add(repair);
33+
} else {
34+
// Put back at the top of the queue
35+
waiting.add(repair);
36+
}
3437

35-
if (repair.isDone())
38+
if (repair.isDone()) {
3639
completed.add(repair);
40+
}
41+
42+
time = System.currentTimeMillis();
3743
}
44+
repairs.addAll(waiting);
3845
repairs.addAll(executed);
3946

4047
for (Repair repair : completed) {

src/main/java/net/countercraft/movecraft/repair/types/Repair.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ public int remaining() {
5252
return queue.size();
5353
}
5454

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

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

6969
if (placedBlocks > 0) {
70-
lastExecution = System.currentTimeMillis();
70+
lastExecution = time;
7171
return true;
7272
}
7373

0 commit comments

Comments
 (0)