summaryrefslogtreecommitdiff
path: root/NET/worlds/scape/Recycler.java
diff options
context:
space:
mode:
authorFuwn <[email protected]>2026-02-12 22:33:32 -0800
committerFuwn <[email protected]>2026-02-12 22:33:32 -0800
commitc7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9 (patch)
treedf9f48bf128a6c0186a8e91857d6ff30fe0e9f18 /NET/worlds/scape/Recycler.java
downloadworldsplayer-c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9.tar.xz
worldsplayer-c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9.zip
Initial commit
Diffstat (limited to 'NET/worlds/scape/Recycler.java')
-rw-r--r--NET/worlds/scape/Recycler.java75
1 files changed, 75 insertions, 0 deletions
diff --git a/NET/worlds/scape/Recycler.java b/NET/worlds/scape/Recycler.java
new file mode 100644
index 0000000..cab50e4
--- /dev/null
+++ b/NET/worlds/scape/Recycler.java
@@ -0,0 +1,75 @@
+package NET.worlds.scape;
+
+import NET.worlds.console.Main;
+import NET.worlds.console.MainCallback;
+
+public class Recycler implements MainCallback {
+ private static int minCapacity = 50;
+ private Object[] list = new Object[minCapacity];
+ private int numFilled = 0;
+ private int numAllocked = 0;
+ private int maxAllockedRecently = 0;
+ private int howRecently = 0;
+
+ Recycler() {
+ Main.register(this);
+ }
+
+ private void resizeTo(int newCapacity) {
+ assert newCapacity > this.numAllocked;
+
+ if (newCapacity < minCapacity) {
+ newCapacity = minCapacity;
+ }
+
+ if (newCapacity < this.list.length && this.list.length < 2 * newCapacity) {
+ while (this.numFilled > newCapacity) {
+ this.list[--this.numFilled] = null;
+ }
+ } else {
+ Object[] newList = new Object[newCapacity];
+
+ try {
+ System.arraycopy(this.list, 0, newList, 0, this.numAllocked);
+ } catch (Exception var4) {
+ throw new Error(var4.toString());
+ }
+
+ this.list = newList;
+ this.numFilled = this.numAllocked;
+ }
+ }
+
+ @Override
+ public void mainCallback() {
+ if (this.numAllocked > this.maxAllockedRecently) {
+ this.maxAllockedRecently = this.numAllocked;
+ }
+
+ if (++this.howRecently == 1000) {
+ int newCapacity = this.maxAllockedRecently + minCapacity;
+ if (newCapacity < this.list.length) {
+ this.resizeTo(newCapacity);
+ }
+
+ this.howRecently = 0;
+ this.maxAllockedRecently = this.numAllocked;
+ }
+
+ this.numAllocked = 0;
+ }
+
+ public Object alloc() {
+ assert Main.isMainThread();
+
+ return this.numAllocked == this.numFilled ? null : this.list[this.numAllocked++];
+ }
+
+ public void recycle(Object o) {
+ if (this.numFilled == this.list.length) {
+ this.resizeTo(this.list.length * 2);
+ }
+
+ this.list[this.numFilled++] = o;
+ }
+}