From c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Thu, 12 Feb 2026 22:33:32 -0800 Subject: Initial commit --- NET/worlds/scape/Recycler.java | 75 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 NET/worlds/scape/Recycler.java (limited to 'NET/worlds/scape/Recycler.java') 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; + } +} -- cgit v1.2.3