-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 改进了 Ebwizardry 的发射器优化。 - 改进了 ParallelRandomBlockTicker 的性能。
- Loading branch information
1 parent
6663d1c
commit 1ec5e47
Showing
125 changed files
with
27,981 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 49 additions & 22 deletions
71
src/main/java/github/kasuminova/stellarcore/common/itemstack/ItemStackCapInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,105 @@ | ||
package github.kasuminova.stellarcore.common.itemstack; | ||
|
||
import github.kasuminova.stellarcore.common.util.StellarLog; | ||
import io.netty.util.internal.shaded.org.jctools.queues.atomic.MpscLinkedAtomicQueue; | ||
import github.kasuminova.stellarcore.shaded.org.jctools.queues.MpmcUnboundedXaddArrayQueue; | ||
|
||
import java.util.List; | ||
import java.util.Queue; | ||
import java.util.concurrent.ConcurrentLinkedQueue; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.concurrent.locks.LockSupport; | ||
|
||
public class ItemStackCapInitializer implements Runnable { | ||
|
||
public static final ItemStackCapInitializer INSTANCE = new ItemStackCapInitializer(); | ||
|
||
|
||
private static final int QUEUE_BOUND_SIZE = 100_000; | ||
private static final int MAX_THREADS = Math.min(Runtime.getRuntime().availableProcessors(), 4); | ||
|
||
private static final ThreadLocal<Long> PARKED_MICROS = ThreadLocal.withInitial(() -> 0L); | ||
|
||
private final Queue<ItemStackCapInitTask> taskQueue = createConcurrentQueue(); | ||
|
||
private long parkedMicros = 0; | ||
|
||
private final Thread worker; | ||
private final AtomicInteger queueSize = new AtomicInteger(); | ||
private final List<Thread> workers = new CopyOnWriteArrayList<>(); | ||
|
||
private ItemStackCapInitializer() { | ||
worker = new Thread(this, "StellarCore-ItemStackCapInitializer"); | ||
createWorkerInternal(); | ||
} | ||
|
||
private synchronized void createWorker() { | ||
if (workers.size() >= MAX_THREADS) { | ||
return; | ||
} | ||
createWorkerInternal(); | ||
} | ||
|
||
private void createWorkerInternal() { | ||
Thread worker = new Thread(this, "StellarCore-ItemStackCapInitializer-" + workers.size()); | ||
worker.start(); | ||
worker.setPriority(7); | ||
workers.add(worker); | ||
} | ||
|
||
public void addTask(final ItemStackCapInitTask task) { | ||
taskQueue.offer(task); | ||
|
||
int tasks = queueSize.incrementAndGet(); | ||
int workers = this.workers.size(); | ||
if ((workers > 0) && (tasks > (QUEUE_BOUND_SIZE * workers)) && (workers < MAX_THREADS)) { | ||
StellarLog.LOG.warn("[StellarCore-ItemStackCapInitializer] Creating new worker because queue size reached bound size (limit {}).", QUEUE_BOUND_SIZE * workers); | ||
createWorker(); | ||
} | ||
} | ||
|
||
@Override | ||
public void run() { | ||
StellarLog.LOG.info("[StellarCore] ItemStackCapInitializer started."); | ||
StellarLog.LOG.info("[StellarCore] {} started.", Thread.currentThread().getName()); | ||
while (!Thread.currentThread().isInterrupted()) { | ||
ItemStackCapInitTask task; | ||
boolean executed = false; | ||
|
||
try { | ||
while ((task = taskQueue.poll()) != null) { | ||
while ((task = pollTask()) != null) { | ||
task.run(); | ||
executed = true; | ||
queueSize.decrementAndGet(); | ||
} | ||
} catch (Throwable e) { | ||
StellarLog.LOG.error("[StellarCore] ItemStackCapInitializer failed to execute task.", e); | ||
} | ||
|
||
if (executed) { | ||
parkedMicros = 0; | ||
PARKED_MICROS.set(0L); | ||
} | ||
park(); | ||
} | ||
StellarLog.LOG.warn("[StellarCore] ItemStackCapInitializer stopped, it may be invalid."); | ||
StellarLog.LOG.warn("[StellarCore] {} stopped, it may be invalid.", Thread.currentThread().getName()); | ||
} | ||
|
||
private void park() { | ||
private ItemStackCapInitTask pollTask() { | ||
return taskQueue.poll(); | ||
} | ||
|
||
private static void park() { | ||
long parkedMicros = PARKED_MICROS.get(); | ||
if (parkedMicros > 100_000) { // 100ms | ||
LockSupport.parkNanos(500_000L); // 0.5ms | ||
parkedMicros += 500; | ||
PARKED_MICROS.set(parkedMicros + 500); | ||
} else if (parkedMicros > 50_000) { // 50ms | ||
LockSupport.parkNanos(100_000L); // 0.1ms | ||
parkedMicros += 100; | ||
PARKED_MICROS.set(parkedMicros + 100); | ||
} else if (parkedMicros > 10_000) { // 10ms | ||
LockSupport.parkNanos(10_000L); // 10μs | ||
parkedMicros += 10; | ||
PARKED_MICROS.set(parkedMicros + 10); | ||
} else { | ||
LockSupport.parkNanos(5_000L); // 5μs | ||
parkedMicros += 5; | ||
PARKED_MICROS.set(parkedMicros + 5); | ||
} | ||
} | ||
|
||
private static <E> Queue<E> createConcurrentQueue() { | ||
try { | ||
// May be incompatible with cleanroom. | ||
return new MpscLinkedAtomicQueue<>(); | ||
} catch (Throwable e) { | ||
return new ConcurrentLinkedQueue<>(); | ||
} | ||
return new MpmcUnboundedXaddArrayQueue<>(1_000, 100 * MAX_THREADS); | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
src/main/java/github/kasuminova/stellarcore/common/pool/CanonicalizeWorker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/main/java/github/kasuminova/stellarcore/mixin/ebwizardry/MixinTileEntityDispenser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package github.kasuminova.stellarcore.mixin.ebwizardry; | ||
|
||
import electroblob.wizardry.data.DispenserCastingData; | ||
import net.minecraft.tileentity.TileEntityDispenser; | ||
import net.minecraft.tileentity.TileEntityLockableLoot; | ||
import net.minecraft.util.ITickable; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Unique; | ||
|
||
@Mixin(TileEntityDispenser.class) | ||
public abstract class MixinTileEntityDispenser extends TileEntityLockableLoot implements ITickable { | ||
|
||
@Unique | ||
@Override | ||
@SuppressWarnings({"DataFlowIssue", "AddedMixinMembersNamePattern"}) | ||
public void update() { | ||
if (world.isRemote) { | ||
return; | ||
} | ||
DispenserCastingData data = DispenserCastingData.get((TileEntityDispenser) (Object) this); | ||
if (data != null) { | ||
data.update(); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.