Skip to content

perf(codec): replace base64 codec with simdutf SIMD-accelerated implementation#54

Merged
craftzdog merged 3 commits into
masterfrom
perf/simduff
May 8, 2026
Merged

perf(codec): replace base64 codec with simdutf SIMD-accelerated implementation#54
craftzdog merged 3 commits into
masterfrom
perf/simduff

Conversation

@craftzdog

@craftzdog craftzdog commented May 8, 2026

Copy link
Copy Markdown
Owner

Closes #50

Benchmark

Before

iOS Android
before-ios before-android

After

iOS Android
after-ios after-android

It's now roughly 2~3x faster, and it performs as fast as Hermes' built-in atob & btoa functions (string only).

Thanks @GrzywN!

craftzdog and others added 3 commits May 8, 2026 17:00
…mentation

Replaces the René Nyffenegger header-only base64.h with simdutf v8.2.0,
the same SIMD-accelerated codec used internally by Node.js, Bun, and
Deno. Vendored as the upstream amalgamation (cpp/simdutf.{h,cpp})
keeping the zero-external-dependency setup.

- Encode (base64FromArrayBuffer) calls simdutf::binary_to_base64
  directly into a pre-sized output buffer, with no intermediate
  std::string copy.
- Decode (base64ToArrayBuffer) mirrors V8's Uint8Array.fromBase64
  via simdutf::base64_to_binary_safe with last_chunk_handling::loose
  and decode_up_to_bad_char=true. The decoded std::string is moved
  into a DecodedBuffer (jsi::MutableBuffer) and returned as
  jsi::ArrayBuffer(rt, shared_ptr<...>) — no memcpy.

Roundtrip throughput now matches Hermes' built-in atob/btoa on the
example app's image.json benchmark.

Behaviour changes (TC39-compliant, see Uint8Array.fromBase64 spec):

- Mid-stream padding (e.g. 'SQ==QU0=') now throws instead of
  silently decoding the prefix.
- Embedded '\n' without removeLinebreaks=true now throws instead
  of being silently ignored.

Test fixtures in example/src/tests/{corrupt,linebreaks}.ts updated
accordingly.

Adapted from #50, ported onto the post-codegen QuickBase64Impl.cpp
and combined with the MutableBuffer-based no-memcpy decode path.

Closes #50

Co-authored-by: Karol Binkowski <karolbinkowski3@proton.me>
@craftzdog craftzdog merged commit ecb6e5d into master May 8, 2026
11 checks passed
@craftzdog craftzdog deleted the perf/simduff branch May 8, 2026 11:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant