-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Scheduler optimization #13705
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Intybyte
wants to merge
11
commits into
PaperMC:main
Choose a base branch
from
Intybyte:opt/timing-wheel
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Scheduler optimization #13705
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
40d1d9e
Timing Wheel
Intybyte d633ac1
Remove comments
Intybyte 28f7c04
Rename interface
Intybyte 6d09fb0
Forgot one
Intybyte 3379a43
Use LinkedList
Intybyte fe3d809
Highlight FIFO
Intybyte 2390f4b
Fix supplying late task
Intybyte 95365e3
Add getCreatedAt to interface
Intybyte 6cf693a
Sorted linked list
Intybyte 54ce706
currentTick is a field come on brain
Intybyte ac5875b
LinkedList sorting
Intybyte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
paper-server/src/main/java/io/papermc/paper/util/concurrent/TickBoundTask.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package io.papermc.paper.util.concurrent; | ||
|
|
||
| public interface TickBoundTask { | ||
| long getNextRun(); | ||
| void setNextRun(long next); | ||
| long getCreatedAt(); | ||
| } |
159 changes: 159 additions & 0 deletions
159
paper-server/src/main/java/io/papermc/paper/util/concurrent/TimingWheel.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| package io.papermc.paper.util.concurrent; | ||
|
|
||
| import org.jetbrains.annotations.NotNull; | ||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
| import java.util.Comparator; | ||
| import java.util.Iterator; | ||
| import java.util.LinkedList; | ||
| import java.util.List; | ||
| import java.util.ListIterator; | ||
| import java.util.NoSuchElementException; | ||
| import java.util.function.Predicate; | ||
|
|
||
| /** | ||
| * This class schedules tasks in ticks and executes them efficiently using a circular array (the wheel). | ||
| * Each slot in the wheel represents a specific tick modulo the wheel size. | ||
| * Tasks are placed into slots based on their target execution tick. | ||
| * On each tick, the wheel checks the current slot and runs any tasks whose execute tick has been reached. | ||
| * | ||
| * O(1) task scheduling and retrieval within a single wheel rotation. | ||
| * We are using power of 2 for faster operations than modulo. | ||
| * | ||
| */ | ||
| public class TimingWheel<T extends TickBoundTask> implements Iterable<T> { | ||
| private final int wheelSize; | ||
| private final long mask; | ||
| private final LinkedList<T>[] wheel; | ||
|
|
||
| private static final Comparator<TickBoundTask> ORDERING = Comparator.comparingLong(TickBoundTask::getCreatedAt); | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| public TimingWheel(int exponent) { | ||
| this.wheelSize = 1 << exponent; | ||
| this.mask = wheelSize - 1L; | ||
|
|
||
| this.wheel = (LinkedList<T>[]) new LinkedList[wheelSize]; | ||
| for (int i = 0; i < wheelSize; i++) { | ||
| wheel[i] = new LinkedList<>(); | ||
| } | ||
| } | ||
|
|
||
| public void add(T task, int currentTick) { | ||
| long nextRun = task.getNextRun(); | ||
|
|
||
| if (nextRun <= currentTick) { | ||
| nextRun = currentTick; | ||
| task.setNextRun(nextRun); | ||
| } | ||
|
|
||
| int slot = (int) (nextRun & mask); | ||
| LinkedList<T> bucket = wheel[slot]; | ||
| bucket.add(task); | ||
| } | ||
|
|
||
| public void addAll(Collection<? extends T> tasks, int currentTick) { | ||
| for (T task : tasks) { | ||
| this.add(task, currentTick); | ||
| } | ||
| } | ||
|
|
||
| public @NotNull List<T> popValid(int currentTick) { | ||
| int slot = (int) (currentTick & mask); | ||
| LinkedList<T> bucket = wheel[slot]; | ||
| if (bucket.isEmpty()) return Collections.emptyList(); | ||
|
|
||
| Iterator<T> iter = bucket.iterator(); | ||
| List<T> list = new ArrayList<>(); | ||
| while (iter.hasNext()) { | ||
| T task = iter.next(); | ||
|
|
||
| if (task.getNextRun() <= currentTick) { | ||
| iter.remove(); | ||
| list.add(task); | ||
| } | ||
| } | ||
|
|
||
| list.sort(ORDERING); | ||
| return list; | ||
| } | ||
|
|
||
| public boolean isReady(int currentTick) { | ||
| int slot = (int) (currentTick & mask); | ||
| LinkedList<T> bucket = wheel[slot]; | ||
| if (bucket.isEmpty()) return false; | ||
|
|
||
| for (final T task : bucket) { | ||
| if (task.getNextRun() <= currentTick) { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| public void removeIf(Predicate<T> apply) { | ||
| Iterator<T> itr = iterator(); | ||
| while (itr.hasNext()) { | ||
| T next = itr.next(); | ||
| if (apply.test(next)) { | ||
| itr.remove(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| private class Itr implements Iterator<T> { | ||
| private int index = 0; | ||
| private Iterator<T> current = Collections.emptyIterator(); | ||
| private Iterator<T> lastIterator = null; | ||
|
|
||
| @Override | ||
| public boolean hasNext() { | ||
| if (current.hasNext()) { | ||
| return true; | ||
| } | ||
|
|
||
| for (int i = index; i < wheelSize; i++) { | ||
| if (!wheel[i].isEmpty()) { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public T next() { | ||
| while (true) { | ||
| if (current.hasNext()) { | ||
| lastIterator = current; | ||
| return current.next(); | ||
| } | ||
|
|
||
| if (index >= wheelSize) { | ||
| throw new NoSuchElementException(); | ||
| } | ||
|
|
||
| current = wheel[index++].iterator(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void remove() { | ||
| if (lastIterator == null) { | ||
| throw new NoSuchElementException(); | ||
| } | ||
|
|
||
| lastIterator.remove(); | ||
| lastIterator = null; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public @NotNull Iterator<T> iterator() { | ||
| return new Itr(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.