DensityBufferPool .java

net.minecraft.world.level.levelgen.densityfunction.DensityBufferPool (class): members: BUFFER_SIZE_INCREMENT, MAX_REUSE_SIZE_FACTOR, maxAge, buffers.

net.minecraft.world.level.levelgen.densityfunctionclass72 linescommon1.8 KB

Implements

API Surface (5)

ScopedDensityBuffer acquire(final int size)
void release(final ScopedDensityBuffer buffer)
void garbageCollect()
void clear()
boolean isEmpty()

Fields & Constants (4)

BUFFER_SIZE_INCREMENT
MAX_REUSE_SIZE_FACTOR
maxAge
buffers

Code Graph: Dependencies (3)

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

Full Source

DensityBufferPool.java72 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.levelgen.densityfunction;
2
3
import com.google.common.annotations.VisibleForTesting;
4
import java.util.ArrayList;
5
import java.util.List;
6
import net.minecraft.util.Mth;
7
import org.jspecify.annotations.Nullable;
8
9
public class DensityBufferPool implements DensityBufferArena {
10
	private static final int BUFFER_SIZE_INCREMENT = 16;
11
	private static final int MAX_REUSE_SIZE_FACTOR = 2;
12
	private final int maxAge;
13
	private final List<ScopedDensityBuffer> buffers = new ArrayList<>();
14
15
	public DensityBufferPool(final int maxAge) {
16
		this.maxAge = maxAge;
17
	}
18
19
	@Override
20
	public ScopedDensityBuffer acquire(final int size) {
21
		int roundedMinSize = Mth.roundToward(size, 16);
22
		ScopedDensityBuffer buffer = this.tryTakeBest(roundedMinSize, roundedMinSize * 2);
23
		if (buffer == null) {
24
			return new ScopedDensityBuffer(this, roundedMinSize, size);
25
		}
26
27
		buffer.restore(size);
28
		return buffer;
29
	}
30
31
	private @Nullable ScopedDensityBuffer tryTakeBest(final int minCapacity, final int maxCapacity) {
32
		int bestIndex = -1;
33
		int bestCapacity = maxCapacity + 1;
34
35
		for (int i = this.buffers.size() - 1; i >= 0; i--) {
36
			int capacity = this.buffers.get(i).capacity();
37
			if (capacity == minCapacity) {
38
				return this.buffers.remove(i);
39
			}
40
41
			if (capacity > minCapacity && capacity < bestCapacity) {
42
				bestIndex = i;
43
				bestCapacity = capacity;
44
			}
45
		}
46
47
		return bestIndex == -1 ? null : this.buffers.remove(bestIndex);
48
	}
49
50
	@Override
51
	public void release(final ScopedDensityBuffer buffer) {
52
		this.buffers.add(buffer);
53
	}
54
55
	public void garbageCollect() {
56
		this.buffers.removeIf(buffer -> buffer.incrementAge() > this.maxAge);
57
	}
58
59
	public void clear() {
60
		this.buffers.clear();
61
	}
62
63
	@VisibleForTesting
64
	int size() {
65
		return this.buffers.size();
66
	}
67
68
	public boolean isEmpty() {
69
		return this.buffers.isEmpty();
70
	}
71
}
72

Decompiled Minecraft 26.3 source of DensityBufferPool (net/minecraft/world/level/levelgen/densityfunction/DensityBufferPool.java) with Mojang official mappings, stored verbatim on the site.