Skip to content

No in-flight dedup on the chunk path: concurrent reads of the same chunk fetch and decode twice #7

Closed

Description

@xinaesthete

Summary

The cache is read while the task list is being built and written only after the worker returns. Two concurrent getWorker calls covering the same chunk both observe a miss, so both fetch the bytes and both run a codec decode.

Where

src/get-worker.ts:

  • :760 — const cachedChunk = cache.get(cacheKey), during task-list construction.
  • :798, :839, :856 — the corresponding cache.set calls, inside the task, after the decode completes.

Nothing occupies the key in between.

Why it matters

Overlapping viewport reads are the normal case for a tiled image viewer: a pan issues a new read while the previous one is still settling, and adjacent requests routinely share chunks. The duplicated work is a wasted network round-trip plus a wasted worker decode — the expensive half — and it is worst exactly when the pool is already saturated.

Suggested fix

Key pending promises, not just settled chunks: on a miss, insert the in-flight promise under the cache key so a concurrent caller awaits it instead of starting its own task. Worth pairing with cleanup on rejection — a cached rejected promise would otherwise poison that key permanently (we have the same bug in our own parquet cache, so this is a sympathetic report rather than a criticism).

Activity

  1. thewtex commented on Aug 10, 2026

    @thewtex
    Member

    Closing per #10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions