Add per player chunk loading limits

Configurable under "settings.chunk-loading.player-max-chunk-load-rate",
defaults to -1. This commit also changes the chunk loading to be
distributed equally for all players, rather than distance based. This is
to ensure players flying around do not take priority over everyone else.
The exception to this new rule is the min-load-radius, which still has
priority over everything else.
This commit is contained in:
Spottedleaf
2022-03-31 06:04:23 -07:00
parent 5c7b445c76
commit a096540d94
6 changed files with 124 additions and 19 deletions

View File

@@ -2899,14 +2899,16 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+public final class IntervalledCounter {
+
+ protected long[] times;
+ protected long[] counts;
+ protected final long interval;
+ protected long minTime;
+ protected int sum;
+ protected long sum;
+ protected int head; // inclusive
+ protected int tail; // exclusive
+
+ public IntervalledCounter(final long interval) {
+ this.times = new long[8];
+ this.counts = new long[8];
+ this.interval = interval;
+ }
+
@@ -2915,7 +2917,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ }
+
+ public void updateCurrentTime(final long currentTime) {
+ int sum = this.sum;
+ long sum = this.sum;
+ int head = this.head;
+ final int tail = this.tail;
+ final long minTime = currentTime - this.interval;
@@ -2924,8 +2926,15 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+
+ // guard against overflow by using subtraction
+ while (head != tail && this.times[head] - minTime < 0) {
+ head = (head + 1) % arrayLen;
+ --sum;
+ sum -= this.counts[head];
+ // there are two ways we can do this:
+ // 1. free the count when adding
+ // 2. free it now
+ // option #2
+ this.counts[head] = 0;
+ if (++head >= arrayLen) {
+ head = 0;
+ }
+ }
+
+ this.sum = sum;
@@ -2934,6 +2943,10 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ }
+
+ public void addTime(final long currTime) {
+ this.addTime(currTime, 1L);
+ }
+
+ public void addTime(final long currTime, final long count) {
+ // guard against overflow by using subtraction
+ if (currTime - this.minTime < 0) {
+ return;
@@ -2945,28 +2958,29 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ }
+
+ this.times[this.tail] = currTime;
+ this.counts[this.tail] += count;
+ this.sum += count;
+ this.tail = nextTail;
+ }
+
+ public void updateAndAdd(final int count) {
+ final long currTime = System.nanoTime();
+ this.updateCurrentTime(currTime);
+ for (int i = 0; i < count; ++i) {
+ this.addTime(currTime);
+ }
+ this.addTime(currTime, count);
+ }
+
+ public void updateAndAdd(final int count, final long currTime) {
+ this.updateCurrentTime(currTime);
+ for (int i = 0; i < count; ++i) {
+ this.addTime(currTime);
+ }
+ this.addTime(currTime, count);
+ }
+
+ private void resize() {
+ final long[] oldElements = this.times;
+ final long[] oldCounts = this.counts;
+ final long[] newElements = new long[this.times.length * 2];
+ final long[] newCounts = new long[this.times.length * 2];
+ this.times = newElements;
+ this.counts = newCounts;
+
+ final int head = this.head;
+ final int tail = this.tail;
@@ -2976,9 +2990,13 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+
+ if (tail >= head) {
+ System.arraycopy(oldElements, head, newElements, 0, size);
+ System.arraycopy(oldCounts, head, newCounts, 0, size);
+ } else {
+ System.arraycopy(oldElements, head, newElements, 0, oldElements.length - head);
+ System.arraycopy(oldElements, 0, newElements, oldElements.length - head, tail);
+
+ System.arraycopy(oldCounts, head, newCounts, 0, oldCounts.length - head);
+ System.arraycopy(oldCounts, 0, newCounts, oldCounts.length - head, tail);
+ }
+ }
+
@@ -2987,11 +3005,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ return this.size() / (this.interval * 1.0e-9);
+ }
+
+ public int size() {
+ final int head = this.head;
+ final int tail = this.tail;
+
+ return tail >= head ? (tail - head) : (tail + (this.times.length - head));
+ public long size() {
+ return this.sum;
+ }
+}
diff --git a/src/main/java/io/papermc/paper/util/WorldUtil.java b/src/main/java/io/papermc/paper/util/WorldUtil.java