Fix incremental thread refresh

This commit is contained in:
TheMode 2021-04-18 11:53:38 +02:00
parent 92f30070db
commit 1dbd3078c3
3 changed files with 26 additions and 13 deletions

View File

@ -126,7 +126,8 @@ public final class UpdateManager {
}
// Clear removed entities & update threads
this.threadProvider.refreshThreads();
long tickTime = System.currentTimeMillis() - tickStart;
this.threadProvider.refreshThreads(tickTime);
}
/**

View File

@ -327,20 +327,20 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
}
//System.out.println(getAcquiredElement().getHandler().getBatchThread());
Collection<Acquirable<Player>> players = new ArrayList<>();
//if (username.equals("TheMode911"))
/*Collection<Acquirable<Player>> players = new ArrayList<>();
if (username.equals("TheMode911"))
for (Player p1 : MinecraftServer.getConnectionManager().getOnlinePlayers()) {
//players.add(p1.getAcquiredElement());
p1.getAcquiredElement().acquire(o -> {
//System.out.println(getUsername()+": "+o+" "+getAliveTicks());
/*for (Player p2 : MinecraftServer.getConnectionManager().getOnlinePlayers())
for (Player p2 : MinecraftServer.getConnectionManager().getOnlinePlayers())
p2.getAcquiredElement().acquire(o2 -> {
//System.out.println(getAcquiredElement().getHandler().getBatchThread().monitor.isOccupiedByCurrentThread());
});*/
});
});
}
/*AcquirableCollection<Player> players1 = new AcquirableCollection<>(players);
AcquirableCollection<Player> players1 = new AcquirableCollection<>(players);
players1.forEach(player -> {
players1.forEach(player2 -> {

View File

@ -1,6 +1,5 @@
package net.minestom.server.thread;
import net.minestom.server.MinecraftServer;
import net.minestom.server.entity.Entity;
import net.minestom.server.instance.Chunk;
import net.minestom.server.instance.Instance;
@ -32,7 +31,9 @@ public abstract class ThreadProvider {
// Represents the maximum percentage of tick time
// that can be spent refreshing chunks thread
protected float refreshPercentage = 0.3f;
protected double refreshPercentage = 0.3f;
// Minimum refresh time
private long min = 3;
public ThreadProvider(int threadCount) {
this.threads = new ArrayList<>(threadCount);
@ -92,7 +93,7 @@ public abstract class ThreadProvider {
protected @NotNull ChunkEntry setChunkThread(@NotNull Chunk chunk,
@NotNull Function<@NotNull BatchThread, @NotNull ChunkEntry> chunkEntrySupplier) {
final int threadId = (int) (Math.abs(findThread(chunk)) % threads.size());
final int threadId = getThreadId(chunk);
BatchThread thread = threads.get(threadId);
var chunks = threadChunkMap.computeIfAbsent(thread, batchThread -> ConcurrentHashMap.newKeySet());
@ -114,6 +115,10 @@ public abstract class ThreadProvider {
this.chunks.remove(chunk);
}
protected int getThreadId(Chunk chunk) {
return (int) (Math.abs(findThread(chunk)) % threads.size());
}
/**
* Prepares the update.
*
@ -152,7 +157,7 @@ public abstract class ThreadProvider {
return countDownLatch;
}
public synchronized void refreshThreads() {
public synchronized void refreshThreads(long tickTime) {
// Clear removed entities
{
for (Entity entity : removedEntities) {
@ -167,11 +172,11 @@ public abstract class ThreadProvider {
}
final long endTime = (long) (System.currentTimeMillis() + ((float) MinecraftServer.TICK_MS * refreshPercentage));
final long endTime = (System.currentTimeMillis() +
Math.max((long) ((double) tickTime * refreshPercentage), min));
final int size = chunks.size();
int counter = 0;
while (counter++ < size ||
System.currentTimeMillis() < endTime) {
while (true) {
Chunk chunk = chunks.pollFirst();
if (!ChunkUtils.isLoaded(chunk)) {
removeChunk(chunk);
@ -196,6 +201,13 @@ public abstract class ThreadProvider {
// Add back to the deque
chunks.addLast(chunk);
if (++counter > size)
break;
if (System.currentTimeMillis() >= endTime)
break;
}
System.out.println("update " + counter);
}