-
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
base: master
Are you sure you want to change the base?
Changes from 4 commits
66a9711
bc4005b
3a94fa4
06d7d82
1f285aa
72b3977
93ba64f
b4d59ef
d792a61
4d109fc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package io.github.pylonmc.rebar.async | ||
|
|
||
| data class ScheduledTask(val executeTick: Long, val runnable: Runnable) : Comparable<ScheduledTask> { | ||
|
Intybyte marked this conversation as resolved.
Outdated
|
||
| override fun compareTo(other: ScheduledTask): Int { | ||
| return executeTick.compareTo(other.executeTick) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package io.github.pylonmc.rebar.async.schedulers | ||
|
|
||
| import io.github.pylonmc.rebar.async.ScheduledTask | ||
| import java.util.* | ||
| import java.util.concurrent.PriorityBlockingQueue | ||
|
|
||
| /** | ||
| * Scheduler using a [PriorityQueue] as a delegate | ||
| * | ||
| * O(log n) insertions and evictions | ||
| */ | ||
| class PriorityQueueScheduler : Scheduler { | ||
| private val taskQueue = PriorityBlockingQueue<ScheduledTask>() | ||
|
Intybyte marked this conversation as resolved.
Outdated
|
||
|
|
||
| override fun schedule(executeAt: Long, runnable: Runnable) { | ||
| taskQueue.add(ScheduledTask(executeAt, runnable)) | ||
| } | ||
|
|
||
| override fun getValid(currentTick: Long): List<ScheduledTask> { | ||
| val list = mutableListOf<ScheduledTask>() | ||
| while (taskQueue.isNotEmpty() && taskQueue.peek().executeTick <= currentTick) { | ||
| val task = taskQueue.poll() | ||
| list.add(task) | ||
| } | ||
|
|
||
| return list | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package io.github.pylonmc.rebar.async.schedulers | ||
|
|
||
| import io.github.pylonmc.rebar.async.ScheduledTask | ||
|
|
||
| interface Scheduler { | ||
|
|
||
| /** | ||
| * Adds a task to the scheduler | ||
| */ | ||
| fun schedule(executeAt: Long, runnable: Runnable) | ||
|
|
||
| /** | ||
| * Gets and evicts valid tasks | ||
| */ | ||
| fun getValid(currentTick: Long) : List<ScheduledTask> | ||
| } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Am I reading the docs right that you can only schedule tasks for tick 2^exponent? A little confused as to how this works, does it actually behave any differently from a priority queue in terms of when it runs task? The docs seem to imply so or maybe I am reading them wrong?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, view it as an hashmap based on the execution tick It runs and check the tasks for a specific tick so the tasks are ran the same for tickSpeed = 1, otherwise it would make some buckets useless and it could break if tasks fall in said buckets
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for example if it does tickspeed 2 it would skip all the even position buckets, and if you run a delay and falls into said positions it would break said task |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package io.github.pylonmc.rebar.async.schedulers | ||
|
|
||
| import io.github.pylonmc.rebar.async.ScheduledTask | ||
| 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 { | ||
|
Intybyte marked this conversation as resolved.
Outdated
|
||
| 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(executeAt: Long, runnable: Runnable) { | ||
| incoming.add(ScheduledTask(executeAt, runnable)) | ||
| } | ||
|
|
||
| override fun getValid(currentTick: Long) : List<ScheduledTask> { | ||
| while (true) { | ||
| val task = incoming.poll() ?: break | ||
| val slot = task.executeTick.toInt() and mask | ||
| wheel[slot].add(task) | ||
| } | ||
|
|
||
| val slot = currentTick.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 <= currentTick) { | ||
| list.add(task) | ||
| iter.remove() | ||
| } | ||
| } | ||
|
|
||
| return list | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.