Skip to content

DCE away base64ReverseLookup#26126

Merged
juj merged 2 commits intoemscripten-core:mainfrom
juj:dce_base64decode
Feb 26, 2026
Merged

DCE away base64ReverseLookup#26126
juj merged 2 commits intoemscripten-core:mainfrom
juj:dce_base64decode

Conversation

@juj
Copy link
Collaborator

@juj juj commented Jan 16, 2026

When building -sSINGLE_FILE builds with -sSINGLE_FILE_BINARY_ENCODE enabled, the base64Decode function was being emitted, and Closure was unable to DCE away the dependent base64ReverseLookup variable. So optimize this away manually to save code size.

Also rename the var i used by the algorithm to a __ prefixed name, so that Closure compiler won't complain about duplicate i variables shadowing each other in different scopes.

@juj juj added the code size label Jan 16, 2026
// Decodes a _known valid_ base64 string (without validation) and returns it as a new Uint8Array.
// Benchmarked to be around 5x faster compared to a simple
// "Uint8Array.from(atob(b64), c => c.charCodeAt(0))" (TODO: perhaps use this form in -Oz builds?)
#if !JS_BASE64_API
Copy link
Collaborator

Choose a reason for hiding this comment

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

How does does JS_BASE64_API relate to SINGLE_FILE_BINARY_ENCODE? The PR description only talks about that later.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah sorry, that was not so clear with GitHub diff.

By static reading the base64Decode() function starts

  $base64Decode: (b64) => {
#if JS_BASE64_API
    return Uint8Array.fromBase64(b64);
#else

i.e. the base64ReverseLookup object is never accessed if JS_BASE64_API is defined. So I updated the ifdef here to be consistent with that scenario as well.

juj and others added 2 commits February 26, 2026 23:28
…nabled, the base64Decode function was being emitted, and Closure was unable to DCE away the dependent base64ReverseLookup variable. So optimize this away manually to save code size.
This is an automatic change generated by tools/maint/rebaseline_tests.py.

The following (3) test expectation files were updated by
running the tests with `--rebaseline`:

```
codesize/test_codesize_hello_single_file.json: 5371 => 5273 [-98 bytes / -1.82%]
codesize/test_minimal_runtime_code_size_hello_webgl2_wasm_singlefile.json: 15140 => 15046 [-94 bytes / -0.62%]
codesize/test_minimal_runtime_code_size_random_printf_wasm.json: 10982 => 10884 [-98 bytes / -0.89%]

Average change: -1.11% (-1.82% - -0.62%)
```
@juj juj force-pushed the dce_base64decode branch from 50d8574 to f634570 Compare February 26, 2026 21:35
@juj juj enabled auto-merge (squash) February 26, 2026 21:36
@juj juj merged commit e0b673d into emscripten-core:main Feb 26, 2026
36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants