Improve performance

This commit is contained in:
TheMode 2021-04-17 03:00:34 +02:00
parent 3b7353300d
commit a5df2376cf
3 changed files with 11 additions and 27 deletions

View File

@ -325,7 +325,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
packet.process(this); packet.process(this);
} }
if (username.equals("TheMode911")) //if (username.equals("TheMode911"))
for (Player p1 : MinecraftServer.getConnectionManager().getOnlinePlayers()) { for (Player p1 : MinecraftServer.getConnectionManager().getOnlinePlayers()) {
p1.getAcquiredElement().acquire(o -> { p1.getAcquiredElement().acquire(o -> {
//for (Player p2 : MinecraftServer.getConnectionManager().getOnlinePlayers()) //for (Player p2 : MinecraftServer.getConnectionManager().getOnlinePlayers())

View File

@ -38,17 +38,11 @@ public interface Acquirable<T> {
* and execute {@code consumer} as a callback with the acquired object. * and execute {@code consumer} as a callback with the acquired object.
* *
* @param consumer the consumer of the acquired object * @param consumer the consumer of the acquired object
* @return true if the acquisition happened without synchronization, false otherwise
*/ */
default boolean acquire(@NotNull Consumer<T> consumer) { default void acquire(@NotNull Consumer<@NotNull T> consumer) {
final Thread currentThread = Thread.currentThread(); final Thread currentThread = Thread.currentThread();
final BatchThread elementThread = getHandler().getBatchThread();
final Handler handler = getHandler();
final BatchThread elementThread = handler.getBatchThread();
Acquisition.acquire(currentThread, elementThread, () -> consumer.accept(unwrap())); Acquisition.acquire(currentThread, elementThread, () -> consumer.accept(unwrap()));
return true;
} }
/** /**

View File

@ -68,7 +68,8 @@ public final class Acquisition {
if (elementThread == null || elementThread == currentThread) { if (elementThread == null || elementThread == currentThread) {
callback.run(); callback.run();
} else { } else {
final Monitor currentMonitor = currentThread instanceof BatchThread ? ((BatchThread) currentThread).monitor : null; final BatchThread currentBatch = currentThread instanceof BatchThread ? ((BatchThread) currentThread) : null;
final Monitor currentMonitor = currentBatch != null ? currentBatch.monitor : null;
boolean enter = false; boolean enter = false;
if (currentMonitor != null && currentMonitor.isOccupiedByCurrentThread()) { if (currentMonitor != null && currentMonitor.isOccupiedByCurrentThread()) {
@ -79,30 +80,20 @@ public final class Acquisition {
Monitor monitor = elementThread.monitor; Monitor monitor = elementThread.monitor;
//System.out.println("acq " + System.currentTimeMillis() + " " + currentThread); if (monitor.isOccupiedByCurrentThread() || GLOBAL_MONITOR.isOccupiedByCurrentThread()) {
if (monitor.isOccupiedByCurrentThread()) { // We already have access to the thread
//System.out.println("already");
callback.run();
process(elementThread);
} else if (GLOBAL_MONITOR.isOccupiedByCurrentThread()) {
callback.run(); callback.run();
} else if (monitor.tryEnter()) { } else if (monitor.tryEnter()) {
//System.out.println("enter"); // Acquire the thread
callback.run(); callback.run();
process(elementThread);
monitor.leave(); monitor.leave();
} else { } else {
// Thread is not available, forward request // Thread is not available, forward request to be executed later
final BatchThread currentBatch = (BatchThread) currentThread;
while (!GLOBAL_MONITOR.tryEnter()) while (!GLOBAL_MONITOR.tryEnter())
processMonitored(currentBatch); processMonitored(currentBatch);
//System.out.println("yes " + elementThread + " " + elementThread.getMainRunnable().isInTick());
var requests = getRequests(elementThread);
var requests = getRequests(elementThread);
Acquirable.Request request = new Acquirable.Request(); Acquirable.Request request = new Acquirable.Request();
request.localLatch = new CountDownLatch(1); request.localLatch = new CountDownLatch(1);
request.processLatch = new CountDownLatch(1); request.processLatch = new CountDownLatch(1);
@ -110,13 +101,11 @@ public final class Acquisition {
try { try {
currentBatch.waitingOn = elementThread; currentBatch.waitingOn = elementThread;
processMonitored(currentBatch);
request.localLatch.await(); request.localLatch.await();
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
currentBatch.waitingOn = null; currentBatch.waitingOn = null;
//System.out.println("end wait");
callback.run(); callback.run();
request.processLatch.countDown(); request.processLatch.countDown();
@ -132,6 +121,7 @@ public final class Acquisition {
public static void process(@NotNull BatchThread thread) { public static void process(@NotNull BatchThread thread) {
var requests = getRequests(thread); var requests = getRequests(thread);
requests.forEach(request -> { requests.forEach(request -> {
requests.remove(request);
request.localLatch.countDown(); request.localLatch.countDown();
try { try {
request.processLatch.await(); request.processLatch.await();