Rewrite chunk system (#8177)
Patch documentation to come Issues with the old system that are fixed now: - World generation does not scale with cpu cores effectively. - Relies on the main thread for scheduling and maintaining chunk state, dropping chunk load/generate rates at lower tps. - Unreliable prioritisation of chunk gen/load calls that block the main thread. - Shutdown logic is utterly unreliable, as it has to wait for all chunks to unload - is it guaranteed that the chunk system is in a state on shutdown that it can reliably do this? Watchdog shutdown also typically failed due to thread checks, which is now resolved. - Saving of data is not unified (i.e can save chunk data without saving entity data, poses problems for desync if shutdown is really abnormal. - Entities are not loaded with chunks. This caused quite a bit of headache for Chunk#getEntities API, but now the new chunk system loads entities with chunks so that they are ready whenever the chunk loads in. Effectively brings the behavior back to 1.16 era, but still storing entities in their own separate regionfiles. The above list is not complete. The patch documentation will complete it. New chunk system hard relies on starlight and dataconverter, and most importantly the new concurrent utilities in ConcurrentUtil. Some of the old async chunk i/o interface (i.e the old file io thread reroutes _some_ calls to the new file io thread) is kept for plugin compat reasons. It will be removed in the next major version of minecraft. The old legacy chunk system patches have been moved to the removed folder in case we need them again.
This commit is contained in:
parent
abe53a7eb4
commit
01a13871de
942 changed files with 20131 additions and 2697 deletions
|
@ -1412,6 +1412,160 @@ index 0000000000000000000000000000000000000000..f4415f782b32fed25da98e44b172f717
|
|||
+ }
|
||||
+ }
|
||||
+}
|
||||
diff --git a/src/main/java/ca/spottedleaf/concurrentutil/collection/SRSWLinkedQueue.java b/src/main/java/ca/spottedleaf/concurrentutil/collection/SRSWLinkedQueue.java
|
||||
new file mode 100644
|
||||
index 0000000000000000000000000000000000000000..597659f38aa816646dcda4ca39c002b6d9f9a792
|
||||
--- /dev/null
|
||||
+++ b/src/main/java/ca/spottedleaf/concurrentutil/collection/SRSWLinkedQueue.java
|
||||
@@ -0,0 +1,148 @@
|
||||
+package ca.spottedleaf.concurrentutil.collection;
|
||||
+
|
||||
+import ca.spottedleaf.concurrentutil.util.ConcurrentUtil;
|
||||
+import ca.spottedleaf.concurrentutil.util.Validate;
|
||||
+import java.lang.invoke.VarHandle;
|
||||
+import java.util.ConcurrentModificationException;
|
||||
+
|
||||
+/**
|
||||
+ * Single reader thread single writer thread queue. The reader side of the queue is ordered by acquire semantics,
|
||||
+ * and the writer side of the queue is ordered by release semantics.
|
||||
+ */
|
||||
+// TODO test
|
||||
+public class SRSWLinkedQueue<E> {
|
||||
+
|
||||
+ // always non-null
|
||||
+ protected LinkedNode<E> head;
|
||||
+
|
||||
+ // always non-null
|
||||
+ protected LinkedNode<E> tail;
|
||||
+
|
||||
+ /* IMPL NOTE: Leave hashCode and equals to their defaults */
|
||||
+
|
||||
+ public SRSWLinkedQueue() {
|
||||
+ final LinkedNode<E> dummy = new LinkedNode<>(null, null);
|
||||
+ this.head = this.tail = dummy;
|
||||
+ }
|
||||
+
|
||||
+ /**
|
||||
+ * Must be the reader thread.
|
||||
+ *
|
||||
+ * <p>
|
||||
+ * Returns, without removing, the first element of this queue.
|
||||
+ * </p>
|
||||
+ * @return Returns, without removing, the first element of this queue.
|
||||
+ */
|
||||
+ public E peekFirst() {
|
||||
+ LinkedNode<E> head = this.head;
|
||||
+ E ret = head.getElementPlain();
|
||||
+ if (ret == null) {
|
||||
+ head = head.getNextAcquire();
|
||||
+ if (head == null) {
|
||||
+ // empty
|
||||
+ return null;
|
||||
+ }
|
||||
+ // update head reference for next poll() call
|
||||
+ this.head = head;
|
||||
+ // guaranteed to be non-null
|
||||
+ ret = head.getElementPlain();
|
||||
+ if (ret == null) {
|
||||
+ throw new ConcurrentModificationException("Multiple reader threads");
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ return ret;
|
||||
+ }
|
||||
+
|
||||
+ /**
|
||||
+ * Must be the reader thread.
|
||||
+ *
|
||||
+ * <p>
|
||||
+ * Returns and removes the first element of this queue.
|
||||
+ * </p>
|
||||
+ * @return Returns and removes the first element of this queue.
|
||||
+ */
|
||||
+ public E poll() {
|
||||
+ LinkedNode<E> head = this.head;
|
||||
+ E ret = head.getElementPlain();
|
||||
+ if (ret == null) {
|
||||
+ head = head.getNextAcquire();
|
||||
+ if (head == null) {
|
||||
+ // empty
|
||||
+ return null;
|
||||
+ }
|
||||
+ // guaranteed to be non-null
|
||||
+ ret = head.getElementPlain();
|
||||
+ if (ret == null) {
|
||||
+ throw new ConcurrentModificationException("Multiple reader threads");
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ head.setElementPlain(null);
|
||||
+ LinkedNode<E> next = head.getNextAcquire();
|
||||
+ this.head = next == null ? head : next;
|
||||
+
|
||||
+ return ret;
|
||||
+ }
|
||||
+
|
||||
+ /**
|
||||
+ * Must be the writer thread.
|
||||
+ *
|
||||
+ * <p>
|
||||
+ * Adds the element to the end of the queue.
|
||||
+ * </p>
|
||||
+ *
|
||||
+ * @throws NullPointerException If the provided element is null
|
||||
+ */
|
||||
+ public void addLast(final E element) {
|
||||
+ Validate.notNull(element, "Provided element cannot be null");
|
||||
+ final LinkedNode<E> append = new LinkedNode<>(element, null);
|
||||
+
|
||||
+ this.tail.setNextRelease(append);
|
||||
+ this.tail = append;
|
||||
+ }
|
||||
+
|
||||
+ protected static final class LinkedNode<E> {
|
||||
+
|
||||
+ protected volatile Object element;
|
||||
+ protected volatile LinkedNode<E> next;
|
||||
+
|
||||
+ protected static final VarHandle ELEMENT_HANDLE = ConcurrentUtil.getVarHandle(LinkedNode.class, "element", Object.class);
|
||||
+ protected static final VarHandle NEXT_HANDLE = ConcurrentUtil.getVarHandle(LinkedNode.class, "next", LinkedNode.class);
|
||||
+
|
||||
+ protected LinkedNode(final Object element, final LinkedNode<E> next) {
|
||||
+ ELEMENT_HANDLE.set(this, element);
|
||||
+ NEXT_HANDLE.set(this, next);
|
||||
+ }
|
||||
+
|
||||
+ /* element */
|
||||
+
|
||||
+ @SuppressWarnings("unchecked")
|
||||
+ protected final E getElementPlain() {
|
||||
+ return (E)ELEMENT_HANDLE.get(this);
|
||||
+ }
|
||||
+
|
||||
+ protected final void setElementPlain(final E update) {
|
||||
+ ELEMENT_HANDLE.set(this, (Object)update);
|
||||
+ }
|
||||
+ /* next */
|
||||
+
|
||||
+ @SuppressWarnings("unchecked")
|
||||
+ protected final LinkedNode<E> getNextPlain() {
|
||||
+ return (LinkedNode<E>)NEXT_HANDLE.get(this);
|
||||
+ }
|
||||
+
|
||||
+ @SuppressWarnings("unchecked")
|
||||
+ protected final LinkedNode<E> getNextAcquire() {
|
||||
+ return (LinkedNode<E>)NEXT_HANDLE.getAcquire(this);
|
||||
+ }
|
||||
+
|
||||
+ protected final void setNextPlain(final LinkedNode<E> next) {
|
||||
+ NEXT_HANDLE.set(this, next);
|
||||
+ }
|
||||
+
|
||||
+ protected final void setNextRelease(final LinkedNode<E> next) {
|
||||
+ NEXT_HANDLE.setRelease(this, next);
|
||||
+ }
|
||||
+ }
|
||||
+}
|
||||
diff --git a/src/main/java/ca/spottedleaf/concurrentutil/completable/Completable.java b/src/main/java/ca/spottedleaf/concurrentutil/completable/Completable.java
|
||||
new file mode 100644
|
||||
index 0000000000000000000000000000000000000000..a1ad3308f9c3545a604b635896259a1cd3382b2a
|
||||
|
@ -1518,7 +1672,7 @@ index 0000000000000000000000000000000000000000..a1ad3308f9c3545a604b635896259a1c
|
|||
+}
|
||||
diff --git a/src/main/java/ca/spottedleaf/concurrentutil/executor/BaseExecutor.java b/src/main/java/ca/spottedleaf/concurrentutil/executor/BaseExecutor.java
|
||||
new file mode 100644
|
||||
index 0000000000000000000000000000000000000000..716a0fd3f558df748e355069746272facb91de22
|
||||
index 0000000000000000000000000000000000000000..8c452b0988da4725762d543f6bee09915c328ae6
|
||||
--- /dev/null
|
||||
+++ b/src/main/java/ca/spottedleaf/concurrentutil/executor/BaseExecutor.java
|
||||
@@ -0,0 +1,198 @@
|
||||
|
@ -1575,11 +1729,11 @@ index 0000000000000000000000000000000000000000..716a0fd3f558df748e355069746272fa
|
|||
+ * @throws IllegalStateException If the current thread is not allowed to wait
|
||||
+ */
|
||||
+ public default void waitUntilAllExecuted() throws IllegalStateException {
|
||||
+ long failures = 9L; // start out at 1ms
|
||||
+ long failures = 1L; // start at 0.25ms
|
||||
+
|
||||
+ while (!this.haveAllTasksExecuted()) {
|
||||
+ Thread.yield();
|
||||
+ failures = ConcurrentUtil.linearLongBackoff(failures, 500_000L, 5_000_000L); // 500us, 5ms
|
||||
+ failures = ConcurrentUtil.linearLongBackoff(failures, 250_000L, 5_000_000L); // 500us, 5ms
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue