Improve async task handler (#203)

This commit is contained in:
ishland 2020-09-20 19:55:56 +03:00 committed by Ivan Pekov
parent d25ccc3f27
commit e65858c78a
No known key found for this signature in database
GPG Key ID: BC975C392D9CA3A3
2 changed files with 83 additions and 0 deletions

View File

@ -65,6 +65,7 @@ # Patches
| server | Implement bed explosion options | William Blake Galbreath | |
| server | Implement respawn anchor explosion options | William Blake Galbreath | |
| server | Improve Hopper Performance | Aikar | |
| server | Improve async task handler | ishland | |
| server | Item stuck sleep config | tr7zw | |
| api | Kill AnnotationTest | tr7zw | |
| server | Lagging threshold | William Blake Galbreath | |

View File

@ -0,0 +1,82 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: ishland <ishlandmc@yeah.net>
Date: Sun, 20 Sep 2020 19:43:03 +0300
Subject: [PATCH] Improve async task handler
Replace ConcurrentLinkedQueue and LockSupport hacks to reduce execute times
diff --git a/src/main/java/net/minecraft/server/IAsyncTaskHandler.java b/src/main/java/net/minecraft/server/IAsyncTaskHandler.java
index 5df6be7e8d9b1295ed0700b3be90c3778fc7d77c..95aec7d4705a7a275fe7f5d6aec877dd7ad0f529 100644
--- a/src/main/java/net/minecraft/server/IAsyncTaskHandler.java
+++ b/src/main/java/net/minecraft/server/IAsyncTaskHandler.java
@@ -13,8 +13,9 @@ public abstract class IAsyncTaskHandler<R extends Runnable> implements Mailbox<R
private final String b;
private static final Logger LOGGER = LogManager.getLogger();
- private final Queue<R> d = Queues.newConcurrentLinkedQueue();
+ private final java.util.concurrent.LinkedBlockingDeque<R> d = new java.util.concurrent.LinkedBlockingDeque<>(); // Yatopia
private int e;
+ private R next = null; // Yatopia
protected IAsyncTaskHandler(String s) {
this.b = s;
@@ -79,7 +80,7 @@ public abstract class IAsyncTaskHandler<R extends Runnable> implements Mailbox<R
public final void addTask(R r0) { a(r0); }; // Paper - OBFHELPER
public void a(R r0) {
this.d.add(r0);
- LockSupport.unpark(this.getThread());
+ //LockSupport.unpark(this.getThread()); // Yatopia - replaced by LinkedBlockingQueue
}
public void execute(Runnable runnable) {
@@ -92,21 +93,30 @@ public abstract class IAsyncTaskHandler<R extends Runnable> implements Mailbox<R
}
public void executeAll() { // Paper - protected -> public
+ /* // Yatopia start - replaced logic
while (this.executeNext()) {
;
}
+ */
+ while (!d.isEmpty()) {
+ if (next == null) queuePull();
+ if (!executeNext()) break;
+ }
+ // Yatopia end
}
protected boolean executeNext() {
- R r0 = this.d.peek(); // Paper - decompile fix
+ R r0 = next; // Paper - decompile fix // Yatopia - temp storage
+ if (next == null || !d.isEmpty()) queuePull(); // Yatopia - attempt to get from queue
if (r0 == null) {
return false;
} else if (this.e == 0 && !this.canExecute(r0)) {
return false;
} else {
- this.executeTask(this.d.remove()); // Paper - decompile fix
+ this.executeTask(r0); // Paper - decompile fix // Yatopia - replaced with r0
+ next = null; // Yatopia - clear temp for next task
return true;
}
}
@@ -126,9 +136,17 @@ public abstract class IAsyncTaskHandler<R extends Runnable> implements Mailbox<R
}
+ protected final void queuePull() { bl(); } // Yatopia - OBFHELPER
protected void bl() {
+ /* // Yatopia start - replaced logic
Thread.yield();
LockSupport.parkNanos("waiting for tasks", 100000L);
+ */
+ next = null;
+ try {
+ next = d.poll(100, java.util.concurrent.TimeUnit.MICROSECONDS);
+ } catch (InterruptedException ignored) {}
+ // Yatopia end
}
protected void executeTask(R r0) {