From c70f470e5327d4eecb7deb186ad649d910738f62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wies=C5=82aw=20=C5=A0olt=C3=A9s?= Date: Mon, 21 Sep 2026 03:32:40 +0200 Subject: [PATCH] Defer binary Blob string expansion --- .../binary-blob-lazy-string-20260921.md | 45 ++++++++++++++++ .../native/webscene_v8_runtime.cpp | 27 ++++++++-- .../webscene_v8_runtime_cache_and_frames.inc | 28 ++++++++-- .../tests/native_v8_runtime_tests.cpp | 2 + .../native_v8_runtime_websocket_tests.inc | 53 +++++++++++++++++++ 5 files changed, 147 insertions(+), 8 deletions(-) create mode 100644 docs/validation/binary-blob-lazy-string-20260921.md diff --git a/docs/validation/binary-blob-lazy-string-20260921.md b/docs/validation/binary-blob-lazy-string-20260921.md new file mode 100644 index 00000000..4296c8ac --- /dev/null +++ b/docs/validation/binary-blob-lazy-string-20260921.md @@ -0,0 +1,45 @@ +# Binary Blob lazy-string validation — 2026-09-21 + +## Owner + +WebScene #280 and the unchanged Code OSS workspace acceptance parent #252. + +## Problem + +The native WebSocket bridge delivers binary messages as `Uint8Array` values. The default WebSocket `binaryType` is `blob`, so each message constructs a WebScene Blob. Blob construction copied the bytes correctly, then eagerly called `String(part)` for every part to support the runtime's existing compatibility `Blob#toString()` behavior. + +For a `Uint8Array`, JavaScript stringification expands every element into comma-separated decimal text. The exact Code OSS startup stream receives about 11.87 MiB on its management socket, mostly in 262,144-byte frames. None of the remote protocol consumers request `String(blob)`, so this expansion was discarded work on every frame. + +## Change + +Both top-level and child-frame Blob implementations retain typed-array string descriptors and materialize the current compatibility string only when `String(blob)` is explicitly requested. Byte construction, Blob size, source snapshotting, `arrayBuffer()`, FileReader order, slicing, and object-URL behavior remain unchanged. + +The optimization is deliberately limited to ordinary `Uint8Array` parts. Other views retain their prior immediate string coercion semantics. + +## Focused gates + +Release native engine, macOS arm64: + +- 48 × 262,144-byte Blob construction (12 MiB total): **0.867 ms** +- the same construction plus explicit string expansion: **55.793 ms** +- deferred path is **64.4× faster** +- source mutation after construction retains `1,3,5,7`, proving byte/string snapshot behavior +- WebSocket cross-socket marker remains event **2** +- 100-cycle WebSocket p95: **0.469 ms** +- 20-cycle protocol Ready p95: **1.775 ms** +- 20-cycle protocol Initialized p95: **8.962 ms** +- FileReader p95: **0.030 ms** +- FileReader under a 4,096-timer backlog completes in **0.318 ms** after one competing timer +- protocol heap returns to the pre-run value + +Command: + +```sh +WEBSCENE_NATIVE_ENGINE_TEST_FILTER=websocket-file-reader \ + /private/tmp/webscene-882-build/webscene_native_engine_tests +``` + +## Product gate + +Rebuild the exact AppScene SDK and unchanged Code OSS package from the merged WebScene commit. Retain the exact remote root/provider/five resolved/model/rendered children and require first-child paint below two seconds before closing #280 or #252. + diff --git a/experiments/WebScene.NativeEngine.Probe/native/webscene_v8_runtime.cpp b/experiments/WebScene.NativeEngine.Probe/native/webscene_v8_runtime.cpp index 16f4ec38..3adf11ae 100644 --- a/experiments/WebScene.NativeEngine.Probe/native/webscene_v8_runtime.cpp +++ b/experiments/WebScene.NativeEngine.Probe/native/webscene_v8_runtime.cpp @@ -4857,17 +4857,28 @@ struct v8_dom_runtime::implementation final { 'Blob.constructor', 'partially-supported', 'byte-preserving construction, type, size, slicing and bounded streams'); const chunks = []; + const stringParts = []; let size = 0; for (const part of parts) { let bytes; if (part instanceof WebSceneBlob) { bytes=part._bytes; + stringParts.push({ blob: part }); } else if (part instanceof ArrayBuffer) { bytes = new Uint8Array(part); + stringParts.push(String(part)); } else if (ArrayBuffer.isView(part)) { bytes = new Uint8Array(part.buffer, part.byteOffset, part.byteLength); + // TypedArray#toString expands every element into decimal + // text. Preserve that compatibility result, but defer the + // work until a caller actually stringifies the Blob. + stringParts.push(part.constructor === Uint8Array + ? { start: size, end: size + bytes.byteLength } + : String(part)); } else { - bytes = new TextEncoder().encode(String(part)); + const text = String(part); + bytes = new TextEncoder().encode(text); + stringParts.push(text); } chunks.push(bytes); size += bytes.byteLength; @@ -4880,9 +4891,19 @@ struct v8_dom_runtime::implementation final { } this.size = size; this.type = String(options.type || '').toLowerCase(); - this._text = Array.from(parts, String).join(''); + this._stringParts = stringParts; + this._text = undefined; + } + toString() { + if (this._text === undefined) { + this._text = this._stringParts.map(part => { + if (typeof part === 'string') return part; + if (part.blob) return String(part.blob); + return this._bytes.subarray(part.start, part.end).toString(); + }).join(''); + } + return this._text; } - toString() { return this._text; } async text() { return new TextDecoder().decode(await this.arrayBuffer()); } diff --git a/experiments/WebScene.NativeEngine.Probe/native/webscene_v8_runtime_cache_and_frames.inc b/experiments/WebScene.NativeEngine.Probe/native/webscene_v8_runtime_cache_and_frames.inc index 7a5a590f..65302068 100644 --- a/experiments/WebScene.NativeEngine.Probe/native/webscene_v8_runtime_cache_and_frames.inc +++ b/experiments/WebScene.NativeEngine.Probe/native/webscene_v8_runtime_cache_and_frames.inc @@ -1589,17 +1589,25 @@ 'Blob.constructor', 'partially-supported', 'byte-preserving construction, type, size, object URLs, and downloads without slicing or streaming'); const chunks = []; + const stringParts = []; let size = 0; for (const part of parts) { let bytes; if (part instanceof WebSceneBlob) { - bytes = part._bytes; - } else if (part instanceof ArrayBuffer) { + bytes = part._bytes; + stringParts.push({ blob: part }); + } else if (part instanceof ArrayBuffer) { bytes = new Uint8Array(part); + stringParts.push(String(part)); } else if (ArrayBuffer.isView(part)) { bytes = new Uint8Array(part.buffer, part.byteOffset, part.byteLength); + stringParts.push(part.constructor === Uint8Array + ? { start: size, end: size + bytes.byteLength } + : String(part)); } else { - bytes = new TextEncoder().encode(String(part)); + const text = String(part); + bytes = new TextEncoder().encode(text); + stringParts.push(text); } chunks.push(bytes); size += bytes.byteLength; @@ -1612,9 +1620,19 @@ } this.size = size; this.type = String(options.type || '').toLowerCase(); - this._text = Array.from(parts, String).join(''); + this._stringParts = stringParts; + this._text = undefined; + } + toString() { + if (this._text === undefined) { + this._text = this._stringParts.map(part => { + if (typeof part === 'string') return part; + if (part.blob) return String(part.blob); + return this._bytes.subarray(part.start, part.end).toString(); + }).join(''); + } + return this._text; } - toString() { return this._text; } async text() { return new TextDecoder().decode(this._bytes); } async arrayBuffer() { return this._bytes.slice().buffer; } slice(start=0, end=this.size, type='') { diff --git a/experiments/WebScene.NativeEngine.Probe/tests/native_v8_runtime_tests.cpp b/experiments/WebScene.NativeEngine.Probe/tests/native_v8_runtime_tests.cpp index 70a3a883..6fd8bee7 100644 --- a/experiments/WebScene.NativeEngine.Probe/tests/native_v8_runtime_tests.cpp +++ b/experiments/WebScene.NativeEngine.Probe/tests/native_v8_runtime_tests.cpp @@ -770,6 +770,7 @@ int main() test_native_websocket_cross_socket_fairness(); test_native_websocket_browser_api(); test_native_websocket_protocol_handshake_timing(); + test_binary_blob_construction_defers_string_expansion(); test_native_file_reader_task_source_fairness(); return 0; } @@ -1485,6 +1486,7 @@ int main() test_native_websocket_cross_socket_fairness(); test_native_websocket_browser_api(); test_native_websocket_protocol_handshake_timing(); + test_binary_blob_construction_defers_string_expansion(); test_native_file_reader_task_source_fairness(); execute( engine, diff --git a/experiments/WebScene.NativeEngine.Probe/tests/native_v8_runtime_websocket_tests.inc b/experiments/WebScene.NativeEngine.Probe/tests/native_v8_runtime_websocket_tests.inc index 772fbe9e..15902856 100644 --- a/experiments/WebScene.NativeEngine.Probe/tests/native_v8_runtime_websocket_tests.inc +++ b/experiments/WebScene.NativeEngine.Probe/tests/native_v8_runtime_websocket_tests.inc @@ -885,6 +885,59 @@ void test_native_websocket_protocol_handshake_timing() require(ix::uninitNetSystem(), "protocol timing network cleanup failed"); } +void test_binary_blob_construction_defers_string_expansion() +{ + auto* engine = webscene_engine_create(0); + require(engine != nullptr, "binary Blob engine creation failed"); + execute(engine, R"JS( + (() => { + const bytes = new Uint8Array(256 * 1024); + bytes.fill(255); + let blob; + const started = performance.now(); + for (let index = 0; index < 48; ++index) blob = new Blob([bytes]); + const elapsedMs = performance.now() - started; + const expandedStarted = performance.now(); + for (let index = 0; index < 48; ++index) String(new Blob([bytes])); + const expandedMs = performance.now() - expandedStarted; + const snapshotSource = Uint8Array.of(1, 3, 5, 7); + const snapshot = new Blob([snapshotSource]); + snapshotSource[0] = 9; + globalThis.__webSceneBinaryBlobConstruction = { + elapsedMs, expandedMs, + size: blob.size, + snapshotText: String(snapshot) + }; + })(); + )JS", "binary-blob-construction.js"); + const auto state = evaluate( + engine, + "globalThis.__webSceneBinaryBlobConstruction", + "binary-blob-construction-state.js"); + const auto elapsed_key = std::string{"\"elapsedMs\":"}; + const auto elapsed_position = state.find(elapsed_key); + require(elapsed_position != std::string::npos, + "binary Blob construction timing was not retained"); + const auto elapsed_ms = std::stod( + state.substr(elapsed_position + elapsed_key.size())); + const auto expanded_key = std::string{"\"expandedMs\":"}; + const auto expanded_position = state.find(expanded_key); + require(expanded_position != std::string::npos, + "binary Blob explicit string-expansion timing was not retained"); + const auto expanded_ms = std::stod( + state.substr(expanded_position + expanded_key.size())); + std::cout << "Binary Blob construction state: " << state << "\n"; + require(state.find("\"size\":262144") != std::string::npos + && state.find("\"snapshotText\":\"1,3,5,7\"") + != std::string::npos, + "binary Blob changed its byte size or lazy string snapshot"); + require(elapsed_ms <= 250.0, + "constructing 12 MiB of binary Blobs exceeded 250 ms"); + require(elapsed_ms * 5.0 <= expanded_ms, + "binary Blob construction still paid the deferred string-expansion cost"); + webscene_engine_destroy(engine); +} + void test_native_file_reader_task_source_fairness() { auto* engine = webscene_engine_create(0);