Skip to content

perf(bcs): closure-based encoder/decoder with bulk ops and bounds checks#1004

Draft
hayes-mysten wants to merge 3 commits intomainfrom
worktree-bcs-perf-optimizations
Draft

perf(bcs): closure-based encoder/decoder with bulk ops and bounds checks#1004
hayes-mysten wants to merge 3 commits intomainfrom
worktree-bcs-perf-optimizations

Conversation

@hayes-mysten
Copy link
Copy Markdown
Contributor

@hayes-mysten hayes-mysten commented Apr 7, 2026

Description

Major performance rewrite of @mysten/bcs replacing the DataView-based reader/writer with a closure-based encoder/decoder architecture.

Many performance ideas in this rewrite were inspired by @unconfirmedlabs/bcs by BL. Their work on codegen-based BCS decoding and object factory patterns informed several of the optimizations here.

Benchmarks vs main (Node.js, 100K iterations)

Serialize:

Payload Main This PR Speedup
SimpleStruct (41B) 1,712 ns 83 ns 20.6x
NestedStruct (65B) 1,485 ns 175 ns 8.5x
Enum variant (33B) 762 ns 128 ns 6x
u64 (8B) 937 ns 75 ns 12.5x
u128 (16B) 1,339 ns 81 ns 16.5x
u256 (32B) 2,578 ns 284 ns 9.1x
Option<Struct> (42B) 1,997 ns 99 ns 20.2x
Vec<u32> x1000 (4KB) 28,778 ns 2,356 ns 12.2x
Address [32]u8 (32B) 1,340 ns 84 ns 16x

Deserialize:

Payload Main This PR Speedup
SimpleStruct (41B) 657 ns 127 ns 5.2x
NestedStruct (65B) 639 ns 249 ns 2.6x
Enum variant (33B) 354 ns 43 ns 8.2x
u64 (8B) 1,036 ns 79 ns 13.1x
u128 (16B) 2,332 ns 196 ns 11.9x
u256 (32B) 5,291 ns 498 ns 10.6x
Option<Struct> (42B) 921 ns 93 ns 9.9x
Vec<u32> x1000 (4KB) 13,246 ns 2,511 ns 5.3x
Address [32]u8 (32B) 495 ns 67 ns 7.4x

Real-world TX Effects (1000 transactions, bun):

Phase Main This PR Speedup
Serialize 44K ops/s 354K ops/s 8.1x
Deserialize 93K ops/s 498K ops/s 5.3x
Roundtrip 28K ops/s 197K ops/s 7.0x

Architecture changes

  • New bcs-decode.ts / bcs-encode.ts with createDecoder() / createEncoder() factories closing over private offset/buffer state
  • Singleton instances for BcsType.parse() / toBytes(); independent instances for standalone BcsReader / BcsWriter
  • BcsReader / BcsWriter are now thin wrappers around decoder/encoder instances
  • Deleted uleb.ts — ULEB logic inlined into encoder/decoder closures

Performance optimizations

  • Manual little-endian byte reads instead of DataView
  • Bulk decode/encode for primitive vectors (u8, u16, u32, u64, bool)
  • ASCII fast path for string encode/decode
  • Unrolled struct/enum/tuple codecs for 1-8 fields
  • Object factory pattern for struct/enum decode (stable hidden classes)
  • Shared buffer reuse for small serializations
  • Pre-computed ULEB bytes for enum variant indices

Safety

  • Post-decode bounds check in parse() and read() — throws RangeError on truncated/malformed input
  • maxSize enforced in ensure() during buffer growth (fails early)
  • Per-field validation preserved through compound builders (_codec.write includes validation)
  • Decoded bytes always returned as copies (slice, not subarray)
  • Enum property order preserved (variant key before $kind)
  • Defense-in-depth throw in enum encoder on no matching variant

Public API

  • No exports added or removed from index.ts
  • New convenience methods on BcsType: toBytes, toHex, toBase64, toBase58
  • BcsType.read() and write() deprecated in favor of parse()/toBytes()/serialize()
  • BcsType.toBytes() is the fast path; serialize() wraps it with SerializedBcs

Test plan

  • 328 BCS tests (up from 124 on main), +5 new test files
  • 339 sui unit tests all passing
  • Test coverage for: enum property order, decoded byte independence, per-field validation in structs/enums/tuples, transform validation chain, malformed input, ULEB >2^32

AI Assistance Notice

  • This PR was primarily written by AI.
  • I used AI for docs / tests, but manually wrote the source code.
  • I used AI to understand the problem space / repository.
  • I did not use AI for this PR.

@hayes-mysten hayes-mysten had a problem deploying to sui-typescript-aws-kms-test-env April 7, 2026 00:18 — with GitHub Actions Failure
@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 7, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
sui-typescript-docs Ignored Ignored Preview Apr 7, 2026 5:56pm

Request Review

@hayes-mysten hayes-mysten had a problem deploying to sui-typescript-aws-kms-test-env April 7, 2026 00:27 — with GitHub Actions Failure
@hayes-mysten hayes-mysten had a problem deploying to sui-typescript-aws-kms-test-env April 7, 2026 00:30 — with GitHub Actions Failure
@hayes-mysten hayes-mysten had a problem deploying to sui-typescript-aws-kms-test-env April 7, 2026 00:35 — with GitHub Actions Failure
@hayes-mysten hayes-mysten had a problem deploying to sui-typescript-aws-kms-test-env April 7, 2026 00:36 — with GitHub Actions Failure
@hayes-mysten hayes-mysten had a problem deploying to sui-typescript-aws-kms-test-env April 7, 2026 00:38 — with GitHub Actions Failure
@hayes-mysten hayes-mysten had a problem deploying to sui-typescript-aws-kms-test-env April 7, 2026 00:45 — with GitHub Actions Failure
@hayes-mysten hayes-mysten had a problem deploying to sui-typescript-aws-kms-test-env April 7, 2026 00:47 — with GitHub Actions Failure
@hayes-mysten hayes-mysten had a problem deploying to sui-typescript-aws-kms-test-env April 7, 2026 00:58 — with GitHub Actions Failure
@hayes-mysten hayes-mysten had a problem deploying to sui-typescript-aws-kms-test-env April 7, 2026 00:59 — with GitHub Actions Failure
@hayes-mysten hayes-mysten force-pushed the worktree-bcs-perf-optimizations branch from f7cbdb0 to fc2d4c3 Compare April 7, 2026 01:10
@hayes-mysten hayes-mysten had a problem deploying to sui-typescript-aws-kms-test-env April 7, 2026 01:10 — with GitHub Actions Failure
@hayes-mysten hayes-mysten force-pushed the worktree-bcs-perf-optimizations branch from fc2d4c3 to d3a3e91 Compare April 7, 2026 01:36
@hayes-mysten hayes-mysten temporarily deployed to sui-typescript-aws-kms-test-env April 7, 2026 01:36 — with GitHub Actions Inactive
@hayes-mysten hayes-mysten force-pushed the worktree-bcs-perf-optimizations branch from d3a3e91 to 833209c Compare April 7, 2026 02:05
@hayes-mysten hayes-mysten temporarily deployed to sui-typescript-aws-kms-test-env April 7, 2026 02:05 — with GitHub Actions Inactive
@hayes-mysten hayes-mysten temporarily deployed to sui-typescript-aws-kms-test-env April 7, 2026 02:09 — with GitHub Actions Inactive
@hayes-mysten hayes-mysten force-pushed the worktree-bcs-perf-optimizations branch from a39ae28 to 1629d1c Compare April 7, 2026 06:39
@hayes-mysten hayes-mysten temporarily deployed to sui-typescript-aws-kms-test-env April 7, 2026 06:39 — with GitHub Actions Inactive
@hayes-mysten hayes-mysten temporarily deployed to sui-typescript-aws-kms-test-env April 7, 2026 16:53 — with GitHub Actions Inactive
@hayes-mysten hayes-mysten force-pushed the worktree-bcs-perf-optimizations branch from fdf2673 to 1f07625 Compare April 7, 2026 16:53
@hayes-mysten hayes-mysten temporarily deployed to sui-typescript-aws-kms-test-env April 7, 2026 16:54 — with GitHub Actions Inactive
@hayes-mysten hayes-mysten force-pushed the worktree-bcs-perf-optimizations branch from 1f07625 to ae249a2 Compare April 7, 2026 17:03
@hayes-mysten hayes-mysten temporarily deployed to sui-typescript-aws-kms-test-env April 7, 2026 17:04 — with GitHub Actions Inactive
@hayes-mysten hayes-mysten force-pushed the worktree-bcs-perf-optimizations branch from ae249a2 to 84c5993 Compare April 7, 2026 17:13
@hayes-mysten hayes-mysten temporarily deployed to sui-typescript-aws-kms-test-env April 7, 2026 17:13 — with GitHub Actions Inactive
@hayes-mysten hayes-mysten force-pushed the worktree-bcs-perf-optimizations branch from 84c5993 to aaca4d2 Compare April 7, 2026 17:31
@hayes-mysten hayes-mysten temporarily deployed to sui-typescript-aws-kms-test-env April 7, 2026 17:31 — with GitHub Actions Inactive
@hayes-mysten hayes-mysten force-pushed the worktree-bcs-perf-optimizations branch from aaca4d2 to d169584 Compare April 7, 2026 17:34
@hayes-mysten hayes-mysten temporarily deployed to sui-typescript-aws-kms-test-env April 7, 2026 17:34 — with GitHub Actions Inactive
@hayes-mysten hayes-mysten force-pushed the worktree-bcs-perf-optimizations branch from d169584 to c8fbcef Compare April 7, 2026 17:39
@hayes-mysten hayes-mysten temporarily deployed to sui-typescript-aws-kms-test-env April 7, 2026 17:39 — with GitHub Actions Inactive
@hayes-mysten hayes-mysten force-pushed the worktree-bcs-perf-optimizations branch from c8fbcef to 25458b2 Compare April 7, 2026 17:46
@hayes-mysten hayes-mysten temporarily deployed to sui-typescript-aws-kms-test-env April 7, 2026 17:47 — with GitHub Actions Inactive
hayes-mysten and others added 3 commits April 7, 2026 10:56
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@hayes-mysten hayes-mysten force-pushed the worktree-bcs-perf-optimizations branch from 25458b2 to c3c82f2 Compare April 7, 2026 17:56
@hayes-mysten hayes-mysten temporarily deployed to sui-typescript-aws-kms-test-env April 7, 2026 17:56 — with GitHub Actions Inactive
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