use separate locks for nodes/ways/areas render instructions cache

This commit is contained in:
Hannes Janetzek 2013-01-30 03:56:02 +01:00
parent bb39069af2
commit 836f0ea82b

View File

@ -28,7 +28,7 @@ import android.graphics.Color;
* A RenderTheme defines how ways and nodes are drawn. * A RenderTheme defines how ways and nodes are drawn.
*/ */
public class RenderTheme { public class RenderTheme {
private final static String TAG = RenderTheme.class.getName(); //private final static String TAG = RenderTheme.class.getName();
private static final int MATCHING_CACHE_SIZE = 512; private static final int MATCHING_CACHE_SIZE = 512;
private static final int RENDER_THEME_VERSION = 1; private static final int RENDER_THEME_VERSION = 1;
@ -84,9 +84,17 @@ public class RenderTheme {
private final int mMapBackground; private final int mMapBackground;
private final ArrayList<Rule> mRulesList; private final ArrayList<Rule> mRulesList;
private final LRUCache<MatchingCacheKey, RenderInstructionItem> mMatchingCacheNodes; private final LRUCache<MatchingCacheKey, RenderInstructionItem> mNodesCache;
private final LRUCache<MatchingCacheKey, RenderInstructionItem> mMatchingCacheWay; private final LRUCache<MatchingCacheKey, RenderInstructionItem> mWayCache;
private final LRUCache<MatchingCacheKey, RenderInstructionItem> mMatchingCacheArea; private final LRUCache<MatchingCacheKey, RenderInstructionItem> mAreaCache;
private MatchingCacheKey mAreaCacheKey = new MatchingCacheKey();
private MatchingCacheKey mWayCacheKey = new MatchingCacheKey();
private MatchingCacheKey mNodeCacheKey = new MatchingCacheKey();
private ArrayList<RenderInstruction> mWayInstructionList = new ArrayList<RenderInstruction>(4);
private ArrayList<RenderInstruction> mAreaInstructionList = new ArrayList<RenderInstruction>(4);
private ArrayList<RenderInstruction> mNodeInstructionList = new ArrayList<RenderInstruction>(4);
class RenderInstructionItem { class RenderInstructionItem {
RenderInstructionItem next; RenderInstructionItem next;
@ -100,11 +108,11 @@ public class RenderTheme {
mBaseTextSize = baseTextSize; mBaseTextSize = baseTextSize;
mRulesList = new ArrayList<Rule>(); mRulesList = new ArrayList<Rule>();
mMatchingCacheNodes = new LRUCache<MatchingCacheKey, RenderInstructionItem>( mNodesCache = new LRUCache<MatchingCacheKey, RenderInstructionItem>(
MATCHING_CACHE_SIZE); MATCHING_CACHE_SIZE);
mMatchingCacheWay = new LRUCache<MatchingCacheKey, RenderInstructionItem>( mWayCache = new LRUCache<MatchingCacheKey, RenderInstructionItem>(
MATCHING_CACHE_SIZE); MATCHING_CACHE_SIZE);
mMatchingCacheArea = new LRUCache<MatchingCacheKey, RenderInstructionItem>( mAreaCache = new LRUCache<MatchingCacheKey, RenderInstructionItem>(
MATCHING_CACHE_SIZE); MATCHING_CACHE_SIZE);
} }
@ -113,9 +121,9 @@ public class RenderTheme {
* resources. * resources.
*/ */
public void destroy() { public void destroy() {
mMatchingCacheNodes.clear(); mNodesCache.clear();
mMatchingCacheArea.clear(); mAreaCache.clear();
mMatchingCacheWay.clear(); mWayCache.clear();
for (int i = 0, n = mRulesList.size(); i < n; ++i) { for (int i = 0, n = mRulesList.size(); i < n; ++i) {
mRulesList.get(i).onDestroy(); mRulesList.get(i).onDestroy();
@ -153,7 +161,7 @@ public class RenderTheme {
* ... * ...
* @return ... * @return ...
*/ */
public synchronized RenderInstruction[] matchNode(IRenderCallback renderCallback, public RenderInstruction[] matchNode(IRenderCallback renderCallback,
Tag[] tags, byte zoomLevel) { Tag[] tags, byte zoomLevel) {
// list of renderinsctruction items in cache // list of renderinsctruction items in cache
@ -162,12 +170,14 @@ public class RenderTheme {
// the item matching tags and zoomlevel // the item matching tags and zoomlevel
RenderInstructionItem ri = null; RenderInstructionItem ri = null;
MatchingCacheKey matchingCacheKey = new MatchingCacheKey(tags); synchronized (mNodesCache) {
boolean found = mMatchingCacheNodes.containsKey(matchingCacheKey); MatchingCacheKey cacheKey = mNodeCacheKey; //new MatchingCacheKey(tags);
cacheKey.set(tags);
boolean found = mNodesCache.containsKey(cacheKey);
int zoomMask = 1 << zoomLevel; int zoomMask = 1 << zoomLevel;
if (found) { if (found) {
ris = mMatchingCacheNodes.get(matchingCacheKey); ris = mNodesCache.get(cacheKey);
for (ri = ris; ri != null; ri = ri.next) for (ri = ris; ri != null; ri = ri.next)
if ((ri.zoom & zoomMask) != 0) if ((ri.zoom & zoomMask) != 0)
@ -177,7 +187,7 @@ public class RenderTheme {
if (ri == null) { if (ri == null) {
// cache miss // cache miss
List<RenderInstruction> matches = mMatchingList; List<RenderInstruction> matches = mNodeInstructionList;
matches.clear(); matches.clear();
for (int i = 0, n = mRulesList.size(); i < n; ++i) for (int i = 0, n = mRulesList.size(); i < n; ++i)
mRulesList.get(i).matchNode(renderCallback, tags, zoomLevel, matches); mRulesList.get(i).matchNode(renderCallback, tags, zoomLevel, matches);
@ -227,10 +237,10 @@ public class RenderTheme {
if (ris != null) if (ris != null)
ris.next = ri; ris.next = ri;
else else
mMatchingCacheNodes.put(matchingCacheKey, ri); mNodesCache.put(new MatchingCacheKey(cacheKey), ri);
}
} }
} }
if (ri.list != null) if (ri.list != null)
render(renderCallback, ri.list, tags); render(renderCallback, ri.list, tags);
@ -238,10 +248,8 @@ public class RenderTheme {
} }
private int missCnt = 0; //private int missCnt = 0;
private int hitCnt = 0; //private int hitCnt = 0;
private MatchingCacheKey mCacheKey = new MatchingCacheKey();
private ArrayList<RenderInstruction> mMatchingList = new ArrayList<RenderInstruction>(4);
/** /**
* Matches a way with the given parameters against this RenderTheme. * Matches a way with the given parameters against this RenderTheme.
@ -258,7 +266,7 @@ public class RenderTheme {
* ... * ...
* @return currently processed render instructions * @return currently processed render instructions
*/ */
public synchronized RenderInstruction[] matchWay(IRenderCallback renderCallback, public RenderInstruction[] matchWay(IRenderCallback renderCallback,
Tag[] tags, byte zoomLevel, boolean closed, boolean render) { Tag[] tags, byte zoomLevel, boolean closed, boolean render) {
// list of renderinsctruction items in cache // list of renderinsctruction items in cache
@ -266,19 +274,25 @@ public class RenderTheme {
// the item matching tags and zoomlevel // the item matching tags and zoomlevel
RenderInstructionItem ri = null; RenderInstructionItem ri = null;
List<RenderInstruction> matches;
LRUCache<MatchingCacheKey, RenderInstructionItem> matchingCache; LRUCache<MatchingCacheKey, RenderInstructionItem> matchingCache;
MatchingCacheKey matchingCacheKey; MatchingCacheKey cacheKey;
if (closed) { if (closed) {
matchingCache = mMatchingCacheArea; matchingCache = mAreaCache;
cacheKey = mAreaCacheKey;
matches = mAreaInstructionList;
} else { } else {
matchingCache = mMatchingCacheWay; matchingCache = mWayCache;
cacheKey = mWayCacheKey;
matches = mWayInstructionList;
} }
int zoomMask = 1 << zoomLevel; int zoomMask = 1 << zoomLevel;
mCacheKey.set(tags); synchronized (matchingCache) {
ris = matchingCache.get(mCacheKey); cacheKey.set(tags);
ris = matchingCache.get(cacheKey);
for (ri = ris; ri != null; ri = ri.next) for (ri = ris; ri != null; ri = ri.next)
if ((ri.zoom & zoomMask) != 0) if ((ri.zoom & zoomMask) != 0)
@ -290,7 +304,7 @@ public class RenderTheme {
//Log.d(TAG, missCnt++ + " / " + hitCnt + " Cache Miss"); //Log.d(TAG, missCnt++ + " / " + hitCnt + " Cache Miss");
int c = (closed ? Closed.YES : Closed.NO); int c = (closed ? Closed.YES : Closed.NO);
List<RenderInstruction> matches = mMatchingList; //List<RenderInstruction> matches = mMatchingList;
matches.clear(); matches.clear();
for (int i = 0, n = mRulesList.size(); i < n; ++i) for (int i = 0, n = mRulesList.size(); i < n; ++i)
@ -345,11 +359,10 @@ public class RenderTheme {
ris.next = ri; ris.next = ri;
} }
else { else {
matchingCacheKey = new MatchingCacheKey(mCacheKey); matchingCache.put(new MatchingCacheKey(cacheKey), ri);
matchingCache.put(matchingCacheKey, ri); }
} }
} }
} }
if (render && ri.list != null) { if (render && ri.list != null) {
for (int i = 0, n = ri.list.length; i < n; i++) for (int i = 0, n = ri.list.length; i < n; i++)