Skip to content

Commit

Permalink
Deprecates LazyRunnable (#13336)
Browse files Browse the repository at this point in the history
Motivation:

Type check vs LazyRunnable receive an always-on performance hit for a common operation that users shouldn't pay if they won't use it.

Modifications:

Deprecates LazyRunnable providing a comment to help
existing users an alternative mechanism.

Result:

Fixes #13335

Co-authored-by: Chris Vest <mr.chrisvest@gmail.com>
  • Loading branch information
franz1981 and chrisvest committed Apr 18, 2023
1 parent 1314ede commit de876d1
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,25 +177,19 @@ protected static void runTask(@Execute Runnable task) {
/**
* Like {@link #execute(Runnable)} but does not guarantee the task will be run until either
* a non-lazy task is executed or the executor is shut down.
*
* This is equivalent to submitting a {@link AbstractEventExecutor.LazyRunnable} to
* {@link #execute(Runnable)} but for an arbitrary {@link Runnable}.
*
* <p>
* The default implementation just delegates to {@link #execute(Runnable)}.
* </p>
*/
@UnstableApi
public void lazyExecute(Runnable task) {
lazyExecute0(task);
}

private void lazyExecute0(@Schedule Runnable task) {
execute(task);
}

/**
* Marker interface for {@link Runnable} to indicate that it should be queued for execution
* but does not need to run immediately.
* @deprecated override {@link SingleThreadEventExecutor#wakesUpForTask} to re-create this behaviour
*
*/
@UnstableApi
@Deprecated
public interface LazyRunnable extends Runnable { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ public void lazyExecute(Runnable task) {

private void execute0(@Schedule Runnable task) {
ObjectUtil.checkNotNull(task, "task");
execute(task, !(task instanceof LazyRunnable) && wakesUpForTask(task));
execute(task, wakesUpForTask(task));
}

private void lazyExecute0(@Schedule Runnable task) {
Expand Down Expand Up @@ -917,7 +917,7 @@ public final ThreadProperties threadProperties() {
}

/**
* @deprecated use {@link AbstractEventExecutor.LazyRunnable}
* @deprecated override {@link SingleThreadEventExecutor#wakesUpForTask} to re-create this behaviour
*/
@Deprecated
protected interface NonWakeupRunnable extends LazyRunnable { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import org.junit.jupiter.api.Test;

import io.netty.util.concurrent.AbstractEventExecutor.LazyRunnable;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.api.function.Executable;

Expand Down Expand Up @@ -208,12 +207,18 @@ public void run() {
}
}

static class LazyLatchTask extends LatchTask implements LazyRunnable { }
static class LazyLatchTask extends LatchTask { }

@Test
public void testLazyExecution() throws Exception {
final SingleThreadEventExecutor executor = new SingleThreadEventExecutor(null,
Executors.defaultThreadFactory(), false) {

@Override
protected boolean wakesUpForTask(final Runnable task) {
return !(task instanceof LazyLatchTask);
}

@Override
protected void run() {
while (!confirmShutdown()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public final void executeAfterEventLoopIteration(Runnable task) {
reject(task);
}

if (!(task instanceof LazyRunnable) && wakesUpForTask(task)) {
if (wakesUpForTask(task)) {
wakeup(inEventLoop());
}
}
Expand Down

0 comments on commit de876d1

Please sign in to comment.