* Added AssetKey.clone()
* WeakRefCloneAssetCache will attempt to collect GCd assets more often by not relying WeakHashMap to do so git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9492 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
This commit is contained in:
parent
7b9b9905ef
commit
fd03d28d81
@ -43,7 +43,7 @@ import java.util.LinkedList;
|
|||||||
* look up a resource from a cache.
|
* look up a resource from a cache.
|
||||||
* This class should be immutable.
|
* This class should be immutable.
|
||||||
*/
|
*/
|
||||||
public class AssetKey<T> implements Savable {
|
public class AssetKey<T> implements Savable, Cloneable {
|
||||||
|
|
||||||
protected String name;
|
protected String name;
|
||||||
protected transient String folder;
|
protected transient String folder;
|
||||||
@ -57,6 +57,15 @@ public class AssetKey<T> implements Savable {
|
|||||||
public AssetKey(){
|
public AssetKey(){
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public AssetKey<T> clone() {
|
||||||
|
try {
|
||||||
|
return (AssetKey<T>) super.clone();
|
||||||
|
} catch (CloneNotSupportedException ex) {
|
||||||
|
throw new AssertionError();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected static String getExtension(String name) {
|
protected static String getExtension(String name) {
|
||||||
int idx = name.lastIndexOf('.');
|
int idx = name.lastIndexOf('.');
|
||||||
//workaround for filenames ending with xml and another dot ending before that (my.mesh.xml)
|
//workaround for filenames ending with xml and another dot ending before that (my.mesh.xml)
|
||||||
|
@ -2,9 +2,13 @@ package com.jme3.asset.cache;
|
|||||||
|
|
||||||
import com.jme3.asset.AssetKey;
|
import com.jme3.asset.AssetKey;
|
||||||
import com.jme3.asset.CloneableSmartAsset;
|
import com.jme3.asset.CloneableSmartAsset;
|
||||||
|
import java.lang.ref.PhantomReference;
|
||||||
|
import java.lang.ref.ReferenceQueue;
|
||||||
import java.lang.ref.WeakReference;
|
import java.lang.ref.WeakReference;
|
||||||
import java.util.ArrayDeque;
|
import java.util.ArrayList;
|
||||||
import java.util.WeakHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <codeWeakRefCloneAssetCache</code> caches cloneable assets in a weak-key
|
* <codeWeakRefCloneAssetCache</code> caches cloneable assets in a weak-key
|
||||||
@ -17,58 +21,110 @@ import java.util.WeakHashMap;
|
|||||||
*/
|
*/
|
||||||
public class WeakRefCloneAssetCache implements AssetCache {
|
public class WeakRefCloneAssetCache implements AssetCache {
|
||||||
|
|
||||||
private static final class SmartCachedAsset {
|
private static final Logger logger = Logger.getLogger(WeakRefAssetCache.class.getName());
|
||||||
|
|
||||||
|
private final ReferenceQueue<AssetKey> refQueue = new ReferenceQueue<AssetKey>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Maps cloned key to AssetRef which has a weak ref to the original
|
||||||
|
* key and a strong ref to the original asset.
|
||||||
|
*/
|
||||||
|
private final ConcurrentHashMap<AssetKey, AssetRef> smartCache
|
||||||
|
= new ConcurrentHashMap<AssetKey, AssetRef>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stored in the ReferenceQueue to find out when originalKey is collected
|
||||||
|
* by GC. Once collected, the clonedKey is used to remove the asset
|
||||||
|
* from the cache.
|
||||||
|
*/
|
||||||
|
private static final class KeyRef extends PhantomReference<AssetKey> {
|
||||||
|
|
||||||
|
AssetKey clonedKey;
|
||||||
|
|
||||||
|
public KeyRef(AssetKey originalKey, ReferenceQueue<AssetKey> refQueue) {
|
||||||
|
super(originalKey, refQueue);
|
||||||
|
clonedKey = originalKey.clone();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stores the original key and original asset.
|
||||||
|
* The asset info contains a cloneable asset (e.g. the original, from
|
||||||
|
* which all clones are made). Also a weak reference to the
|
||||||
|
* original key which is used when the clones are produced.
|
||||||
|
*/
|
||||||
|
private static final class AssetRef extends WeakReference<AssetKey> {
|
||||||
|
|
||||||
WeakReference<AssetKey> key;
|
|
||||||
CloneableSmartAsset asset;
|
CloneableSmartAsset asset;
|
||||||
|
|
||||||
public SmartCachedAsset(CloneableSmartAsset originalAsset, AssetKey originalKey) {
|
public AssetRef(CloneableSmartAsset originalAsset, AssetKey originalKey) {
|
||||||
this.key = new WeakReference<AssetKey>(originalKey);
|
super(originalKey);
|
||||||
this.asset = originalAsset;
|
this.asset = originalAsset;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private final WeakHashMap<AssetKey, SmartCachedAsset> smartCache
|
private final ThreadLocal<ArrayList<AssetKey>> assetLoadStack
|
||||||
= new WeakHashMap<AssetKey, SmartCachedAsset>();
|
= new ThreadLocal<ArrayList<AssetKey>>() {
|
||||||
|
|
||||||
private final ThreadLocal<ArrayDeque<AssetKey>> assetLoadStack
|
|
||||||
= new ThreadLocal<ArrayDeque<AssetKey>>() {
|
|
||||||
@Override
|
@Override
|
||||||
protected ArrayDeque<AssetKey> initialValue() {
|
protected ArrayList<AssetKey> initialValue() {
|
||||||
return new ArrayDeque<AssetKey>();
|
return new ArrayList<AssetKey>();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
public <T> void addToCache(AssetKey<T> key, T obj) {
|
private void removeCollectedAssets(){
|
||||||
|
int removedAssets = 0;
|
||||||
|
for (KeyRef ref; (ref = (KeyRef)refQueue.poll()) != null;){
|
||||||
|
// (Cannot use ref.get() since it was just collected by GC!)
|
||||||
|
AssetKey key = ref.clonedKey;
|
||||||
|
|
||||||
|
// Asset was collected, note that at this point the asset cache
|
||||||
|
// might not even have this asset anymore, it is OK.
|
||||||
|
if (smartCache.remove(key) != null){
|
||||||
|
removedAssets ++;
|
||||||
|
//System.out.println("WeakRefAssetCache: The asset " + ref.assetKey + " was purged from the cache");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (removedAssets >= 1) {
|
||||||
|
logger.log(Level.INFO, "WeakRefAssetCache: {0} assets were purged from the cache.", removedAssets);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> void addToCache(AssetKey<T> originalKey, T obj) {
|
||||||
|
// Make room for new asset
|
||||||
|
removeCollectedAssets();
|
||||||
|
|
||||||
CloneableSmartAsset asset = (CloneableSmartAsset) obj;
|
CloneableSmartAsset asset = (CloneableSmartAsset) obj;
|
||||||
|
|
||||||
// No circular references, since the original asset is
|
// No circular references, since the original asset is
|
||||||
// strongly referenced, we don't want the key strongly referenced.
|
// strongly referenced, we don't want the key strongly referenced.
|
||||||
asset.setKey(null);
|
asset.setKey(null);
|
||||||
|
|
||||||
// Place the asset in the cache with a weak ref to the key.
|
// Start tracking the collection of originalKey
|
||||||
synchronized (smartCache) {
|
// (this adds the KeyRef to the ReferenceQueue)
|
||||||
smartCache.put(key, new SmartCachedAsset(asset, key));
|
KeyRef ref = new KeyRef(originalKey, refQueue);
|
||||||
}
|
|
||||||
|
// Place the asset in the cache, but use a clone of
|
||||||
|
// the original key.
|
||||||
|
smartCache.put(ref.clonedKey, new AssetRef(asset, originalKey));
|
||||||
|
|
||||||
// Push the original key used to load the asset
|
// Push the original key used to load the asset
|
||||||
// so that it can be set on the clone later
|
// so that it can be set on the clone later
|
||||||
ArrayDeque<AssetKey> loadStack = assetLoadStack.get();
|
ArrayList<AssetKey> loadStack = assetLoadStack.get();
|
||||||
loadStack.push(key);
|
loadStack.add(originalKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T> void registerAssetClone(AssetKey<T> key, T clone) {
|
public <T> void registerAssetClone(AssetKey<T> key, T clone) {
|
||||||
ArrayDeque<AssetKey> loadStack = assetLoadStack.get();
|
ArrayList<AssetKey> loadStack = assetLoadStack.get();
|
||||||
((CloneableSmartAsset)clone).setKey(loadStack.pop());
|
((CloneableSmartAsset)clone).setKey(loadStack.remove(loadStack.size() - 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void notifyNoAssetClone() {
|
public void notifyNoAssetClone() {
|
||||||
ArrayDeque<AssetKey> loadStack = assetLoadStack.get();
|
ArrayList<AssetKey> loadStack = assetLoadStack.get();
|
||||||
loadStack.pop();
|
loadStack.remove(loadStack.size() - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T> T getFromCache(AssetKey<T> key) {
|
public <T> T getFromCache(AssetKey<T> key) {
|
||||||
SmartCachedAsset smartInfo;
|
AssetRef smartInfo;
|
||||||
synchronized (smartCache){
|
synchronized (smartCache){
|
||||||
smartInfo = smartCache.get(key);
|
smartInfo = smartCache.get(key);
|
||||||
}
|
}
|
||||||
@ -79,7 +135,7 @@ public class WeakRefCloneAssetCache implements AssetCache {
|
|||||||
// NOTE: Optimization so that registerAssetClone()
|
// NOTE: Optimization so that registerAssetClone()
|
||||||
// can check this and determine that the asset clone
|
// can check this and determine that the asset clone
|
||||||
// belongs to the asset retrieved here.
|
// belongs to the asset retrieved here.
|
||||||
AssetKey keyForTheClone = smartInfo.key.get();
|
AssetKey keyForTheClone = smartInfo.get();
|
||||||
if (keyForTheClone == null){
|
if (keyForTheClone == null){
|
||||||
// The asset was JUST collected by GC
|
// The asset was JUST collected by GC
|
||||||
// (between here and smartCache.get)
|
// (between here and smartCache.get)
|
||||||
@ -88,34 +144,32 @@ public class WeakRefCloneAssetCache implements AssetCache {
|
|||||||
|
|
||||||
// Prevent original key from getting collected
|
// Prevent original key from getting collected
|
||||||
// while an asset is loaded for it.
|
// while an asset is loaded for it.
|
||||||
ArrayDeque<AssetKey> loadStack = assetLoadStack.get();
|
ArrayList<AssetKey> loadStack = assetLoadStack.get();
|
||||||
loadStack.push(keyForTheClone);
|
loadStack.add(keyForTheClone);
|
||||||
|
|
||||||
return (T) smartInfo.asset;
|
return (T) smartInfo.asset;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean deleteFromCache(AssetKey key) {
|
public boolean deleteFromCache(AssetKey key) {
|
||||||
ArrayDeque<AssetKey> loadStack = assetLoadStack.get();
|
ArrayList<AssetKey> loadStack = assetLoadStack.get();
|
||||||
|
|
||||||
if (!loadStack.isEmpty()){
|
if (!loadStack.isEmpty()){
|
||||||
throw new UnsupportedOperationException("Cache cannot be modified"
|
throw new UnsupportedOperationException("Cache cannot be modified"
|
||||||
+ "while assets are being loaded");
|
+ "while assets are being loaded");
|
||||||
}
|
}
|
||||||
synchronized (smartCache) {
|
|
||||||
return smartCache.remove(key) != null;
|
return smartCache.remove(key) != null;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void clearCache() {
|
public void clearCache() {
|
||||||
ArrayDeque<AssetKey> loadStack = assetLoadStack.get();
|
ArrayList<AssetKey> loadStack = assetLoadStack.get();
|
||||||
|
|
||||||
if (!loadStack.isEmpty()){
|
if (!loadStack.isEmpty()){
|
||||||
throw new UnsupportedOperationException("Cache cannot be modified"
|
throw new UnsupportedOperationException("Cache cannot be modified"
|
||||||
+ "while assets are being loaded");
|
+ "while assets are being loaded");
|
||||||
}
|
}
|
||||||
synchronized (smartCache) {
|
|
||||||
smartCache.clear();
|
smartCache.clear();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user