Skip to content

fix(deck.gl-geotiff): free the layers of MosaicLayer items out of view - #839

Merged
kylebarron merged 2 commits into
mainfrom
kyle/mosaic-draw-items-in-view
Nov 5, 2026
Merged

kylebarron merged 2 commits into
mainfrom
kyle/mosaic-draw-items-in-view

Conversation

@kylebarron

@kylebarron kylebarron commented Nov 4, 2026 •

Copy link
Copy Markdown
Member
naip-mosaic-main-vs-839.mp4

naip-mosaic using MosaicLayer's own cache, panning at zoom 11 from A to B to C and back to A. Top: main. Bottom: this PR. The readout shows live COGLayers, textures and texture memory. On main they climb to 96 COGLayers and 45 MB; with this PR they stay at 32 and 15 MB.

Note

This PR was written by Claude (Claude Code), not by @kylebarron.

MosaicLayer kept a whole layer alive for every item in its cache, even items out of view. deck.gl's TileLayer keeps 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 that maxCacheByteSize can bound the cache.

Changes

  • MosaicTileLayer, an internal TileLayer subclass that MosaicLayer now renders.
    • Its renderLayers gives each item out of view an empty list of layers, so deck.gl's renderLayers renders none for it.
    • Afterwards it clears that list, so the item renders again once it's back in view.
    • deck.gl's own renderLayers clears tiles' layers the same way when visibleMinZoom or visibleMaxZoom hides the whole layer (tile-layer.ts#L394-L406).
  • Docs: the MosaicLayer class doc says that an item out of view keeps its source but not its layers. The spec's decisions 16 and 17 describe it as built.

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: 0 removed locally). The test pans at zoom 11 through six areas of 36 items each, then back to the first:

After the last pan main this PR
COGLayers alive 180 36
Textures alive 361 73
Texture memory 85 MB 17 MB

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:

Back to the first area main this PR
Time until drawn 20 ms, 21 ms 295 ms, 283 ms, 292 ms

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

  • Runtime, through deck.gl's LayerManager, with a test layer that logs when deck.gl initializes and finalizes it:
    • An item that leaves the view has its layers finalized, and keeps its source: it isn't passed to onSourceUnload.
    • The same happens when the app renders the layer again after the view moves.
    • An item back in view draws again, without opening its source again.
    • An item that stays in view keeps its layers.
  • A new pan helper 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 existing moveTo only takes effect on the next render.
  • Mutation checks:
    • Without the empty list, 3 of the 4 tests fail.
    • Without clearing the list afterwards, the "draws again" test fails.
  • All 216 deck.gl-geotiff tests pass, as do the workspace typecheck and Biome.

Ref #803

🤖 Written by Claude Code

kylebarron and others added 2 commits November 4, 2026 16:34
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]>
@github-actions github-actions Bot added the fix label Nov 4, 2026
*
* 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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. How to fetch the data for a tile (our current getTileData)
  2. How to create GPU resources for a tile's data (new)
  3. 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...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@kylebarron
kylebarron merged commit b4a08b5 into main Nov 5, 2026
5 checks passed
@kylebarron
kylebarron deleted the kyle/mosaic-draw-items-in-view branch November 5, 2026 14:50
kylebarron added a commit that referenced this pull request Nov 5, 2026
#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]>
kylebarron added a commit that referenced this pull request Nov 5, 2026
> [!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]>
kylebarron added a commit that referenced this pull request Nov 5, 2026
> [!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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant