aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/makamys/neodymium/util/RecyclingList.java
diff options
context:
space:
mode:
authormakamys <makamys@outlook.com>2022-06-21 04:46:43 +0200
committermakamys <makamys@outlook.com>2022-06-21 06:09:26 +0200
commitd6b0f3f9978799b26a3ebca1a91ebc110a6b894a (patch)
treea4f7f2124f98c0bbe6b0797228b14d99f32bf1c8 /src/main/java/makamys/neodymium/util/RecyclingList.java
parent6f22d15c8532ee5e5d81a383363a6148e9449ebb (diff)
downloadNeodymium-d6b0f3f9978799b26a3ebca1a91ebc110a6b894a.tar.gz
Neodymium-d6b0f3f9978799b26a3ebca1a91ebc110a6b894a.tar.bz2
Neodymium-d6b0f3f9978799b26a3ebca1a91ebc110a6b894a.zip
Recycle MeshQuad objects
Reduces client thread's RAM allocation rate from ~80 MB/s to ~20 MB/s.
Diffstat (limited to 'src/main/java/makamys/neodymium/util/RecyclingList.java')
-rw-r--r--src/main/java/makamys/neodymium/util/RecyclingList.java37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/main/java/makamys/neodymium/util/RecyclingList.java b/src/main/java/makamys/neodymium/util/RecyclingList.java
new file mode 100644
index 0000000..ea240bb
--- /dev/null
+++ b/src/main/java/makamys/neodymium/util/RecyclingList.java
@@ -0,0 +1,37 @@
+package makamys.neodymium.util;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.function.Supplier;
+
+public class RecyclingList<T> {
+
+ private Supplier<T> constructor;
+
+ int nextIndex;
+ private List<T> list;
+
+ public RecyclingList(Supplier<T> constructor) {
+ this.constructor = constructor;
+ this.list = new ArrayList<T>();
+ }
+
+ public T get(int i) {
+ while(list.size() <= i) {
+ list.add(constructor.get());
+ }
+ return list.get(i);
+ }
+
+ public T next() {
+ return get(nextIndex++);
+ }
+
+ public void reset() {
+ nextIndex = 0;
+ }
+
+ public List<T> getAsList() {
+ return list.subList(0, nextIndex);
+ }
+}