Skip to content

Commit

Permalink
refactor(cache): use LRUCache instead of own cache
Browse files Browse the repository at this point in the history
BREAKING CHANGE:
- remove Source#onParsedFile callback
  • Loading branch information
ftoromanoff authored and Desplandis committed Jan 31, 2025
1 parent fdd2bd9 commit 6d12fde
Show file tree
Hide file tree
Showing 26 changed files with 493 additions and 770 deletions.
34 changes: 20 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"copc": "^0.0.6",
"earcut": "^3.0.0",
"js-priority-queue": "^0.1.5",
"lru-cache": "^11.0.1",
"pbf": "^4.0.1",
"shpjs": "^6.1.0",
"threads": "^1.7.0"
Expand Down
3 changes: 0 additions & 3 deletions src/Core/MainLoop.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ function updateElements(context, geometryLayer, elements) {
for (const attachedLayer of geometryLayer.attachedLayers) {
if (attachedLayer.ready) {
attachedLayer.update(context, attachedLayer, sub.element, sub.parent);
attachedLayer.cache.flush();
}
}
} else if (sub.elements) {
Expand All @@ -67,7 +66,6 @@ function updateElements(context, geometryLayer, elements) {
for (const attachedLayer of geometryLayer.attachedLayers) {
if (attachedLayer.ready) {
attachedLayer.update(context, attachedLayer, sub.elements[i], sub.parent);
attachedLayer.cache.flush();
}
}
}
Expand Down Expand Up @@ -161,7 +159,6 @@ class MainLoop extends EventDispatcher {
}

// Clear the cache of expired resources
geometryLayer.cache.flush();

view.execFrameRequesters(MAIN_LOOP_EVENTS.AFTER_LAYER_UPDATE, dt, this.#updateLoopRestarted, geometryLayer);
}
Expand Down
11 changes: 6 additions & 5 deletions src/Core/Prefab/TileBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as THREE from 'three';
import { TileGeometry } from 'Core/TileGeometry';
import Cache from 'Core/Scheduler/Cache';
import { LRUCache } from 'lru-cache';
import { computeBuffers } from 'Core/Prefab/computeBufferTileGeometry';
import OBB from 'Renderer/OBB';
import type Extent from 'Core/Geographic/Extent';
Expand All @@ -10,7 +10,7 @@ const cacheBuffer = new Map<string, {
index: THREE.BufferAttribute,
uv: THREE.BufferAttribute,
}>();
const cacheTile = new Cache();
const cacheTile = new LRUCache<string, Promise<TileGeometry>>({ max: 500 });

export type GpuBufferAttributes = {
index: THREE.BufferAttribute | null;
Expand Down Expand Up @@ -84,13 +84,14 @@ export function newTileGeometry(
const bufferKey =
`${builder.crs}_${params.disableSkirt ? 0 : 1}_${params.segments}`;

let promiseGeometry = cacheTile.get(south, params.level, bufferKey);
const key = `s${south}l${params.level}bK${bufferKey}`;
let promiseGeometry = cacheTile.get(key);

// build geometry if doesn't exist
if (!promiseGeometry) {
let resolve;
promiseGeometry = new Promise((r) => { resolve = r; });
cacheTile.set(promiseGeometry, south, params.level, bufferKey);
cacheTile.set(key, promiseGeometry);

params.extent = shareableExtent;
params.center = builder.center(params.extent).clone();
Expand Down Expand Up @@ -145,7 +146,7 @@ export function newTileGeometry(
const geometry = new TileGeometry(builder, params, gpuBuffers);
geometry.OBB =
new OBB(geometry.boundingBox!.min, geometry.boundingBox!.max);
geometry.initRefCount(cacheTile, [south, params.level, bufferKey]);
geometry.initRefCount(cacheTile, key);
resolve!(geometry);

return Promise.resolve({ geometry, quaternion, position });
Expand Down
Loading

0 comments on commit 6d12fde

Please sign in to comment.