Repository navigation
Optimize Cadence ArrayValue and Go []byte conversions (up to 13.7x faster and 8x less memory) - #4443
Merged
turbolent merged 6 commits intoMar 9, 2026
Conversation
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)
fxamacker
requested review from
RZhang05,
SupunS and
turbolent
as code owners
February 22, 2026 03:29
Dependency Review✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.OpenSSF Scorecard
Scanned Files
|
Benchstat comparison
Results
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
turbolent
reviewed
Feb 28, 2026
turbolent
left a comment
Member
There was a problem hiding this comment.
Great work! 👏 Thank you for optimizing this!
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.
8 of 12 tasks
Member
|
@SupunS Can you please also take a look? |
turbolent
deleted the
fxamacker/optimize-array-and-go-byte-slice-conversion
branch
March 9, 2026 16:59
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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/cadenceoronflow/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()usingatree.ByteArrayToByteSlice()is:Cadence
ByteArrayValueToByteSlicebenchmark using the optimized function:Cadence
ByteSliceToByteArrayValuebenchmark using the optimized function:masterbranchFiles changedin the Github PR explorer