Skip to content

Commit

Permalink
ResourcePool : use ObjectMap for performance enhancement
Browse files Browse the repository at this point in the history
  • Loading branch information
exch-bms2 committed Mar 17, 2018
1 parent a3c52e8 commit f5bb6da
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions src/bms/player/beatoraja/ResourcePool.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package bms.player.beatoraja;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Disposable;
import com.badlogic.gdx.utils.ObjectMap;

/**
* リソースプール。イメージデータやオーディオデータ等の読み込みコストが大きく、
Expand All @@ -23,7 +21,7 @@ public abstract class ResourcePool<K, V> implements Disposable {
/**
* リソース
*/
private Map<K, ResourceCacheElement<V>> image = new HashMap<K, ResourceCacheElement<V>> ();
private ObjectMap<K, ResourceCacheElement<V>> image = new ObjectMap<K, ResourceCacheElement<V>> ();

public ResourcePool(int maxgen) {
this.maxgen = maxgen;
Expand Down Expand Up @@ -61,32 +59,39 @@ public V get(K key) {
return ie != null ? ie.image : null;
}

private final Array<K> removes = new Array<K>();

/**
* 世代数を進め、最大世代数を経過したリソースを開放する
*/
public void disposeOld() {
for(Entry<K, ResourceCacheElement<V>> entry : image.entrySet().toArray(new Entry[image.size()])) {
ResourceCacheElement<V> ie = entry.getValue();
removes.clear();
for(ObjectMap.Entry<K, ResourceCacheElement<V>> entry : image) {
ResourceCacheElement<V> ie = entry.value;
if(ie.gen == maxgen) {
dispose(ie.image);
image.remove(entry.getKey());
removes.add(entry.key);
} else {
ie.gen++;
}
}

for(K key : removes) {
image.remove(key);
}
}

/**
* 現在のリソースの要素数を返す。
* @return リソースの
*/
public int size() {
return image.size();
return image.size;
}

public void dispose() {
for(Map.Entry<K, ResourceCacheElement<V>> e : image.entrySet()) {
dispose(e.getValue().image);
for(ObjectMap.Entry<K, ResourceCacheElement<V>> entry : image) {
dispose(entry.value.image);
}
image.clear();
}
Expand All @@ -113,7 +118,7 @@ private static class ResourceCacheElement<R> {
/**
* リソース
*/
public R image;
public final R image;
/**
* 世代
*/
Expand Down

0 comments on commit f5bb6da

Please sign in to comment.