From 99c32e98639ac7b9b12eb8514fe9c89f12f0f857 Mon Sep 17 00:00:00 2001 From: nathan Date: Thu, 2 Oct 2014 01:45:30 +0200 Subject: [PATCH] tweaked fastObjectPool --- .../util/{ => objectPool}/FastObjectPool.java | 127 +++++++----------- .../src/dorkbox/util/storage/Metadata.java | 8 +- .../src/dorkbox/util/storage/Storage.java | 4 +- .../src/dorkbox/util/storage/StorageBase.java | 7 +- 4 files changed, 58 insertions(+), 88 deletions(-) rename Dorkbox-Util/src/dorkbox/util/{ => objectPool}/FastObjectPool.java (59%) diff --git a/Dorkbox-Util/src/dorkbox/util/FastObjectPool.java b/Dorkbox-Util/src/dorkbox/util/objectPool/FastObjectPool.java similarity index 59% rename from Dorkbox-Util/src/dorkbox/util/FastObjectPool.java rename to Dorkbox-Util/src/dorkbox/util/objectPool/FastObjectPool.java index e1c003e..9d388ca 100644 --- a/Dorkbox-Util/src/dorkbox/util/FastObjectPool.java +++ b/Dorkbox-Util/src/dorkbox/util/objectPool/FastObjectPool.java @@ -1,4 +1,4 @@ -package dorkbox.util; +package dorkbox.util.objectPool; /* * @@ -22,13 +22,36 @@ package dorkbox.util; import java.lang.reflect.Field; import java.security.AccessController; import java.security.PrivilegedExceptionAction; -import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.locks.ReentrantLock; import sun.misc.Unsafe; public class FastObjectPool { + public static final Unsafe THE_UNSAFE; + static { + try { + final PrivilegedExceptionAction action = new PrivilegedExceptionAction() { + @Override + public Unsafe run() throws Exception { + Class unsafeClass = sun.misc.Unsafe.class; + Field theUnsafe = unsafeClass.getDeclaredField("theUnsafe"); + theUnsafe.setAccessible(true); + Object unsafeObject = theUnsafe.get(null); + if (unsafeClass.isInstance(unsafeObject)) { + return unsafeClass.cast(unsafeObject); + } + + throw new NoSuchFieldError("the Unsafe"); + } + }; + + THE_UNSAFE = AccessController.doPrivileged(action); + } + catch (Exception e) { + throw new RuntimeException("Unable to load unsafe", e); + } + } private Holder[] objects; @@ -43,21 +66,23 @@ public class FastObjectPool { public ReentrantLock lock = new ReentrantLock(); private ThreadLocal> localValue = new ThreadLocal<>(); - @SuppressWarnings("unchecked") - public FastObjectPool(PoolFactory factory , int size) - { + public FastObjectPool(PoolFactory factory, int size) { - int newSize=1; - while(newSize[] stuff = new Holder[size]; + this.objects = stuff; + + for (int x=0;x(factory.create()); } + this.mask = size-1; this.releasePointer = size; this.BASE = THE_UNSAFE.arrayBaseOffset(Holder[].class); @@ -65,29 +90,23 @@ public class FastObjectPool { this.ASHIFT = 31 - Integer.numberOfLeadingZeros((int) this.INDEXSCALE); } - public Holder take() - { + public Holder take() { int localTakePointer; Holder localObject = this.localValue.get(); - if(localObject!=null) - { - if(localObject.state.compareAndSet(Holder.FREE, Holder.USED)) - { + if (localObject != null) { + if(localObject.state.compareAndSet(Holder.FREE, Holder.USED)) { return localObject; } } - while(this.releasePointer != (localTakePointer=this.takePointer) ) - { + while (this.releasePointer != (localTakePointer=this.takePointer)) { int index = localTakePointer & this.mask; Holder holder = this.objects[index]; //if(holder!=null && THE_UNSAFE.compareAndSwapObject(objects, (index*INDEXSCALE)+BASE, holder, null)) - if(holder!=null && THE_UNSAFE.compareAndSwapObject(this.objects, (index< { return null; } - public void release(Holder object) throws InterruptedException - { + public void release(Holder object) throws InterruptedException { this.lock.lockInterruptibly(); - try{ + + try { int localValue=this.releasePointer; //long index = ((localValue & mask) * INDEXSCALE ) + BASE; long index = ((localValue & this.mask)< - { - private T value; - public static final int FREE=0; - public static final int USED=1; - - private AtomicInteger state = new AtomicInteger(FREE); - public Holder(T value) - { - this.value = value; - } - - public T getValue() { - return this.value; - } - } - - public static interface PoolFactory - { - public T create(); - } - - public static final Unsafe THE_UNSAFE; - static - { - try - { - final PrivilegedExceptionAction action = new PrivilegedExceptionAction() - { - @Override - public Unsafe run() throws Exception - { - Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe"); - theUnsafe.setAccessible(true); - return (Unsafe) theUnsafe.get(null); - } - }; - - THE_UNSAFE = AccessController.doPrivileged(action); - } - catch (Exception e) - { - throw new RuntimeException("Unable to load unsafe", e); - } - } } \ No newline at end of file diff --git a/Dorkbox-Util/src/dorkbox/util/storage/Metadata.java b/Dorkbox-Util/src/dorkbox/util/storage/Metadata.java index 7994ee4..2d5fe8e 100644 --- a/Dorkbox-Util/src/dorkbox/util/storage/Metadata.java +++ b/Dorkbox-Util/src/dorkbox/util/storage/Metadata.java @@ -221,18 +221,18 @@ public class Metadata { lock.release(); // Sys.printArray(buf, buf.length, false, 0); - return buf; } /** * Reads the record data for the given record header. */ - @SuppressWarnings("unchecked") + T readData(Kryo kryo, InflaterInputStream inputStream) throws IOException { Input input = new Input(inputStream, 1024); // read 1024 at a time - Object readObject = kryo.readClassAndObject(input); - return (T) readObject; + @SuppressWarnings("unchecked") + T readObject = (T) kryo.readClassAndObject(input); + return readObject; } /** diff --git a/Dorkbox-Util/src/dorkbox/util/storage/Storage.java b/Dorkbox-Util/src/dorkbox/util/storage/Storage.java index bac6bf9..22e668e 100644 --- a/Dorkbox-Util/src/dorkbox/util/storage/Storage.java +++ b/Dorkbox-Util/src/dorkbox/util/storage/Storage.java @@ -324,14 +324,14 @@ public class Storage { } /** - * Reads a object using the default (blank) key + * Reads a object using the default (blank) key, and casts it to the expected class */ public final T get() { return get0(this.defaultKey); } /** - * Reads a object using the specific key. + * Reads a object using the specific key, and casts it to the expected class */ public final T get(String key) { ByteArrayWrapper wrap = wrap(key); diff --git a/Dorkbox-Util/src/dorkbox/util/storage/StorageBase.java b/Dorkbox-Util/src/dorkbox/util/storage/StorageBase.java index ce7c390..2a34434 100644 --- a/Dorkbox-Util/src/dorkbox/util/storage/StorageBase.java +++ b/Dorkbox-Util/src/dorkbox/util/storage/StorageBase.java @@ -140,10 +140,10 @@ public class StorageBase { this.kryo.setRegistrationRequired(false); this.deflater = new Deflater(7, true); - this.outputStream = new DeflaterOutputStream(new FileOutputStream(this.file.getFD()), this.deflater, 1024, true); + this.outputStream = new DeflaterOutputStream(new FileOutputStream(this.file.getFD()), this.deflater, 65536, true); this.inflater = new Inflater(true); - this.inputStream = new InflaterInputStream(new FileInputStream(this.file.getFD()), this.inflater, 1024); + this.inputStream = new InflaterInputStream(new FileInputStream(this.file.getFD()), this.inflater, 65536); this.weight = .5F; this.memoryIndex = new ConcurrentHashMap(this.numberOfRecords); @@ -250,8 +250,7 @@ public class StorageBase { this.inflater.reset(); this.file.seek(meta.dataPointer); - @SuppressWarnings("unchecked") - T readRecordData = (T) meta.readData(this.kryo, this.inputStream); + T readRecordData = meta.readData(this.kryo, this.inputStream); if (readRecordData != null) { // now stuff it into our reference cache for future lookups!