Bläddra i källkod

Merge branch 'master' of github.com:NASA-AMMOS/3DTilesRendererJS

Garrett Johnson 5 år sedan
förälder
incheckning
775419fa92
3 ändrade filer med 33 tillägg och 30 borttagningar
  1. 4 0
      CHANGELOG.md
  2. 2 2
      TESTCASES.md
  3. 27 28
      src/utilities/LRUCache.js

+ 4 - 0
CHANGELOG.md

@@ -20,6 +20,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
 - Tiles not rendering if an empty tile is encountered.
 - Tiles not rendering if an empty tile is encountered.
 - Child tiles not rendering if a parent tile content failed to load.
 - Child tiles not rendering if a parent tile content failed to load.
 
 
+### Changed
+
+- Improved `update` function performance by deferring LRUCache array update.
+
 ## [0.1.2] - 2020-06-08
 ## [0.1.2] - 2020-06-08
 ### Changed
 ### Changed
 
 

+ 2 - 2
TESTCASES.md

@@ -84,14 +84,14 @@ tiles.traverse( tile => {
   str += ' : ';
   str += ' : ';
   str += tile.__loadingState;
   str += tile.__loadingState;
   console.log( str )
   console.log( str )
-	return tile.__isLeaf;
+  return tile.__isLeaf;
 
 
 } );
 } );
 ```
 ```
 
 
 #### expected
 #### expected
 
 
-Verify that no parent tiles have loaded ( `__loadingState = 0` ) if they have a threshold above 18 ( `( target + 1 ) * threshold` ).
+Verify that no parent tiles have loaded ( `__loadingState` should equal `0` ) if they have a threshold above 18 ( `( target + 1 ) * threshold` ).
 
 
 ## Verify tiles do not display past the maxDepth threshold
 ## Verify tiles do not display past the maxDepth threshold
 
 

+ 27 - 28
src/utilities/LRUCache.js

@@ -14,13 +14,19 @@ class LRUCache {
 		this.minSize = 600;
 		this.minSize = 600;
 		this.unloadPercent = 0.05;
 		this.unloadPercent = 0.05;
 
 
-		this.usedSet = new Set();
-		this.itemSet = new Set();
+		// "itemSet" doubles as both the list of the full set of items currently
+		// stored in the cache (keys) as well as a map to the time the item was last
+		// used so it can be sorted appropriately.
+		this.itemSet = new Map();
 		this.itemList = [];
 		this.itemList = [];
+		this.usedSet = new Set();
 		this.callbacks = new Map();
 		this.callbacks = new Map();
 
 
 		this.unloadPriorityCallback = null;
 		this.unloadPriorityCallback = null;
 
 
+		const itemSet = this.itemSet;
+		this.defaultPriorityCallback = item => itemSet.get( item );
+
 	}
 	}
 
 
 	// Returns whether or not the cache has reached the maximum size
 	// Returns whether or not the cache has reached the maximum size
@@ -50,7 +56,7 @@ class LRUCache {
 		const callbacks = this.callbacks;
 		const callbacks = this.callbacks;
 		itemList.push( item );
 		itemList.push( item );
 		usedSet.add( item );
 		usedSet.add( item );
-		itemSet.add( item );
+		itemSet.set( item, Date.now() );
 		callbacks.set( item, removeCb );
 		callbacks.set( item, removeCb );
 
 
 		return true;
 		return true;
@@ -88,10 +94,7 @@ class LRUCache {
 		const usedSet = this.usedSet;
 		const usedSet = this.usedSet;
 		if ( itemSet.has( item ) && ! usedSet.has( item ) ) {
 		if ( itemSet.has( item ) && ! usedSet.has( item ) ) {
 
 
-			const itemList = this.itemList;
-			const index = itemList.indexOf( item );
-			itemList.splice( index, 1 );
-			itemList.push( item );
+			itemSet.set( item, Date.now() );
 			usedSet.add( item );
 			usedSet.add( item );
 
 
 		}
 		}
@@ -116,38 +119,34 @@ class LRUCache {
 		const callbacks = this.callbacks;
 		const callbacks = this.callbacks;
 		const unused = itemList.length - usedSet.size;
 		const unused = itemList.length - usedSet.size;
 		const excess = itemList.length - targetSize;
 		const excess = itemList.length - targetSize;
-		const unloadPriorityCallback = this.unloadPriorityCallback;
+		const unloadPriorityCallback = this.unloadPriorityCallback || this.defaultPriorityCallback;
 
 
 		if ( excess > 0 && unused > 0 ) {
 		if ( excess > 0 && unused > 0 ) {
 
 
-			if ( unloadPriorityCallback ) {
+			// used items should be at the end of the array
+			itemList.sort( ( a, b ) => {
 
 
-				// used items should be at the end of the array
-				itemList.sort( ( a, b ) => {
+				const usedA = usedSet.has( a );
+				const usedB = usedSet.has( b );
+				if ( usedA && usedB ) {
 
 
-					const usedA = usedSet.has( a );
-					const usedB = usedSet.has( b );
-					if ( usedA && usedB ) {
+					// If they're both used then don't bother moving them
+					return 0;
 
 
-						// If they're both used then don't bother moving them
-						return 0;
+				} else if ( ! usedA && ! usedB ) {
 
 
-					} else if ( ! usedA && ! usedB ) {
+					// Use the sort function otherwise
+					// higher priority should be further to the left
+					return unloadPriorityCallback( b ) - unloadPriorityCallback( a );
 
 
-						// Use the sort function otherwise
-						// higher priority should be further to the left
-						return unloadPriorityCallback( b ) - unloadPriorityCallback( a );
+				} else {
 
 
-					} else {
+					// If one is used and the other is not move the used one towards the end of the array
+					return usedA ? 1 : - 1;
 
 
-						// If one is used and the other is not move the used one towards the end of the array
-						return usedA ? 1 : - 1;
-
-					}
-
-				} );
+				}
 
 
-			}
+			} );
 
 
 			// address corner cases where the minSize might be zero or smaller than maxSize - minSize,
 			// address corner cases where the minSize might be zero or smaller than maxSize - minSize,
 			// which would result in a very small or no items being unloaded.
 			// which would result in a very small or no items being unloaded.