Merge pull request #952 from eidyia/pr-guidCacheHWMbug
Eliminate CPU churning on cache highwater check
This commit is contained in:
commit
dd9f053ed3
@ -93,6 +93,7 @@ window.findPortalLatLng = function(guid) {
|
|||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
var cache = {};
|
var cache = {};
|
||||||
|
var cache_level = 0;
|
||||||
var GC_LIMIT = 5000; // run garbage collector when cache has more that 5000 items
|
var GC_LIMIT = 5000; // run garbage collector when cache has more that 5000 items
|
||||||
var GC_KEEP = 4000; // keep the 4000 most recent items
|
var GC_KEEP = 4000; // keep the 4000 most recent items
|
||||||
|
|
||||||
@ -128,13 +129,15 @@ window.findPortalLatLng = function(guid) {
|
|||||||
|
|
||||||
window.pushPortalGuidPositionCache = function(guid, latE6, lngE6) {
|
window.pushPortalGuidPositionCache = function(guid, latE6, lngE6) {
|
||||||
cache[latE6+","+lngE6] = [guid, Date.now()];
|
cache[latE6+","+lngE6] = [guid, Date.now()];
|
||||||
|
cache_level += 1;
|
||||||
|
|
||||||
if(Object.keys(cache).length > GC_LIMIT) {
|
if(cache_level > GC_LIMIT) {
|
||||||
Object.keys(cache) // get all latlngs
|
Object.keys(cache) // get all latlngs
|
||||||
.map(function(latlng) { return [latlng, cache[latlng][1]]; }) // map them to [latlng, timestamp]
|
.map(function(latlng) { return [latlng, cache[latlng][1]]; }) // map them to [latlng, timestamp]
|
||||||
.sort(function(a,b) { return b[1] - a[1]; }) // sort them
|
.sort(function(a,b) { return b[1] - a[1]; }) // sort them
|
||||||
.slice(GC_KEEP) // drop the MRU
|
.slice(GC_KEEP) // drop the MRU
|
||||||
.forEach(function(item) { delete cache[item[0]] }); // delete the rest
|
.forEach(function(item) { delete cache[item[0]] }); // delete the rest
|
||||||
|
cache_level = Object.keys(cache).length
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user