-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add caching to sampleTerrain #6284
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
4820390
Add caching to sampleTerrain
40412b2
Merge branch 'master' into cache-sampleterrain
26892f1
LRUCache expiration
f615296
cleanup
a2b7a28
use `requestRenderFrame` instead of tying in to scene render loop [ci…
f8f2fa9
fix loop, change cache expiration [ci skip]
5d8d240
fix specs
0b74558
fix doc
6b38b9a
Merge branch 'master' into cache-sampleterrain
e5f2e7b
cleanup after review
fd1bc99
move private functions
b0c7575
fix indent
4ec3576
fix eslint
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
define([ | ||
'./defined', | ||
'./defineProperties', | ||
'./getTimestamp', | ||
'./DeveloperError', | ||
'./DoublyLinkedList' | ||
], function( | ||
defined, | ||
defineProperties, | ||
getTimestamp, | ||
DeveloperError, | ||
DoublyLinkedList) { | ||
'use strict'; | ||
|
||
/** | ||
* A cache for storing key-value pairs | ||
* @param {Number} [capacity] The capacity of the cache. If undefined, the size will be unlimited. | ||
* @param {Number} [expiration] The number of milliseconds before an item in the cache expires and will be discarded when LRUCache.prune is called. If undefined, items do not expire. | ||
* @alias LRUCache | ||
* @constructor | ||
* @private | ||
*/ | ||
function LRUCache(capacity, expiration) { | ||
this._list = new DoublyLinkedList(); | ||
this._hash = {}; | ||
this._hasCapacity = defined(capacity); | ||
this._capacity = capacity; | ||
|
||
this._hasExpiration = defined(expiration); | ||
this._expiration = expiration; | ||
this._interval = undefined; | ||
} | ||
|
||
defineProperties(LRUCache.prototype, { | ||
/** | ||
* Gets the cache length | ||
* @memberof LRUCache.prototype | ||
* @type {Number} | ||
* @readonly | ||
*/ | ||
length : { | ||
get : function() { | ||
return this._list.length; | ||
} | ||
} | ||
}); | ||
|
||
/** | ||
* Retrieves the value associated with the provided key. | ||
* | ||
* @param {String|Number} key The key whose value is to be retrieved. | ||
* @returns {Object} The associated value, or undefined if the key does not exist in the collection. | ||
*/ | ||
LRUCache.prototype.get = function(key) { | ||
//>>includeStart('debug', pragmas.debug); | ||
if (typeof key !== 'string' && typeof key !== 'number') { | ||
throw new DeveloperError('key is required to be a string or number.'); | ||
} | ||
//>>includeEnd('debug'); | ||
var list = this._list; | ||
var node = this._hash[key]; | ||
if (defined(node)) { | ||
list.moveToFront(node); | ||
var item = node.item; | ||
item.touch(); | ||
return item.value; | ||
} | ||
}; | ||
|
||
/** | ||
* Associates the provided key with the provided value. If the key already | ||
* exists, it is overwritten with the new value. | ||
* | ||
* @param {String|Number} key A unique identifier. | ||
* @param {Object} value The value to associate with the provided key. | ||
*/ | ||
LRUCache.prototype.set = function(key, value) { | ||
//>>includeStart('debug', pragmas.debug); | ||
if (typeof key !== 'string' && typeof key !== 'number') { | ||
throw new DeveloperError('key is required to be a string or number.'); | ||
} | ||
//>>includeEnd('debug'); | ||
var hash = this._hash; | ||
var list = this._list; | ||
|
||
var node = hash[key]; | ||
var item; | ||
if (!defined(node)) { | ||
item = new Item(key, value); | ||
node = list.addFront(item); | ||
hash[key] = node; | ||
if (this._hasExpiration) { | ||
LRUCache._checkExpiration(this); | ||
} | ||
if (this._hasCapacity && list.length > this._capacity) { | ||
var tail = list.tail; | ||
delete this._hash[tail.item.key]; | ||
list.remove(tail); | ||
} | ||
} else { | ||
item = node.item; | ||
item.value = value; | ||
item.touch(); | ||
list.moveToFront(node); | ||
} | ||
}; | ||
|
||
function prune(cache) { | ||
var currentTime = getTimestamp(); | ||
var pruneAfter = currentTime - cache._expiration; | ||
|
||
var list = cache._list; | ||
var node = list.tail; | ||
var index = list.length; | ||
while (defined(node) && node.item.timestamp < pruneAfter) { | ||
node = node.previous; | ||
index--; | ||
} | ||
|
||
if (node === list.tail) { | ||
return; | ||
} | ||
|
||
if (!defined(node)) { | ||
node = list.head; | ||
} else { | ||
node = node.next; | ||
} | ||
|
||
while (defined(node)) { | ||
delete cache._hash[node.item.key]; | ||
node = node.next; | ||
} | ||
|
||
list.removeAfter(index); | ||
} | ||
|
||
function checkExpiration(cache) { | ||
if (defined(cache._interval)) { | ||
return; | ||
} | ||
function loop() { | ||
if (!cache._hasExpiration || cache.length === 0) { | ||
clearInterval(cache._interval); | ||
cache._interval = undefined; | ||
return; | ||
} | ||
|
||
prune(cache); | ||
|
||
if (cache.length === 0) { | ||
clearInterval(cache._interval); | ||
cache._interval = undefined; | ||
} | ||
} | ||
cache._interval = setInterval(loop, 1000); | ||
} | ||
|
||
function Item(key, value) { | ||
this.key = key; | ||
this.value = value; | ||
this.timestamp = getTimestamp(); | ||
} | ||
|
||
Item.prototype.touch = function() { | ||
this.timestamp = getTimestamp(); | ||
}; | ||
|
||
//exposed for testing | ||
LRUCache._checkExpiration = checkExpiration; | ||
LRUCache._prune = prune; | ||
|
||
return LRUCache; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this was discussed and this is the best solution - if so please point me to it, but this is not how I originally suggested to implement this.
We generally want to centralize all the time-related cache logic so I suggested that we use the render loop to flush any caches just like we do for the shader cache. Are we sure this
setInterval
approach is better? I think explicit and consistent control of caches in our render loop (but still based on time stamp) would be more cohesive.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was some discussion here: #6284 (comment)
Since
sampleTerrain
doesn't have access toframeState
some of the options were:sampleTerrain
that tells it to flush the cache.sampleTerrain
usesrequestAnimationFrame
to hook into the render loop to flush the cache, makingsampleTerrain
self contained.sampleTerrain
usessetInterval
instead ofrequestAnimationFrame
.One benefit of the third approach (which I guess is opposite your original suggestion) is that
sampleTerrain
is not tied to the render loop at all, making it possible to use in Node.js or wherever else the Cesium engine isn't actually running.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I did not think about the standalone use case. I'm still not sure that a standalone API would implicitly cache and flush like this (imagine if an OpenGL driver did thinks like this); the user would likely have more fine-grained control and would be able to pass in a cache policy or whatever.
Still not convinced that having random caches with random set intervals is a good design in Cesium, but proceed as you see fit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think everyone agrees with this sentiment. In my opinion what Cesium really needs is a shared centralized cache (just like we also need a centralized worker pool).