Skip to content

Optimize event emission#4334

Merged
turbolent merged 1 commit into
masterfrom
bastian/optimize-event-emission
Nov 13, 2025
Merged

Optimize event emission#4334
turbolent merged 1 commit into
masterfrom
bastian/optimize-event-emission

Conversation

@turbolent
Copy link
Copy Markdown
Member

@turbolent turbolent commented Nov 12, 2025

Description

Looking at the optimizations of the FastBreak contract (dapperlabs/nba-smart-contracts#294) I noticed this change to an event parameter: https://github.com/dapperlabs/nba-smart-contracts/pull/294/files#diff-9f296e6a8c507b02a639681b7295fafccaf5868f24299c03efda0bde31a18047L68.

Given the event emission handler exports the event arguments immediately, we can avoid transfers (copies) in general.


  • 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

only convert arguments, do not transfer (avoid unnecessary copies)
@github-actions
Copy link
Copy Markdown

Benchstat comparison

  • Base branch: onflow:master
  • Base commit: 7104cac
Results

old.txtnew.txt
time/opdelta
pkg:github.com/onflow/cadence/bbq/compiler goos:linux goarch:amd64
CompileTime-433.4µs ± 0%32.6µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/runtime goos:linux goarch:amd64
ContractFunctionInvocation-4408µs ± 0%402µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
ContractImport-4192µs ± 0%198µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/runtime goos:linux goarch:amd64
ExportType/composite_type-4261ns ± 0%260ns ± 0%~(p=1.000 n=1+1)
ExportType/simple_type-477.9ns ± 0%77.8ns ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
FTTransfer-4121µs ± 0%117µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
ImperativeFib-420.9µs ± 0%21.0µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
ImperativeFib-415.0µs ± 0%14.9µs ± 0%~(p=1.000 n=1+1)
ImperativeFibNewCompilerNewVM-435.2µs ± 0%35.2µs ± 0%~(p=1.000 n=1+1)
ImperativeFibNewVM-417.4µs ± 0%17.3µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
InterpretRecursionFib-42.23ms ± 0%2.14ms ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
InterpreterFTTransfer-4111µs ± 0%116µs ± 0%~(p=1.000 n=1+1)
InterpreterImperativeFib-421.9µs ± 0%22.5µs ± 0%~(p=1.000 n=1+1)
InterpreterNewStruct-463.5µs ± 0%61.4µs ± 0%~(p=1.000 n=1+1)
MethodCall/concrete_type_method_call-429.3µs ± 0%29.3µs ± 0%~(p=1.000 n=1+1)
MethodCall/interface_method_call-451.0µs ± 0%51.1µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
NewInterpreter/new_interpreter-41.04µs ± 0%1.07µs ± 0%~(p=1.000 n=1+1)
NewInterpreter/new_sub-interpreter-4337ns ± 0%314ns ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
NewResource-487.3µs ± 0%86.6µs ± 0%~(p=1.000 n=1+1)
NewStruct-434.6µs ± 0%34.5µs ± 0%~(p=1.000 n=1+1)
NewStructRaw-43.69µs ± 0%3.80µs ± 0%~(p=1.000 n=1+1)
RecursionFib-4848µs ± 0%858µs ± 0%~(p=1.000 n=1+1)
RuntimeFungibleTokenTransfer-4725µs ± 0%722µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/runtime goos:linux goarch:amd64
RuntimeFungibleTokenTransferInterpreter-4590µs ± 0%579µs ± 0%~(p=1.000 n=1+1)
RuntimeFungibleTokenTransferVM-4632µs ± 0%629µs ± 0%~(p=1.000 n=1+1)
RuntimeResourceDictionaryValues-42.61ms ± 0%2.58ms ± 0%~(p=1.000 n=1+1)
RuntimeResourceTracking-411.6ms ± 0%11.5ms ± 0%~(p=1.000 n=1+1)
RuntimeScriptNoop-414.6µs ± 0%14.8µs ± 0%~(p=1.000 n=1+1)
RuntimeVMInvokeContractImperativeFib-427.6µs ± 0%27.6µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
ValueIsSubtypeOfSemaType-466.9ns ± 0%65.9ns ± 0%~(p=1.000 n=1+1)
 
alloc/opdelta
pkg:github.com/onflow/cadence/bbq/compiler goos:linux goarch:amd64
CompileTime-416.3kB ± 0%16.3kB ± 0%~(all equal)
pkg:github.com/onflow/cadence/runtime goos:linux goarch:amd64
ContractFunctionInvocation-4152kB ± 0%153kB ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
ContractImport-473.6kB ± 0%73.6kB ± 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/bbq/vm/test goos:linux goarch:amd64
FTTransfer-438.2kB ± 0%38.1kB ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
ImperativeFib-48.30kB ± 0%8.30kB ± 0%~(all equal)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
ImperativeFib-41.94kB ± 0%1.94kB ± 0%~(all equal)
ImperativeFibNewCompilerNewVM-421.8kB ± 0%21.8kB ± 0%~(all equal)
ImperativeFibNewVM-44.68kB ± 0%4.68kB ± 0%~(all equal)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
InterpretRecursionFib-41.19MB ± 0%1.19MB ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
InterpreterFTTransfer-439.9kB ± 0%39.9kB ± 0%~(p=1.000 n=1+1)
InterpreterImperativeFib-48.29kB ± 0%8.29kB ± 0%~(all equal)
InterpreterNewStruct-424.3kB ± 0%24.4kB ± 0%~(p=1.000 n=1+1)
MethodCall/concrete_type_method_call-47.02kB ± 0%7.00kB ± 0%~(p=1.000 n=1+1)
MethodCall/interface_method_call-414.5kB ± 0%14.5kB ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
NewInterpreter/new_interpreter-4976B ± 0%976B ± 0%~(all equal)
NewInterpreter/new_sub-interpreter-4232B ± 0%232B ± 0%~(all equal)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
NewResource-440.3kB ± 0%40.3kB ± 0%~(all equal)
NewStruct-410.4kB ± 0%10.3kB ± 0%~(p=1.000 n=1+1)
NewStructRaw-42.58kB ± 0%2.56kB ± 0%~(p=1.000 n=1+1)
RecursionFib-488.1kB ± 0%88.1kB ± 0%~(all equal)
RuntimeFungibleTokenTransfer-4238kB ± 0%238kB ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/runtime goos:linux goarch:amd64
RuntimeFungibleTokenTransferInterpreter-4159kB ± 0%160kB ± 0%~(p=1.000 n=1+1)
RuntimeFungibleTokenTransferVM-4173kB ± 0%173kB ± 0%~(p=1.000 n=1+1)
RuntimeResourceDictionaryValues-41.76MB ± 0%1.76MB ± 0%~(p=1.000 n=1+1)
RuntimeResourceTracking-49.26MB ± 0%9.27MB ± 0%~(p=1.000 n=1+1)
RuntimeScriptNoop-48.04kB ± 0%8.04kB ± 0%~(p=1.000 n=1+1)
RuntimeVMInvokeContractImperativeFib-49.79kB ± 0%9.79kB ± 0%~(all equal)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
ValueIsSubtypeOfSemaType-448.0B ± 0%48.0B ± 0%~(all equal)
 
allocs/opdelta
pkg:github.com/onflow/cadence/bbq/compiler goos:linux goarch:amd64
CompileTime-4176 ± 0%176 ± 0%~(all equal)
pkg:github.com/onflow/cadence/runtime goos:linux goarch:amd64
ContractFunctionInvocation-42.46k ± 0%2.46k ± 0%~(all equal)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
ContractImport-4931 ± 0%931 ± 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/bbq/vm/test goos:linux goarch:amd64
FTTransfer-4978 ± 0%966 ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
ImperativeFib-4176 ± 0%176 ± 0%~(all equal)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
ImperativeFib-449.0 ± 0%49.0 ± 0%~(all equal)
ImperativeFibNewCompilerNewVM-4205 ± 0%205 ± 0%~(all equal)
ImperativeFibNewVM-486.0 ± 0%86.0 ± 0%~(all equal)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
InterpretRecursionFib-417.7k ± 0%17.7k ± 0%~(all equal)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
InterpreterFTTransfer-4837 ± 0%833 ± 0%~(p=1.000 n=1+1)
InterpreterImperativeFib-4175 ± 0%175 ± 0%~(all equal)
InterpreterNewStruct-4418 ± 0%418 ± 0%~(all equal)
MethodCall/concrete_type_method_call-4135 ± 0%135 ± 0%~(all equal)
MethodCall/interface_method_call-4285 ± 0%285 ± 0%~(all equal)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
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/bbq/vm/test goos:linux goarch:amd64
NewResource-4722 ± 0%722 ± 0%~(all equal)
NewStruct-4219 ± 0%219 ± 0%~(all equal)
NewStructRaw-438.0 ± 0%38.0 ± 0%~(all equal)
RecursionFib-43.65k ± 0%3.65k ± 0%~(all equal)
RuntimeFungibleTokenTransfer-44.00k ± 0%3.98k ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/runtime goos:linux goarch:amd64
RuntimeFungibleTokenTransferInterpreter-42.99k ± 0%2.98k ± 0%~(p=1.000 n=1+1)
RuntimeFungibleTokenTransferVM-43.01k ± 0%3.00k ± 0%~(p=1.000 n=1+1)
RuntimeResourceDictionaryValues-436.7k ± 0%36.7k ± 0%~(all equal)
RuntimeResourceTracking-4159k ± 0%159k ± 0%~(p=1.000 n=1+1)
RuntimeScriptNoop-4113 ± 0%113 ± 0%~(all equal)
RuntimeVMInvokeContractImperativeFib-4197 ± 0%197 ± 0%~(all equal)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
ValueIsSubtypeOfSemaType-41.00 ± 0%1.00 ± 0%~(all equal)
 

Copy link
Copy Markdown
Member

@SupunS SupunS left a comment

Choose a reason for hiding this comment

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

Good idea!

@turbolent turbolent merged commit a942168 into master Nov 13, 2025
12 of 15 checks passed
@turbolent turbolent deleted the bastian/optimize-event-emission branch November 13, 2025 00:05
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