reuse RenderTheme cache key

This commit is contained in:
Hannes Janetzek 2012-09-07 21:13:24 +02:00
parent 9b697cc05d
commit f01f5c7bfa
2 changed files with 45 additions and 10 deletions

View File

@ -17,9 +17,12 @@ package org.mapsforge.android.rendertheme;
import org.mapsforge.core.Tag;
class MatchingCacheKey {
private final int mHashCodeValue;
final Tag[] mTags;
final byte mZoomLevel;
int mHashCodeValue;
Tag[] mTags;
byte mZoomLevel;
MatchingCacheKey() {
}
MatchingCacheKey(Tag[] tags, byte zoomLevel) {
mTags = tags;
@ -27,13 +30,37 @@ class MatchingCacheKey {
mHashCodeValue = calculateHashCode();
}
MatchingCacheKey(MatchingCacheKey key) {
mTags = key.mTags;
mZoomLevel = key.mZoomLevel;
mHashCodeValue = key.mHashCodeValue;
}
void set(Tag[] tags, byte zoomLevel) {
mTags = tags;
mZoomLevel = zoomLevel;
int result = 7;
for (int i = 0, n = mTags.length; i < n; i++) {
if (mTags[i] == null)
break;
result = 31 * result + mTags[i].hashCode();
}
result = 31 * result + mZoomLevel;
mHashCodeValue = result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
} else if (!(obj instanceof MatchingCacheKey)) {
return false;
}
// else if (!(obj instanceof MatchingCacheKey)) {
// return false;
// }
MatchingCacheKey other = (MatchingCacheKey) obj;
if (mZoomLevel != other.mZoomLevel)

View File

@ -172,6 +172,10 @@ public class RenderTheme {
}
private int missCnt = 0;
private int hitCnt = 0;
private MatchingCacheKey mCacheKey = new MatchingCacheKey();
/**
* Matches a way with the given parameters against this RenderTheme.
*
@ -201,13 +205,17 @@ public class RenderTheme {
matchingCache = mMatchingCacheWay;
}
matchingCacheKey = new MatchingCacheKey(tags, zoomLevel);
boolean found = matchingCache.containsKey(matchingCacheKey);
mCacheKey.set(tags, zoomLevel);
renderInstructions = matchingCache.get(mCacheKey);
if (renderInstructions != null) {
// Log.d("RenderTheme", hitCnt++ + "Cache Hit");
} else if (!matchingCache.containsKey(mCacheKey)) {
matchingCacheKey = new MatchingCacheKey(mCacheKey);
if (found) {
renderInstructions = matchingCache.get(matchingCacheKey);
} else {
// cache miss
// Log.d("RenderTheme", missCnt++ + " Cache Miss");
int c = (closed ? Closed.YES : Closed.NO);
List<RenderInstruction> matchingList = new ArrayList<RenderInstruction>(4);
for (int i = 0, n = mRulesList.size(); i < n; ++i) {