Skip to content

Commit 63ff0fa

Browse files
committed
No alloc pls
- Remove lambda allocation fetching instancers
1 parent a82c5ca commit 63ff0fa

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

common/src/backend/java/dev/engine_room/flywheel/backend/engine/DrawManager.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.Queue;
88
import java.util.concurrent.ConcurrentHashMap;
99
import java.util.concurrent.ConcurrentLinkedQueue;
10+
import java.util.function.Function;
1011

1112
import org.jetbrains.annotations.Nullable;
1213

@@ -31,24 +32,30 @@ public abstract class DrawManager<N extends AbstractInstancer<?>> {
3132

3233
/**
3334
* A map of instancer keys to instancers.
34-
* <br>
35-
* This map is populated as instancers are requested and contains both initialized and uninitialized instancers.
35+
*
36+
* <p>This map is populated as instancers are requested and contains both initialized and uninitialized instancers.
3637
*/
3738
protected final Map<InstancerKey<?>, N> instancers = new ConcurrentHashMap<>();
3839
/**
3940
* A list of instancers that have not yet been initialized.
40-
* <br>
41-
* All new instancers land here before having resources allocated in {@link #render}.
41+
*
42+
* <p>All new instancers land here before having resources allocated in {@link #render}.
4243
*/
4344
protected final Queue<UninitializedInstancer<N, ?>> initializationQueue = new ConcurrentLinkedQueue<>();
4445

46+
/**
47+
* Function object to pass into computeIfAbsent.
48+
* <p>Create once and cache to avoid allocating every time.
49+
*/
50+
protected final Function<InstancerKey<?>, N> createAndDeferInit = this::createAndDeferInit;
51+
4552
public <I extends Instance> AbstractInstancer<I> getInstancer(Environment environment, InstanceType<I> type, Model model, int bias) {
4653
return getInstancer(new InstancerKey<>(environment, type, model, bias));
4754
}
4855

4956
@SuppressWarnings("unchecked")
5057
public <I extends Instance> AbstractInstancer<I> getInstancer(InstancerKey<I> key) {
51-
return (AbstractInstancer<I>) instancers.computeIfAbsent(key, this::createAndDeferInit);
58+
return (AbstractInstancer<I>) instancers.computeIfAbsent(key, createAndDeferInit);
5259
}
5360

5461
public Plan<RenderContext> createFramePlan() {

0 commit comments

Comments
 (0)