Skip to content

Avoid decoding imported images several times before they load #131

Closed

Description

@vycdev

With #124, handleFiles in src/App.tsx checks each import before committing it, one step after another:

  1. decodeImage loads the file into an <img>.
  2. isCompleteRasterImage reads the whole file into memory for isTruncatedImageData, then fully decodes it again with createImageBitmap and discards the bitmap.
  3. After the commit, useSwatches and CanvasPreview decode the image again.

For the 3888×2916 JPEG fixture, the createImageBitmap step alone allocates about 45 MB. Nothing shows that a check is in progress, so large imports feel unresponsive, and the longer wait widens the window in which an edit cancels the pending import.

Proposal: run the <img> decode and the createImageBitmap check together with Promise.all, or use createImageBitmap alone for raster files and keep <img> for SVG and engines without it. In testing for #124, WebKit's createImageBitmap accepted the same formats as its <img> (including HEIC and TIFF), and in Chromium both rejected HEIC and TIFF, so the second option should not change which files load. Re-check this on both engines. Keep the structural truncation check, which is cheap next to a decode. If a large file still takes noticeably long, show a short busy state while it is checked.

Activity

  1. vycdev commented on Nov 10, 2026

    @vycdev
    OwnerAuthor

    Note

    🤖 Claude Opus 5.5 responding on behalf of vycdev

    Partly done in #140 (merged 2026-11-10). The check before an import is committed now decodes each file once. The two decodes after the commit are still there, and they take most of the import time.

    Done in #140

    • validateImageFile (src/lib/imageFile.ts) decodes raster files once, with createImageBitmap alone. SVGs, and engines without createImageBitmap, still use an <img>. The truncation check still runs first, so a truncated file is refused before any decode.
    • The browsers accept the same files as before. This was checked in Chromium and WebKit, with and without a MIME type.
    • On the 3888×2916 JPEG fixture, the pre-commit check went from ~94–112 ms to ~67–69 ms in Chromium, and from ~103–109 ms to ~90–92 ms in WebKit.

    Still open

    1. Decodes after the commit. CanvasPreview loads the committed URL into a new <img>, and useSwatches decodes it again with decodeImage. So the time from choosing a file to seeing its size didn't change (~700 ms Chromium, ~577 ms WebKit on the fixture). The next step is to pass the decode that validateImageFile already returns to these two, or let useSwatches read pixels from the preview's canvas. Both also decode again on every imageSrc change, such as edits, undo and redo, so the fix should not cover the import path only.
    2. Busy state. Nothing shows yet that an import is being checked. This matters less now that the check itself is short, but it's still worth adding if the decodes after the commit stay slow.
    3. Untested engines. Firefox and the Windows WebView2 haven't been checked against the new decode rule.
  2. vycdev commented on Nov 19, 2026

    @vycdev
    OwnerAuthor

    Note

    🤖 Claude Opus 5.5 responding on behalf of vycdev

    Fixed by #153 (merged into develop). Imports now reuse their validation decode, edits and Undo/Redo share one pixel snapshot, and a checking indicator appears for checks over 150 ms. On the 3888×2916 fixture, file-to-size-shown went from 1,052 to 305 ms in Chromium and from 1,427 to 501 ms in WebKit. Firefox and WebView2 still haven't been tested with the new decode rule.

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