Fix entity memory leak, improve entity collection creation performance

This commit is contained in:
TheMode 2021-04-23 12:58:14 +02:00
parent 136ed17c0b
commit 555cdb66de

View File

@ -164,20 +164,19 @@ public abstract class ThreadProvider {
public synchronized @NotNull CountDownLatch update(long time) { public synchronized @NotNull CountDownLatch update(long time) {
CountDownLatch countDownLatch = new CountDownLatch(threads.size()); CountDownLatch countDownLatch = new CountDownLatch(threads.size());
for (TickThread thread : threads) { for (TickThread thread : threads) {
final var chunkEntries = threadChunkMap.get(thread);
if (chunkEntries == null || chunkEntries.isEmpty()) {
// The thread never had any task
countDownLatch.countDown();
continue;
}
// Execute tick // Execute tick
thread.runnable.startTick(countDownLatch, () -> { thread.runnable.startTick(countDownLatch, () -> {
final var entitiesList = chunkEntries.stream().map(chunkEntry -> chunkEntry.entities).collect(Collectors.toList()); final var chunkEntries = threadChunkMap.get(thread);
final var entities = entitiesList.stream() if (chunkEntries == null || chunkEntries.isEmpty()) {
.flatMap(Collection::stream) // Nothing to tick
.collect(Collectors.toList()); AcquirableEntity.refresh(Collections.emptyList());
AcquirableEntity.refresh(Collections.unmodifiableList(entities)); return;
}
final var entities = chunkEntries.stream()
.flatMap(chunkEntry -> chunkEntry.entities.stream())
.collect(Collectors.toUnmodifiableList());
AcquirableEntity.refresh(entities);
final ReentrantLock lock = thread.getLock(); final ReentrantLock lock = thread.getLock();
lock.lock(); lock.lock();