ScheduledFutureTask: avoid invoke system clock again#14162
Merged
normanmaurer merged 2 commits intonetty:4.1from Jul 16, 2024
Merged
ScheduledFutureTask: avoid invoke system clock again#14162normanmaurer merged 2 commits intonetty:4.1from
normanmaurer merged 2 commits intonetty:4.1from
Conversation
Contributor
Author
|
@franz1981 hey, please review it |
Member
|
@franz1981 @chrisvest PTAL |
chrisvest
approved these changes
Jul 15, 2024
Member
|
@CLFutureX thanks a lot! |
normanmaurer
added a commit
that referenced
this pull request
Jul 16, 2024
**Motivation:**
When examining the serialized design of NioEventLoop, during the
execution of the runAllTasks method within the run method, expired tasks
are first transferred to the taskQueue. After that, tasks are retrieved
from the taskQueue and executed.
During the task transfer process, if it is discovered that the current
taskQueue is full, the retrieved tasks are put back into the delayed
tasks queue. However, upon retrieval, there is a setConsumed method
called. In this particular scenario, the task is actually not consumed,
which begs the question: Could this have any implications?
After clicking to view the comments:
“Optimization to avoid checking system clock again,after deadline has
passed and task has been dequeued“
Based on the comments, it is speculated that when a task is executed or
re-enqueued, it might involve checking the system clock. After
conducting a thorough investigation, it was found that this could be
related to the task's own run method.
io.netty.util.concurrent.ScheduledFutureTask#run
io.netty.util.concurrent.ScheduledFutureTask#delayNanos()
`public void run() {
...
if (delayNanos() > 0L)
...
}`
`public long delayNanos() {
return delayNanos(scheduledExecutor().getCurrentTimeNanos());
}`
`static long deadlineToDelayNanos(long currentTimeNanos, long
deadlineNanos) {
return deadlineNanos == 0L ? 0L : Math.max(0L, deadlineNanos -
currentTimeNanos);
}`
` public long delayNanos(long currentTimeNanos) {
return deadlineToDelayNanos(currentTimeNanos, deadlineNanos);
}`
If this is indeed the case, then the optimization brought about is
merely reducing a single computation and does not involve the system
clock.
**Modification:**
Therefore, I suggest adding a pre-check here. If the current delayNanos
equals 0L, we can directly return without proceeding, thus reducing the
computation involving the system clock.
` public long delayNanos() {
if(deadlineNanos == 0L){
return 0L;
}
return delayNanos(scheduledExecutor().getCurrentTimeNanos());
}`
**Result:**
Reducing the number of invocations by the amount of expired tasks that
are now bypassed, would lead to an overall significant benefit.
Describe the modifications you've done.
---------
Co-authored-by: Norman Maurer <norman_maurer@apple.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Motivation:
When examining the serialized design of NioEventLoop, during the execution of the runAllTasks method within the run method, expired tasks are first transferred to the taskQueue. After that, tasks are retrieved from the taskQueue and executed.
During the task transfer process, if it is discovered that the current taskQueue is full, the retrieved tasks are put back into the delayed tasks queue. However, upon retrieval, there is a setConsumed method called. In this particular scenario, the task is actually not consumed, which begs the question: Could this have any implications?
After clicking to view the comments:
“Optimization to avoid checking system clock again,after deadline has passed and task has been dequeued“
Based on the comments, it is speculated that when a task is executed or re-enqueued, it might involve checking the system clock. After conducting a thorough investigation, it was found that this could be related to the task's own run method.
io.netty.util.concurrent.ScheduledFutureTask#run
io.netty.util.concurrent.ScheduledFutureTask#delayNanos()
public void run() { ... if (delayNanos() > 0L) ... }public long delayNanos() { return delayNanos(scheduledExecutor().getCurrentTimeNanos()); }static long deadlineToDelayNanos(long currentTimeNanos, long deadlineNanos) { return deadlineNanos == 0L ? 0L : Math.max(0L, deadlineNanos - currentTimeNanos); }public long delayNanos(long currentTimeNanos) { return deadlineToDelayNanos(currentTimeNanos, deadlineNanos); }If this is indeed the case, then the optimization brought about is merely reducing a single computation and does not involve the system clock.
Modification:
Therefore, I suggest adding a pre-check here. If the current delayNanos equals 0L, we can directly return without proceeding, thus reducing the computation involving the system clock.
public long delayNanos() { if(deadlineNanos == 0L){ return 0L; } return delayNanos(scheduledExecutor().getCurrentTimeNanos()); }Result:
Reducing the number of invocations by the amount of expired tasks that are now bypassed, would lead to an overall significant benefit.
Describe the modifications you've done.
Fixes #.
If there is no issue then describe the changes introduced by this PR.