-
Notifications
You must be signed in to change notification settings - Fork 13
Use TimingWheel on Scheduler when possible #671
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
10
commits into
master
Choose a base branch
from
vaan/opt/scheduler
base: master
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
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
66a9711
Use TimingWheel when possible
Intybyte bc4005b
Refine Scheduler interface
Intybyte 3a94fa4
Make PriorityQueueScheduler thread safe
Intybyte 06d7d82
Relocate classes
Intybyte 1f285aa
Cleanup and small changes
Intybyte 72b3977
Use synchronizedQueue
Intybyte 93ba64f
Formatting goof
Seggan b4d59ef
Make TimingWheel internal
Intybyte d792a61
REEEEEEEEEEEEEBARRRRR
Intybyte 4d109fc
Whopsie
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
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
26 changes: 26 additions & 0 deletions
26
rebar/src/main/kotlin/io/github/pylonmc/rebar/collections/tasks/PriorityQueueScheduler.kt
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,26 @@ | ||
| package io.github.pylonmc.rebar.collections.tasks | ||
|
|
||
| import java.util.* | ||
|
|
||
| /** | ||
| * Scheduler using a [PriorityQueue] as a delegate | ||
| * | ||
| * O(log n) insertions and evictions | ||
| */ | ||
| class PriorityQueueScheduler : Scheduler { | ||
| private val taskQueue = PriorityQueue<ScheduledTask>() // this is not really thread safe, should it be changed? | ||
|
Intybyte marked this conversation as resolved.
Outdated
|
||
|
|
||
| override fun schedule(tick: Long, delayTicks: Long, runnable: Runnable) { | ||
| taskQueue.add(ScheduledTask(tick + delayTicks, runnable)) | ||
| } | ||
|
|
||
| override fun getValid(tick: Long): List<ScheduledTask> { | ||
| val list = mutableListOf<ScheduledTask>() | ||
| while (taskQueue.isNotEmpty() && taskQueue.peek().executeTick <= tick) { | ||
| val task = taskQueue.poll() | ||
| list.add(task) | ||
| } | ||
|
|
||
| return list | ||
| } | ||
| } | ||
7 changes: 7 additions & 0 deletions
7
rebar/src/main/kotlin/io/github/pylonmc/rebar/collections/tasks/ScheduledTask.kt
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.github.pylonmc.rebar.collections.tasks | ||
|
|
||
| data class ScheduledTask(val executeTick: Long, val runnable: Runnable) : Comparable<ScheduledTask> { | ||
| override fun compareTo(other: ScheduledTask): Int { | ||
| return executeTick.compareTo(other.executeTick) | ||
| } | ||
| } |
14 changes: 14 additions & 0 deletions
14
rebar/src/main/kotlin/io/github/pylonmc/rebar/collections/tasks/Scheduler.kt
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,14 @@ | ||
| package io.github.pylonmc.rebar.collections.tasks | ||
|
|
||
| interface Scheduler { | ||
|
|
||
| /** | ||
| * Adds a task to the scheduler | ||
| */ | ||
| fun schedule(tick: Long, delayTicks: Long, runnable: Runnable) | ||
|
Intybyte marked this conversation as resolved.
Outdated
|
||
|
|
||
| /** | ||
| * Gets and evicts valid tasks | ||
| */ | ||
| fun getValid(tick: Long) : List<ScheduledTask> | ||
|
Intybyte marked this conversation as resolved.
Outdated
|
||
| } | ||
55 changes: 55 additions & 0 deletions
55
rebar/src/main/kotlin/io/github/pylonmc/rebar/collections/tasks/TimingWheel.kt
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,55 @@ | ||
| package io.github.pylonmc.rebar.collections.tasks | ||
|
|
||
| import java.util.concurrent.ConcurrentLinkedQueue | ||
|
|
||
|
|
||
| /** | ||
| * 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 (even though I doubt there would be much improvement) | ||
| * | ||
| * @param exponent wheel size (wheelSize = 2 ^ exponent) | ||
| */ | ||
| class TimingWheel(exponent: Int) : Scheduler { | ||
| private val wheelSize = 1 shl exponent | ||
| private val mask = wheelSize - 1 | ||
| private val wheel = Array(wheelSize) { ArrayDeque<ScheduledTask>() } | ||
| // use thread safe queue | ||
| private val incoming = ConcurrentLinkedQueue<ScheduledTask>() | ||
|
|
||
| override fun schedule(tick: Long, delayTicks: Long, runnable: Runnable) { | ||
| val executeTick = tick + delayTicks | ||
| incoming.add(ScheduledTask(executeTick, runnable)) | ||
| } | ||
|
|
||
| override fun getValid(tick: Long) : List<ScheduledTask> { | ||
| while (true) { | ||
| val task = incoming.poll() ?: break | ||
| val slot = task.executeTick.toInt() and mask | ||
| wheel[slot].add(task) | ||
| } | ||
|
|
||
| val slot = tick.toInt() and mask | ||
| val bucket = wheel[slot] | ||
| if (bucket.isEmpty()) { | ||
| return emptyList() | ||
| } | ||
|
|
||
| val iter = bucket.iterator() | ||
| val list = mutableListOf<ScheduledTask>() | ||
| while (iter.hasNext()) { | ||
| val task = iter.next() | ||
|
|
||
| if (task.executeTick <= tick) { | ||
| list.add(task) | ||
| iter.remove() | ||
| } | ||
| } | ||
|
|
||
| return list | ||
| } | ||
| } |
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.