perf[executor]: use & bits operation to improve performance

This commit is contained in:
godotg committed 2024-08-11 12:12:40 +08:00
1 parent 6b4ad5f7bd
commit 452723508b
2 files changed
+18 -13

No files matched your search

@@ -22,6 +22,7 @@ import com.zfoo.protocol.util.RandomUtils;
import com.zfoo.protocol.util.StringUtils;
import com.zfoo.protocol.util.ThreadUtils;
import io.netty.util.concurrent.FastThreadLocalThread;
import io.netty.util.internal.MathUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -46,7 +47,8 @@ public abstract class EventBus {
* EN: The size of the thread pool. Event's thread pool is often used to do time-consuming operations, so set it a little bigger
* CN: 线程池的大小. event的线程池经常用来做一些耗时的操作,所以要设置大一点
*/
private static final int EXECUTORS_SIZE = Math.max(Runtime.getRuntime().availableProcessors(), 4) * 2 + 1;
public static final int EXECUTORS_SIZE = MathUtil.safeFindNextPositivePowerOfTwo(Math.max(Runtime.getRuntime().availableProcessors(), 4) * 2);
private static final int EXECUTOR_MASK = EXECUTORS_SIZE - 1;
private static final ExecutorService[] executors = new ExecutorService[EXECUTORS_SIZE];
@@ -144,8 +146,8 @@ public abstract class EventBus {
executorOf(hash).execute(ThreadUtils.safeRunnable(runnable));
}
public static ExecutorService executorOf(int hash){
return executors[Math.abs(hash % EXECUTORS_SIZE)];
public static ExecutorService executorOf(int hash) {
return executors[hash & EXECUTOR_MASK];
}
/**