Skip to content

ScheduledFutureTask: avoid invoke system clock again#14162

Merged
normanmaurer merged 2 commits intonetty:4.1from
CLFutureX:4.1_avoid_checking_system_clock_again
Jul 16, 2024
Merged

ScheduledFutureTask: avoid invoke system clock again#14162
normanmaurer merged 2 commits intonetty:4.1from
CLFutureX:4.1_avoid_checking_system_clock_again

Conversation

@CLFutureX
Copy link
Copy Markdown
Contributor

@CLFutureX CLFutureX commented Jul 5, 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.
Fixes #.

If there is no issue then describe the changes introduced by this PR.

@CLFutureX CLFutureX changed the title ScheduledFutureTask: avoid checking system clock again ScheduledFutureTask: avoid invoke system clock again Jul 5, 2024
@normanmaurer normanmaurer requested a review from franz1981 July 5, 2024 07:30
@CLFutureX
Copy link
Copy Markdown
Contributor Author

@franz1981 hey, please review it

Comment thread common/src/main/java/io/netty/util/concurrent/ScheduledFutureTask.java Outdated
@normanmaurer
Copy link
Copy Markdown
Member

@franz1981 @chrisvest PTAL

@normanmaurer normanmaurer requested a review from chrisvest July 12, 2024 11:49
@normanmaurer normanmaurer added this to the 4.1.112.Final milestone Jul 12, 2024
@normanmaurer normanmaurer merged commit a6bf424 into netty:4.1 Jul 16, 2024
@normanmaurer
Copy link
Copy Markdown
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants