Skip to content

Commit 7b58b1d

Browse files
committed
fix: some performance issues
1 parent 92a710f commit 7b58b1d

File tree

2 files changed

+62
-8
lines changed

2 files changed

+62
-8
lines changed

c2me-rewrites-chunk-system/src/main/java/com/ishland/c2me/rewrites/chunksystem/common/TheChunkSystem.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
package com.ishland.c2me.rewrites.chunksystem.common;
22

3-
import com.ishland.c2me.base.common.GlobalExecutors;
43
import com.ishland.c2me.base.common.scheduler.IVanillaChunkManager;
54
import com.ishland.c2me.base.common.scheduler.SchedulingManager;
65
import com.ishland.c2me.base.mixin.access.IThreadedAnvilChunkStorage;
76
import com.ishland.c2me.base.mixin.access.IVersionedChunkStorage;
7+
import com.ishland.c2me.rewrites.chunksystem.common.structs.ChunkSystemExecutors;
88
import com.ishland.flowsched.scheduler.ExceptionHandlingAction;
99
import com.ishland.flowsched.scheduler.ItemHolder;
1010
import com.ishland.flowsched.scheduler.ItemStatus;
1111
import com.ishland.flowsched.scheduler.KeyStatusPair;
1212
import com.ishland.flowsched.scheduler.StatusAdvancingScheduler;
1313
import com.ishland.flowsched.util.Assertions;
14-
import io.netty.util.internal.PlatformDependent;
1514
import io.reactivex.rxjava3.core.Scheduler;
16-
import io.reactivex.rxjava3.schedulers.Schedulers;
1715
import it.unimi.dsi.fastutil.longs.Long2IntMap;
1816
import it.unimi.dsi.fastutil.longs.Long2IntMaps;
1917
import it.unimi.dsi.fastutil.longs.Long2IntOpenHashMap;
@@ -26,7 +24,6 @@
2624
import org.slf4j.Logger;
2725
import org.slf4j.LoggerFactory;
2826

29-
import java.util.Queue;
3027
import java.util.concurrent.Executor;
3128

3229
public class TheChunkSystem extends StatusAdvancingScheduler<ChunkPos, ChunkState, ChunkLoadingContext, NewChunkHolderVanillaInterface> {
@@ -35,8 +32,6 @@ public class TheChunkSystem extends StatusAdvancingScheduler<ChunkPos, ChunkStat
3532

3633
private final Long2IntMap managedTickets = Long2IntMaps.synchronize(new Long2IntOpenHashMap());
3734
private final SchedulingManager schedulingManager;
38-
private final Executor backingBackgroundExecutor = GlobalExecutors.prioritizedScheduler.executor(15);
39-
private final Scheduler backgroundScheduler = Schedulers.from(this.backingBackgroundExecutor);
4035
private final ServerChunkLoadingManager tacs;
4136

4237
public TheChunkSystem(ServerChunkLoadingManager tacs) {
@@ -49,12 +44,12 @@ public TheChunkSystem(ServerChunkLoadingManager tacs) {
4944

5045
@Override
5146
protected Executor getBackgroundExecutor() {
52-
return this.backingBackgroundExecutor;
47+
return ChunkSystemExecutors.consolidatingBackgroundExecutor;
5348
}
5449

5550
@Override
5651
protected Scheduler getSchedulerBackedByBackgroundExecutor() {
57-
return this.backgroundScheduler;
52+
return ChunkSystemExecutors.consolidatingBackgroundScheduler;
5853
}
5954

6055
@Override
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.ishland.c2me.rewrites.chunksystem.common.structs;
2+
3+
import com.ishland.c2me.base.common.GlobalExecutors;
4+
import io.reactivex.rxjava3.core.Scheduler;
5+
import io.reactivex.rxjava3.schedulers.Schedulers;
6+
7+
import java.util.ArrayDeque;
8+
import java.util.Queue;
9+
import java.util.concurrent.Executor;
10+
11+
public class ChunkSystemExecutors {
12+
13+
private static final ThreadLocal<Queue<Runnable>> CONSOLIDATING_QUEUE = new ThreadLocal<>();
14+
15+
public static final Executor backingBackgroundExecutor = GlobalExecutors.prioritizedScheduler.executor(15);
16+
public static final Scheduler backgroundScheduler = Schedulers.from(backingBackgroundExecutor);
17+
public static final Executor consolidatingBackgroundExecutor = command -> {
18+
Queue<Runnable> runnables = CONSOLIDATING_QUEUE.get();
19+
if (runnables == null) { // first entry
20+
consolidatingRoot(command);
21+
return;
22+
}
23+
runnables.add(command);
24+
};
25+
public static final Scheduler consolidatingBackgroundScheduler = Schedulers.from(consolidatingBackgroundExecutor);
26+
27+
private static void consolidatingRoot(Runnable initialCommand) {
28+
backingBackgroundExecutor.execute(() -> {
29+
Queue<Runnable> runnables = CONSOLIDATING_QUEUE.get();
30+
if (runnables != null) {
31+
new Throwable("CONSOLIDATING_QUEUE leak").printStackTrace();
32+
try {
33+
initialCommand.run();
34+
} catch (Throwable t) {
35+
t.printStackTrace();
36+
}
37+
return;
38+
}
39+
40+
CONSOLIDATING_QUEUE.set(runnables = new ArrayDeque<>());
41+
runnables.add(initialCommand);
42+
try {
43+
while (!runnables.isEmpty()) {
44+
try {
45+
runnables.remove().run();
46+
} catch (Throwable t) {
47+
t.printStackTrace();
48+
}
49+
}
50+
} finally {
51+
if (!runnables.isEmpty()) {
52+
new Throwable("runnable leak").printStackTrace();
53+
}
54+
CONSOLIDATING_QUEUE.remove();
55+
}
56+
});
57+
}
58+
59+
}

0 commit comments

Comments
 (0)