Skip to content

Commit

Permalink
perf: remove Fiber.id and use IdentityHashMap
Browse files Browse the repository at this point in the history
  • Loading branch information
areyouok committed Dec 25, 2024
1 parent d675e24 commit e96bdec
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ public class Fiber extends WaitSource {
private static final DtLog log = DtLogs.getLogger(Fiber.class);
protected final boolean daemon;

long id;

long scheduleTimeoutMillis;
long scheduleNanoTime;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@

import com.github.dtprj.dongting.common.DtException;
import com.github.dtprj.dongting.common.IndexedQueue;
import com.github.dtprj.dongting.common.LongObjMap;
import com.github.dtprj.dongting.log.BugLog;
import com.github.dtprj.dongting.log.DtLog;
import com.github.dtprj.dongting.log.DtLogs;

import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
import java.util.IdentityHashMap;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
Expand All @@ -42,8 +42,8 @@ public class FiberGroup {
final IndexedQueue<Fiber> readyFibers = new IndexedQueue<>(64);
final IndexedQueue<Fiber> readyFibersNextRound1 = new IndexedQueue<>(16);
final IndexedQueue<Fiber> readyFibersNextRound2 = new IndexedQueue<>(16);
private final LongObjMap<Fiber> normalFibers = new LongObjMap<>(32, 0.6f);
private final LongObjMap<Fiber> daemonFibers = new LongObjMap<>(32, 0.6f);
private final IdentityHashMap<Fiber, Fiber> normalFibers = new IdentityHashMap<>(128);
private final IdentityHashMap<Fiber, Fiber> daemonFibers = new IdentityHashMap<>(128);

@SuppressWarnings("FieldMayBeFinal")
private volatile boolean shouldStop = false;
Expand All @@ -60,8 +60,6 @@ public class FiberGroup {
}
}

private long nextId;

boolean finished;
boolean ready;

Expand Down Expand Up @@ -166,22 +164,20 @@ void start(Fiber f, boolean addFirst) {
return;
}
f.started = true;
long id = nextId++;
f.id = id;
if (f.daemon) {
daemonFibers.put(id, f);
daemonFibers.put(f, f);
} else {
normalFibers.put(id, f);
normalFibers.put(f, f);
}
tryMakeFiberReady(f, addFirst);
}

void removeFiber(Fiber f) {
boolean removed;
if (f.daemon) {
removed = daemonFibers.remove(f.id) != null;
removed = daemonFibers.remove(f) != null;
} else {
removed = normalFibers.remove(f.id) != null;
removed = normalFibers.remove(f) != null;
}
if (!removed) {
BugLog.getLog().error("fiber is not in set: {}", f.getName());
Expand Down Expand Up @@ -231,7 +227,7 @@ private void makeGroupReady() {
void updateFinishStatus() {
boolean ss = (boolean) SHOULD_STOP.get(this);
if (ss && !finished) {
if (normalFibers.size() > 0) {
if (!normalFibers.isEmpty()) {
return;
}
if (sysChannel.queue.size() > 0) {
Expand Down

0 comments on commit e96bdec

Please sign in to comment.