Repository navigation
fix(deck.gl-geotiff): free the layers of MosaicLayer items out of view - #839
Conversation
deck.gl's TileLayer keeps the layers of every cached tile and only hides the ones out of view, so each cached MosaicLayer item kept its whole layer, such as a COGLayer with its textures. An item out of view now keeps only its opened source, and draws again from it once back in view. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
| * | ||
| * deck.gl's TileLayer keeps the layers of every cached tile and only hides | ||
| * those out of view, so each cached item would keep its layers and their GPU | ||
| * memory. Here an item out of view keeps only its opened source. See #803. |
There was a problem hiding this comment.
I wonder if it's possible to keep CPU-cached resources of those tiles instead of GPU-cached resources? But probably not with our current architecture, because we cache whatever the user passes back in getTileData.
TBH now that I think about it, it feels like we kinda want three user callbacks:
- How to fetch the data for a tile (our current
getTileData) - How to create GPU resources for a tile's data (new)
- How to render GPU resources to the screen
This would allow us to have a CPU-level cache (and perhaps a disk cache if desired) without needing to touch the GPU resources...
There was a problem hiding this comment.
Note
This reply was written by Claude (Claude Code), not by @kylebarron.
Right, not with today's architecture. deck.gl's tile cache holds whatever getTileData returns, and our pipelines create their textures there: render-pipeline.ts, MultiCOGLayer's band textures, and the examples' own getTileData. So that cache is a GPU cache. Our only CPU-level cache sits below it: the shared range reader's byte cache. That's why panning back made no requests in the measurements above.
Your three callbacks are #516, and "fork B" in the chunk-manager research. Once fetching and decoding are separate from creating GPU resources, the library can keep a CPU tier of many tiles under a byte budget, and GPU resources only for the tiles drawn.
deck.gl already splits it this way for BitmapLayer: getTileData returns an image, and the layer's image prop creates the texture and destroys it with the layer. The catch is that deck.gl's TileLayer keeps every cached tile's sublayer alive, and this PR stops that only for mosaic items.
For mosaics, the CPU tier would also have to outlive the item's COGLayer, which this PR finalizes. So it would be keyed by source and tile, rather than live in the layer's tileset.
I'd keep this PR as the GPU-side fix, and design the split in #516. As a first step, I'd measure where the ~290 ms of panning back goes: decode, upload, mesh, or COGLayer's 50 ms debounce. That's what a CPU tier could save.
🤖 Written by Claude Code
#842) > [!NOTE] > This PR was written by Claude (Claude Code), not by @kylebarron. naip-mosaic and vrt-mosaic turned MosaicLayer's cache off (`maxCacheSize: 0`) and kept their own cache of opened sources. Until #839, each item in MosaicLayer's cache kept its whole COGLayer and that layer's textures, so caching items there was expensive. Now an item out of view keeps only its opened source. So both examples pass `COGSource.open` straight to `getSource`, and rely on MosaicLayer's cache. ## Changes - **naip-mosaic and vrt-mosaic** remove their module-level source caches and `maxCacheSize: 0`. naip-mosaic still passes its `epsgResolver` to `COGSource.open`. - **Spec:** decision 16 says that the examples rely on MosaicLayer's cache. One difference: the examples' caches kept every source they had ever opened, forever. By default, MosaicLayer's cache holds up to five times the items in view. So after a long trip, an item may have left the cache and must be opened again, though its header bytes are usually still in the range reader's cache. ## Checks In Chrome, panning through several areas and back: | | naip-mosaic, zoom 11 | vrt-mosaic, zoom 8 | | --- | --- | --- | | COGLayers alive | 36, the items in view | 15, the items in view | | Texture memory | 17 MB throughout | 24-26 MB throughout | | Back to the first area right after leaving it | 285 ms, **0 requests** | 221 ms, **0 requests** | | Back after six areas | 1.2 s, **8 requests**, for sources opened again | not tested | Neither example logged errors. The workspace typecheck, Biome, and both examples' `vite build` pass. Ref #803 🤖 Written by [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
> [!NOTE] > This PR was written by Claude (Claude Code), not by @kylebarron. `MosaicLayer` no longer takes `maxCacheByteSize`. Setting it never bounded the cache: an item's cached content reports no `byteLength`, so deck.gl's byte count became `NaN`, and the cache grew without limit (#803). Since #839, MosaicLayer's cache holds only the items' opened sources. A byte budget on a mosaic reads as the memory of its items' layers. But those layers keep their tiles in their own caches, and only for items in view, so MosaicLayer's cache can't bound them. In deck.gl's TileLayer, `maxCacheSize` and `maxCacheByteSize` bound the same cache; in MosaicLayer they would bound two different ones. A budget across the items' layers needs one tile cache that they all share, evicting the oldest hidden tiles across items. That belongs with the CPU/GPU split in #516. ## Migration - `maxCacheSize` still sets how many items keep their opened source in the cache. By default, that's five times the items in view. - To bound an item's memory by bytes, set `maxCacheByteSize` on the layer that `renderSource` returns. COGLayer's default pipeline reports each tile's texture bytes. A custom `getTileData` must return a `byteLength` for it to work. ```ts renderSource: (item, { source, subLayerProps }) => new COGLayer({ ...subLayerProps, source, maxCacheByteSize: 16 * 2 ** 20 }), ``` ## Changes - `MosaicLayerProps` no longer picks `maxCacheByteSize` from `TileLayerProps`, and MosaicLayer no longer passes it to its inner `TileLayer`. A comment next to the existing `debounceTime` note says why. - **Spec:** decision 17 records the removal, and the MosaicLayer section no longer proposes that sources report a `byteLength`. ## Tests - A type test checks that `new MosaicLayer({ maxCacheByteSize })` doesn't typecheck. It failed before the change. - All 217 `deck.gl-geotiff` tests pass, as do the workspace typecheck and Biome. Closes #803 🤖 Written by [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
> [!NOTE] > This PR was written by Claude (Claude Code), not by @kylebarron. Bumps `version` to `0.10.0-beta.3` in the root `package.json` and in every `packages/*/package.json`. The publish workflow publishes it under the `beta` dist-tag, so `latest` stays on 0.9.0. `CHANGELOG.md` gets a `v0.10.0-beta.3` section with GitHub's generated notes for the 17 PRs merged since v0.10.0-beta.2. ## Breaking change - #843: `MosaicLayer` no longer takes `maxCacheByteSize`. To bound an item's memory by bytes, set `maxCacheByteSize` on the layer that `renderSource` returns. ## Behavior changes - #839: a `MosaicLayer` item out of view keeps its opened source in the cache but drops its layers, which frees their textures. - #785: `parseGeoZarrMetadata` takes Y from the first entry of `spatial:dimensions` and X from the second, whatever their names, as v0.1 of the spatial convention says. Lists that aren't two entries long are rejected. - #841: `RasterTileset2D.getParentIndex` picks the parent tile that contains the child's centre. ## New APIs - #835: `parseVRT` in `@developmentseed/deck.gl-geotiff`, and `fromGDAL` in `@developmentseed/affine`. - #789: `renderSource` gets the props that `MosaicLayer` passes to its sublayers. ## Not included #838 (`ZarrSource` opens from a store with a `path`) and #837 (`DEFAULT_RANGE_READER` moves to `@developmentseed/deck.gl-raster`) are still open. Both are breaking. 🤖 Written by [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 <[email protected]>
naip-mosaic-main-vs-839.mp4
Note
This PR was written by Claude (Claude Code), not by @kylebarron.
MosaicLayerkept a whole layer alive for every item in its cache, even items out of view. deck.gl'sTileLayerkeeps the layers of every cached tile and only hides the ones out of view, and each of MosaicLayer's tiles is an item. So each cached item kept its COGLayer and all of its textures. A hidden COGLayer isn't drawn, so its own tileset never updates and never evicts anything.Now an item out of view keeps only its opened source in the cache. Its layers are dropped, and deck.gl finalizes them, which frees their textures. When the item comes back into view, it draws again from its cached source, without opening it again.
This is step 2 of #803's plan. Step 1 is still to do: sources report a
byteLength, so thatmaxCacheByteSizecan bound the cache.Changes
MosaicTileLayer, an internalTileLayersubclass that MosaicLayer now renders.renderLayersgives each item out of view an empty list of layers, so deck.gl'srenderLayersrenders none for it.renderLayersclears tiles' layers the same way whenvisibleMinZoomorvisibleMaxZoomhides the whole layer (tile-layer.ts#L394-L406).naip-mosaic and vrt-mosaic still turn MosaicLayer's cache off (
maxCacheSize: 0) and keep their own cache of opened sources. They no longer need to, and their comments explaining why are now out of date. A follow-up PR removes both.Measurements
naip-mosaic in Chrome, using MosaicLayer's own cache (its
maxCacheSize: 0removed locally). The test pans at zoom 11 through six areas of 36 items each, then back to the first:On main, the number of layers alive grows with MosaicLayer's cache: 180 items, five times the 36 in view. With this PR it stays at the items in view.
The cost is that an item coming back into view draws again from scratch. Panning to the next area and straight back:
Returning makes no COG requests, because the tile bytes come from memory. The time goes to decoding and uploading the tiles again. Loading a new area took about 2-4 s on both. The returning view looks the same on both.
Tests
LayerManager, with a test layer that logs when deck.gl initializes and finalizes it:onSourceUnload.panhelper moves the view the way a draw does: deck.gl hands each layer the new viewport, and the app doesn't render the layer again. The existingmoveToonly takes effect on the next render.deck.gl-geotifftests pass, as do the workspace typecheck and Biome.Ref #803
🤖 Written by Claude Code