Chunked JSON transport for large stat payloads (Phase 1 + 2)#108
Merged
steve-the-edwards merged 2 commits intomainfrom Apr 16, 2026
Merged
Chunked JSON transport for large stat payloads (Phase 1 + 2)#108steve-the-edwards merged 2 commits intomainfrom
steve-the-edwards merged 2 commits intomainfrom
Conversation
When a stat file's serialized JSON exceeds 2 MB, InvertJsReportWriter now emits a chunk manifest (stat_<key>.manifest.json) and multiple chunk files (stat_<key>.chunk.N.json) alongside the legacy single-file JS. Each chunk is a valid StatJsReportModel with the full statInfo and a subset of statsByModule entries, split on sorted module-key boundaries targeting ~2 MB per chunk. This is producer-side only — no runtime/browser changes yet. The legacy js/stat_<key>.js file is always emitted for backward compatibility. Adds ChunkManifest model to invert-models and 7 new tests covering: - Small stats produce no chunks - Large stats produce manifest + chunk files - Chunks are valid StatJsReportModel JSON - Merged chunks equal the original model - No duplicate module keys across chunks - buildChunks preserves all modules - Single oversized module produces one chunk Amp-Thread-ID: https://ampcode.com/threads/T-019d96d7-0e27-7579-b264-f93da72fbaa4 Co-authored-by: Amp <amp@ampcode.com>
Add externalLoadChunkedJsonFile to invert.js: for stat_ keys, tries fetching a chunk manifest, then loads all chunks in parallel via fetch(), merges statsByModule across chunks, and passes the merged JSON to the Kotlin/JS callback. Falls back to the legacy script-tag loader if no manifest is found (404). Update RemoteJsLoadingProgress to route stat_ keys through the chunked loader. Bump timeouts from 10s/30s to 15s/120s. Replace window.alert() on timeout with a console log and bounded state cleanup. Add externalLoadChunkedJsonFile external declaration to External.kt. Amp-Thread-ID: https://ampcode.com/threads/T-019d96d7-0e27-7579-b264-f93da72fbaa4 Co-authored-by: Amp <amp@ampcode.com>
zz-squareup
approved these changes
Apr 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Large stat payloads in hosted Invert reports cause browser timeouts. This PR adds a chunked JSON transport that splits oversized stats into parallel-fetchable chunks and teaches the runtime to load them via
fetch()instead of<script>tag injection.Phase 1: Producer-Side Chunking
When a stat file's serialized JSON exceeds 2 MB,
InvertJsReportWriteremits:js/stat_<key>.manifest.json— chunk manifestjs/stat_<key>.chunk.N.json— individual chunks (validStatJsReportModelJSON each)js/stat_<key>.js— legacy single-file JS (always emitted for backward compat)Each chunk contains the full
statInfoand a subset ofstatsByModuleentries, split on sorted module-key boundaries targeting ~2 MB per chunk.Phase 2: Runtime Chunked Loading
invert.js: NewexternalLoadChunkedJsonFile(key, callback)— fetches manifest, loads all chunks in parallel viafetch(), mergesstatsByModule, passes merged JSON to callback. Falls back to legacyexternalLoadJavaScriptFileif manifest 404s.RemoteJsLoadingProgress: Routesstat_*keys through chunked loader. Bumps timeouts from 10s/30s to 15s/120s. Replaceswindow.alert()on timeout with bounded state cleanup.External.kt: NewexternalLoadChunkedJsonFileexternal declaration.Why This Is Faster
fetch()of ~2MB chunksfetch().json()— no JS executionJSON.stringify()100MB object back to stringBackward Compatibility
Files Changed
ChunkManifest.ktInvertJsReportWriter.ktwriteChunkedStatIfNeeded()+buildChunks()InvertJsReportWriterTest.ktinvert.jsexternalLoadChunkedJsonFile()External.ktRemoteJsLoadingProgress.kt