Skip to content

Optimize Cadence ArrayValue and Go []byte conversions (up to 13.7x faster and 8x less memory) - #4443

Merged
turbolent merged 6 commits into
masterfrom
fxamacker/optimize-array-and-go-byte-slice-conversion
Mar 9, 2026
Merged

turbolent merged 6 commits into
masterfrom
fxamacker/optimize-array-and-go-byte-slice-conversion

Conversation

@fxamacker

@fxamacker fxamacker commented Feb 22, 2026 •

Copy link
Copy Markdown
Contributor

Updates onflow/flow-go#8447 onflow/flow-go#8401
Requires onflow/atree#629

Some conversions can be faster and use less memory by doing them in onflow/atree rather than in client software such as onflow/cadence or onflow/flow-go.

This PR uses 2 new functions exported by Atree to optimize conversions for these 3 existing Cadence functions:

  • ByteArrayValueToByteSlice()
  • ByteSliceToByteArrayValue()
  • ByteSliceToConstantSizedByteArrayValue()

The above conversion functions are used in both Cadence in flow-go.

Using the new functions provided by Atree will optimize various operations such as decoding RLP data and calling EVM functions.

Benchmarks

Notably, cadence.ByteArrayValueToByteSlice() using atree.ByteArrayToByteSlice() is:

  • 13.7 times faster 🚀
  • 8 times less memory used (bytes/op)
  • 8 times fewer memory allocs (allocs/op)

Cadence ByteArrayValueToByteSlice benchmark using the optimized function:

                             │  before.txt   │              after.txt              │
                             │    sec/op     │   sec/op     vs base                │
ByteArrayValueToByteSlice-12   1049.50n ± 2%   76.52n ± 4%  -92.71% (p=0.000 n=20)

                             │ before.txt  │             after.txt              │
                             │    B/op     │    B/op     vs base                │
ByteArrayValueToByteSlice-12   256.00 ± 0%   32.00 ± 0%  -87.50% (p=0.000 n=20)

                             │ before.txt │             after.txt              │
                             │ allocs/op  │ allocs/op   vs base                │
ByteArrayValueToByteSlice-12   8.000 ± 0%   1.000 ± 0%  -87.50% (p=0.000 n=20)

Cadence ByteSliceToByteArrayValue benchmark using the optimized function:

                             │  before.txt  │              after.txt              │
                             │    sec/op    │   sec/op     vs base                │
ByteSliceToByteArrayValue-12   1038.0n ± 2%   669.7n ± 1%  -35.48% (p=0.000 n=20)

                             │ before.txt  │             after.txt              │
                             │    B/op     │    B/op     vs base                │
ByteSliceToByteArrayValue-12   1399.0 ± 0%   869.5 ± 0%  -37.85% (p=0.000 n=20)

                             │ before.txt  │             after.txt              │
                             │  allocs/op  │ allocs/op   vs base                │
ByteSliceToByteArrayValue-12   11.000 ± 0%   5.000 ± 0%  -54.55% (p=0.000 n=20)

  • Targeted PR against master branch
  • Linked to Github issue with discussion and accepted design OR link to spec that describes this work
  • Code follows the standards mentioned here
  • Updated relevant documentation
  • Re-reviewed Files changed in the Github PR explorer
  • Added appropriate labels

Conversions can be significantly faster and use less memory
if we do the conversions in Atree rather than in client software
such as onflow/cadence or onflow/flow-go.

This commit uses two new functions in Atree to optimize
conversions in these three Cadence functions:
- ByteArrayValueToByteSlice()
- ByteSliceToByteArrayValue()
- ByteSliceToConstantSizedByteArrayValue()

The above conversion functions are used in both Cadence in flow-go.
Using the new functions provided by Atree will optimize various
operations such as decoding RLP data and calling EVM functions.

Notably, ByteArrayValueToByteSlice() using the new conversion function
provided by Atree is:
- 13.7 times faster
- 8 times less memory used (bytes/op)
- 8 times fewer memory allocs (allocs/op)

Cadence ByteArrayValueToByteSlice benchmark using the optimized function:

                             │  before.txt   │              after.txt              │
                             │    sec/op     │   sec/op     vs base                │
ByteArrayValueToByteSlice-12   1049.50n ± 2%   76.52n ± 4%  -92.71% (p=0.000 n=20)

                             │ before.txt  │             after.txt              │
                             │    B/op     │    B/op     vs base                │
ByteArrayValueToByteSlice-12   256.00 ± 0%   32.00 ± 0%  -87.50% (p=0.000 n=20)

                             │ before.txt │             after.txt              │
                             │ allocs/op  │ allocs/op   vs base                │
ByteArrayValueToByteSlice-12   8.000 ± 0%   1.000 ± 0%  -87.50% (p=0.000 n=20)

Cadence ByteSliceToByteArrayValue benchmark using the optimized function:

                             │  before.txt  │              after.txt              │
                             │    sec/op    │   sec/op     vs base                │
ByteSliceToByteArrayValue-12   1038.0n ± 2%   669.7n ± 1%  -35.48% (p=0.000 n=20)

                             │ before.txt  │             after.txt              │
                             │    B/op     │    B/op     vs base                │
ByteSliceToByteArrayValue-12   1399.0 ± 0%   869.5 ± 0%  -37.85% (p=0.000 n=20)

                             │ before.txt  │             after.txt              │
                             │  allocs/op  │ allocs/op   vs base                │
ByteSliceToByteArrayValue-12   11.000 ± 0%   5.000 ± 0%  -54.55% (p=0.000 n=20)
@github-actions

github-actions Bot commented Feb 22, 2026 •

Copy link
Copy Markdown

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

OpenSSF Scorecard

PackageVersionScoreDetails
gomod/github.com/onflow/atree 0.13.0 🟢 8.8
Details
CheckScoreReason
Code-Review🟢 10all changesets reviewed
Maintained🟢 1030 commit(s) and 2 issue activity found in the last 90 days -- score normalized to 10
Packaging⚠️ -1packaging workflow not detected
Security-Policy🟢 10security policy file detected
Dangerous-Workflow🟢 10no dangerous workflow patterns detected
Binary-Artifacts🟢 10no binaries found in the repo
Token-Permissions🟢 10GitHub workflow tokens follow principle of least privilege
CII-Best-Practices⚠️ 0no effort to earn an OpenSSF best practices badge detected
Pinned-Dependencies🟢 10all dependencies are pinned
License🟢 10license file detected
Fuzzing⚠️ 0project is not fuzzed
Signed-Releases⚠️ -1no releases found
Branch-Protection⚠️ -1internal error: error during branchesHandler.setup: internal error: some github tokens can't read classic branch protection rules: https://github.com/ossf/scorecard-action/blob/main/docs/authentication/fine-grained-auth-token.md
SAST🟢 10SAST tool is run on all commits
gomod/github.com/onflow/atree 0.13.0 🟢 8.8
Details
CheckScoreReason
Code-Review🟢 10all changesets reviewed
Maintained🟢 1030 commit(s) and 2 issue activity found in the last 90 days -- score normalized to 10
Packaging⚠️ -1packaging workflow not detected
Security-Policy🟢 10security policy file detected
Dangerous-Workflow🟢 10no dangerous workflow patterns detected
Binary-Artifacts🟢 10no binaries found in the repo
Token-Permissions🟢 10GitHub workflow tokens follow principle of least privilege
CII-Best-Practices⚠️ 0no effort to earn an OpenSSF best practices badge detected
Pinned-Dependencies🟢 10all dependencies are pinned
License🟢 10license file detected
Fuzzing⚠️ 0project is not fuzzed
Signed-Releases⚠️ -1no releases found
Branch-Protection⚠️ -1internal error: error during branchesHandler.setup: internal error: some github tokens can't read classic branch protection rules: https://github.com/ossf/scorecard-action/blob/main/docs/authentication/fine-grained-auth-token.md
SAST🟢 10SAST tool is run on all commits

Scanned Files

  • go.mod
  • tools/compatibility-check/go.mod

@github-actions

github-actions Bot commented Feb 22, 2026 •

Copy link
Copy Markdown

Benchstat comparison

  • Base branch: onflow:master
  • Base commit: 8fd8bd4
Results

old.txtnew.txt
time/opdelta
pkg:github.com/onflow/cadence/runtime goos:linux goarch:amd64
ContractFunctionInvocation-4416µs ± 0%431µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
Emit-44.59ms ± 0%4.57ms ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/runtime goos:linux goarch:amd64
ExportType/composite_type-4285ns ± 0%266ns ± 0%~(p=1.000 n=1+1)
ExportType/simple_type-478.6ns ± 0%77.6ns ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
ImperativeFib-423.6µs ± 0%23.1µs ± 0%~(p=1.000 n=1+1)
InterpretRecursionFib-42.27ms ± 0%2.28ms ± 0%~(p=1.000 n=1+1)
NewInterpreter/new_interpreter-4915ns ± 0%920ns ± 0%~(p=1.000 n=1+1)
NewInterpreter/new_sub-interpreter-4316ns ± 0%332ns ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/runtime goos:linux goarch:amd64
RuntimeFungibleTokenTransferInterpreter-4638µs ± 0%639µs ± 0%~(p=1.000 n=1+1)
RuntimeFungibleTokenTransferVM-4703µs ± 0%683µs ± 0%~(p=1.000 n=1+1)
RuntimeResourceDictionaryValues-42.68ms ± 0%2.69ms ± 0%~(p=1.000 n=1+1)
RuntimeResourceTracking-411.4ms ± 0%11.5ms ± 0%~(p=1.000 n=1+1)
RuntimeScriptNoop-414.8µs ± 0%15.0µs ± 0%~(p=1.000 n=1+1)
RuntimeVMInvokeContractImperativeFib-441.4µs ± 0%40.7µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
ValueIsSubtypeOfSemaType-467.4ns ± 0%60.9ns ± 0%~(p=1.000 n=1+1)
 
alloc/opdelta
pkg:github.com/onflow/cadence/runtime goos:linux goarch:amd64
ContractFunctionInvocation-4153kB ± 0%152kB ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
Emit-41.50MB ± 0%1.49MB ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/runtime goos:linux goarch:amd64
ExportType/composite_type-4120B ± 0%120B ± 0%~(all equal)
ExportType/simple_type-40.00B 0.00B ~(all equal)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
ImperativeFib-48.30kB ± 0%8.30kB ± 0%~(all equal)
InterpretRecursionFib-41.19MB ± 0%1.19MB ± 0%~(p=1.000 n=1+1)
NewInterpreter/new_interpreter-4976B ± 0%976B ± 0%~(all equal)
NewInterpreter/new_sub-interpreter-4232B ± 0%232B ± 0%~(all equal)
pkg:github.com/onflow/cadence/runtime goos:linux goarch:amd64
RuntimeFungibleTokenTransferInterpreter-4170kB ± 0%170kB ± 0%~(p=1.000 n=1+1)
RuntimeFungibleTokenTransferVM-4192kB ± 0%193kB ± 0%~(p=1.000 n=1+1)
RuntimeResourceDictionaryValues-41.76MB ± 0%1.77MB ± 0%~(p=1.000 n=1+1)
RuntimeResourceTracking-48.61MB ± 0%8.59MB ± 0%~(p=1.000 n=1+1)
RuntimeScriptNoop-48.06kB ± 0%8.05kB ± 0%~(p=1.000 n=1+1)
RuntimeVMInvokeContractImperativeFib-413.3kB ± 0%13.3kB ± 0%~(all equal)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
ValueIsSubtypeOfSemaType-432.0B ± 0%32.0B ± 0%~(all equal)
 
allocs/opdelta
pkg:github.com/onflow/cadence/runtime goos:linux goarch:amd64
ContractFunctionInvocation-42.47k ± 0%2.47k ± 0%~(all equal)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
Emit-440.0k ± 0%40.0k ± 0%~(all equal)
pkg:github.com/onflow/cadence/runtime goos:linux goarch:amd64
ExportType/composite_type-43.00 ± 0%3.00 ± 0%~(all equal)
ExportType/simple_type-40.00 0.00 ~(all equal)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
ImperativeFib-4176 ± 0%176 ± 0%~(all equal)
InterpretRecursionFib-417.7k ± 0%17.7k ± 0%~(all equal)
NewInterpreter/new_interpreter-415.0 ± 0%15.0 ± 0%~(all equal)
NewInterpreter/new_sub-interpreter-44.00 ± 0%4.00 ± 0%~(all equal)
pkg:github.com/onflow/cadence/runtime goos:linux goarch:amd64
RuntimeFungibleTokenTransferInterpreter-43.27k ± 0%3.27k ± 0%~(all equal)
RuntimeFungibleTokenTransferVM-43.72k ± 0%3.73k ± 0%~(p=1.000 n=1+1)
RuntimeResourceDictionaryValues-436.7k ± 0%36.7k ± 0%~(p=1.000 n=1+1)
RuntimeResourceTracking-4144k ± 0%144k ± 0%~(p=1.000 n=1+1)
RuntimeScriptNoop-4114 ± 0%114 ± 0%~(all equal)
RuntimeVMInvokeContractImperativeFib-4424 ± 0%424 ± 0%~(all equal)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
ValueIsSubtypeOfSemaType-41.00 ± 0%1.00 ± 0%~(all equal)
 

@turbolent turbolent left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Great work! 👏 Thank you for optimizing this!

Comment thread tools/compatibility-check/go.mod Resolved
This commit also adds the AtreeArrayBatchConstruction meter, but
only if atree.NewArrayFromBatchData() is very likely to be used
since single-slab arrays don't need to use that function.
@fxamacker
fxamacker requested a review from turbolent March 2, 2026 17:04
@turbolent

Copy link
Copy Markdown
Member

@SupunS Can you please also take a look?

@turbolent turbolent left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Great work! 👏

@SupunS SupunS left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👏

@turbolent
turbolent merged commit 3f3470e into master Mar 9, 2026
17 checks passed
@turbolent
turbolent deleted the fxamacker/optimize-array-and-go-byte-slice-conversion branch March 9, 2026 16:59
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.

3 participants

⌂ 换网址