ChunkTaskPriorityQueue .java

net.minecraft.server.level.ChunkTaskPriorityQueue (class, 92 lines): members: topPriorityQueueIndex, PRIORITY_LEVEL_COUNT, queuesPerPriority, hasWork, pop.

net.minecraft.server.levelclass92 linesserver3.0 KB

API Surface (6)

void resortChunkTasks(final int oldPriority, final ChunkPos pos, final int newPriority)
void submit(final Runnable task, final long chunkPos, final int level)
void release(final long pos, final boolean unschedule)
boolean hasWork()
String toString()
record TasksForChunk(long chunkPos, List<Runnable> tasks)

Fields & Constants (4)

PRIORITY_LEVEL_COUNT
queuesPerPriority
topPriorityQueueIndex
name

Code Graph: Dependencies (1)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

ChunkTaskPriorityQueue.java92 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.server.level;
2
3
import com.google.common.collect.Lists;
4
import it.unimi.dsi.fastutil.longs.Long2ObjectLinkedOpenHashMap;
5
import java.util.List;
6
import java.util.stream.IntStream;
7
import net.minecraft.world.level.ChunkPos;
8
import org.jspecify.annotations.Nullable;
9
10
public class ChunkTaskPriorityQueue {
11
	public static final int PRIORITY_LEVEL_COUNT = ChunkLevel.MAX_LEVEL + 2;
12
	private final List<Long2ObjectLinkedOpenHashMap<List<Runnable>>> queuesPerPriority = IntStream.range(0, PRIORITY_LEVEL_COUNT)
13
		.mapToObj(priority -> new Long2ObjectLinkedOpenHashMap<>())
14
		.toList();
15
	private volatile int topPriorityQueueIndex = PRIORITY_LEVEL_COUNT;
16
	private final String name;
17
18
	public ChunkTaskPriorityQueue(final String name) {
19
		this.name = name;
20
	}
21
22
	protected void resortChunkTasks(final int oldPriority, final ChunkPos pos, final int newPriority) {
23
		if (oldPriority < PRIORITY_LEVEL_COUNT) {
24
			Long2ObjectLinkedOpenHashMap<List<Runnable>> oldQueue = this.queuesPerPriority.get(oldPriority);
25
			List<Runnable> oldTasks = oldQueue.remove(pos.pack());
26
			if (oldPriority == this.topPriorityQueueIndex) {
27
				while (this.hasWork() && this.queuesPerPriority.get(this.topPriorityQueueIndex).isEmpty()) {
28
					this.topPriorityQueueIndex++;
29
				}
30
			}
31
32
			if (oldTasks != null && !oldTasks.isEmpty()) {
33
				this.queuesPerPriority.get(newPriority).computeIfAbsent(pos.pack(), k -> Lists.newArrayList()).addAll(oldTasks);
34
				this.topPriorityQueueIndex = Math.min(this.topPriorityQueueIndex, newPriority);
35
			}
36
		}
37
	}
38
39
	protected void submit(final Runnable task, final long chunkPos, final int level) {
40
		this.queuesPerPriority.get(level).computeIfAbsent(chunkPos, p -> Lists.newArrayList()).add(task);
41
		this.topPriorityQueueIndex = Math.min(this.topPriorityQueueIndex, level);
42
	}
43
44
	protected void release(final long pos, final boolean unschedule) {
45
		for (Long2ObjectLinkedOpenHashMap<List<Runnable>> queue : this.queuesPerPriority) {
46
			List<Runnable> tasks = queue.get(pos);
47
			if (tasks != null) {
48
				if (unschedule) {
49
					tasks.clear();
50
				}
51
52
				if (tasks.isEmpty()) {
53
					queue.remove(pos);
54
				}
55
			}
56
		}
57
58
		while (this.hasWork() && this.queuesPerPriority.get(this.topPriorityQueueIndex).isEmpty()) {
59
			this.topPriorityQueueIndex++;
60
		}
61
	}
62
63
	public ChunkTaskPriorityQueue.@Nullable TasksForChunk pop() {
64
		if (!this.hasWork()) {
65
			return null;
66
		}
67
68
		int index = this.topPriorityQueueIndex;
69
		Long2ObjectLinkedOpenHashMap<List<Runnable>> queue = this.queuesPerPriority.get(index);
70
		long chunkPos = queue.firstLongKey();
71
		List<Runnable> tasks = queue.removeFirst();
72
73
		while (this.hasWork() && this.queuesPerPriority.get(this.topPriorityQueueIndex).isEmpty()) {
74
			this.topPriorityQueueIndex++;
75
		}
76
77
		return new ChunkTaskPriorityQueue.TasksForChunk(chunkPos, tasks);
78
	}
79
80
	public boolean hasWork() {
81
		return this.topPriorityQueueIndex < PRIORITY_LEVEL_COUNT;
82
	}
83
84
	@Override
85
	public String toString() {
86
		return this.name + " " + this.topPriorityQueueIndex + "...";
87
	}
88
89
	public record TasksForChunk(long chunkPos, List<Runnable> tasks) {
90
	}
91
}
92

Decompiled Minecraft 26.3 source of ChunkTaskPriorityQueue (net/minecraft/server/level/ChunkTaskPriorityQueue.java) with Mojang official mappings, stored verbatim on the site.