Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow embedders to reuse interpreter shared state #2100

Merged
merged 2 commits into from
Oct 28, 2022

Conversation

turbolent
Copy link
Member

@turbolent turbolent commented Oct 26, 2022

Closes #2048

Description

Based on @janezpodhostnik's idea and proof-of-concept:
Allow the embedder, e.g. FVM, to reuse the shared state across multiple runtime top-level entry calls (e.g. transaction invocation, contract function invocation).

If the embedder, through the new interface functions SetInterpreterSharedState and GetInterpreterSharedState, saves the state and re-supplies it in a subsequent top-level entry call, interpreters for imported contracts are re-used – contract data does not need to be re-read from storage, and contract values do not need to be recreated by the interpreter.

When enabling this optimization for BenchmarkRuntimeFungibleTokenTransfer, this optimization improves performance and allocations by ca ~16%:

6878	    172776 ns/op	  106226 B/op	    2002 allocs/op
8194	    145198 ns/op	   81168 B/op	    1645 allocs/op

  • 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

@bluesign
Copy link
Contributor

Very nice

Does this support rollback on state? Can I use this with debugger to rollback state for example? Like time machine.

Is this state is serializable?

runtime/runtime_test.go Outdated Show resolved Hide resolved
runtime/runtime_test.go Outdated Show resolved Hide resolved
Comment on lines 246 to 257
func (i *testRuntimeInterface) SetInterpreterSharedState(state *interpreter.SharedState) {
if i.setInterpreterSharedState == nil {
return
}

i.setInterpreterSharedState(state)
}

func (i *testRuntimeInterface) GetInterpreterSharedState() *interpreter.SharedState {
if i.getInterpreterShareState == nil {
return nil
}
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe panic here like the other testRuntimeInterface methods, since returning nil doesn't signify whether the operation failed or if the interface just doesn't have a fn pointer

Copy link
Member Author

Choose a reason for hiding this comment

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

GetInterpreterSharedState is an optional function, the embedder does not need to implement it. The runtime still works if this function is not implemented

runtime/sharedstate_test.go Outdated Show resolved Hide resolved
Base automatically changed from bastian/refactor-interpreter-config to master October 28, 2022 17:57
@turbolent
Copy link
Member Author

turbolent commented Oct 28, 2022

@bluesign

Does this support rollback on state? Can I use this with debugger to rollback state for example? Like time machine.
Is this state is serializable?

The word "state" here refers to the in-memory data structures that the interpreter builds up for executing programs. For example, there is one Interpreter instance per location (e.g. contract), and one interpreter value representing a contract object (a CompositeValue). The state also e.g. contains the call stack.

This interpreter state is unrelated to the "chain execution state", which is called "storage" in Cadence.

The interpreter state is not currently serializable.

@codecov
Copy link

codecov bot commented Oct 28, 2022

Codecov Report

Merging #2100 (3d44ae4) into master (c06ec60) will decrease coverage by 0.00%.
The diff coverage is 88.53%.

@@            Coverage Diff             @@
##           master    #2100      +/-   ##
==========================================
- Coverage   77.71%   77.71%   -0.01%     
==========================================
  Files         304      304              
  Lines       63529    63563      +34     
==========================================
+ Hits        49373    49398      +25     
- Misses      12401    12407       +6     
- Partials     1755     1758       +3     
Flag Coverage Δ
unittests 77.71% <88.53%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
runtime/interpreter/interpreter_tracing.go 68.04% <61.90%> (ø)
runtime/environment.go 91.73% <85.71%> (-0.25%) ⬇️
runtime/interpreter/value.go 70.69% <88.00%> (ø)
runtime/interpreter/interpreter.go 89.17% <98.11%> (ø)
runtime/interpreter/interpreter_expression.go 86.29% <100.00%> (ø)
runtime/interpreter/interpreter_import.go 84.00% <100.00%> (ø)
runtime/interpreter/interpreter_invocation.go 91.26% <100.00%> (ø)
runtime/interpreter/interpreter_statement.go 91.99% <100.00%> (ø)
runtime/interpreter/sharedstate.go 100.00% <100.00%> (ø)
runtime/interpreter/storagemap.go 83.45% <100.00%> (ø)
... and 3 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@github-actions
Copy link

Cadence Benchstat comparison

This branch with compared with the base branch onflow:master commit 77938db
The command for i in {1..N}; do go test ./... -run=XXX -bench=. -benchmem -shuffle=on; done was used.
Bench tests were run a total of 7 times on each branch.

Collapsed results for better readability

old.txtnew.txt
time/opdelta
CheckContractInterfaceFungibleTokenConformance-2113µs ± 1%113µs ± 1%~(p=0.295 n=7+6)
ContractInterfaceFungibleToken-241.1µs ± 1%42.4µs ± 9%~(p=0.805 n=7+7)
InterpretRecursionFib-22.51ms ± 1%2.52ms ± 1%~(p=0.535 n=7+7)
NewInterpreter/new_interpreter-21.14µs ± 2%1.12µs ± 0%−0.93%(p=0.039 n=6+6)
NewInterpreter/new_sub-interpreter-2589ns ± 3%582ns ± 1%~(p=0.343 n=7+6)
ParseArray-27.80ms ± 2%7.77ms ± 2%~(p=0.731 n=6+7)
ParseDeploy/byte_array-211.6ms ± 2%11.7ms ± 1%~(p=0.445 n=7+6)
ParseDeploy/decode_hex-21.20ms ± 4%1.19ms ± 2%~(p=0.456 n=7+7)
ParseFungibleToken/With_memory_metering-2182µs ± 1%184µs ± 3%~(p=0.836 n=6+7)
ParseFungibleToken/Without_memory_metering-2145µs ± 1%145µs ± 4%~(p=0.535 n=7+7)
ParseInfix-27.23µs ± 1%7.24µs ± 1%~(p=0.805 n=7+7)
QualifiedIdentifierCreation/One_level-22.41ns ± 0%2.42ns ± 1%~(p=0.387 n=6+6)
QualifiedIdentifierCreation/Three_levels-2117ns ± 1%117ns ± 0%~(p=0.292 n=6+6)
RuntimeFungibleTokenTransfer-2501µs ±27%555µs ±19%~(p=0.209 n=7+7)
RuntimeResourceDictionaryValues-25.12ms ± 1%5.11ms ± 1%~(p=0.620 n=7+7)
RuntimeScriptNoop-217.7µs ±37%20.2µs ±24%~(p=0.318 n=7+7)
ValueIsSubtypeOfSemaType-291.8ns ± 2%92.0ns ± 2%~(p=0.535 n=7+7)
 
alloc/opdelta
CheckContractInterfaceFungibleTokenConformance-250.1kB ± 0%50.1kB ± 0%~(p=0.462 n=7+7)
ContractInterfaceFungibleToken-224.2kB ± 0%24.2kB ± 0%~(p=0.462 n=7+7)
InterpretRecursionFib-21.00MB ± 0%1.00MB ± 0%~(all equal)
NewInterpreter/new_interpreter-2752B ± 0%752B ± 0%~(all equal)
NewInterpreter/new_sub-interpreter-2200B ± 0%200B ± 0%~(all equal)
ParseArray-22.74MB ± 3%2.70MB ± 2%~(p=0.303 n=7+7)
ParseDeploy/byte_array-24.20MB ± 2%4.23MB ± 3%~(p=0.646 n=7+7)
ParseDeploy/decode_hex-2214kB ± 0%214kB ± 0%~(p=0.878 n=7+7)
ParseFungibleToken/With_memory_metering-229.4kB ± 0%29.3kB ± 0%~(p=0.140 n=7+6)
ParseFungibleToken/Without_memory_metering-229.4kB ± 0%29.4kB ± 0%~(p=0.298 n=7+7)
ParseInfix-21.93kB ± 0%1.93kB ± 0%~(p=0.671 n=7+7)
QualifiedIdentifierCreation/One_level-20.00B 0.00B ~(all equal)
QualifiedIdentifierCreation/Three_levels-264.0B ± 0%64.0B ± 0%~(all equal)
RuntimeFungibleTokenTransfer-2102kB ± 1%103kB ± 1%~(p=0.902 n=7+7)
RuntimeResourceDictionaryValues-22.28MB ± 0%2.28MB ± 0%~(p=0.456 n=7+7)
RuntimeScriptNoop-28.64kB ± 0%8.64kB ± 1%~(p=0.435 n=7+7)
ValueIsSubtypeOfSemaType-248.0B ± 0%48.0B ± 0%~(all equal)
 
allocs/opdelta
CheckContractInterfaceFungibleTokenConformance-2900 ± 0%900 ± 0%~(all equal)
ContractInterfaceFungibleToken-2434 ± 0%434 ± 0%~(all equal)
InterpretRecursionFib-218.9k ± 0%18.9k ± 0%~(all equal)
NewInterpreter/new_interpreter-213.0 ± 0%13.0 ± 0%~(all equal)
NewInterpreter/new_sub-interpreter-24.00 ± 0%4.00 ± 0%~(all equal)
ParseArray-269.6k ± 0%69.6k ± 0%~(p=0.592 n=7+7)
ParseDeploy/byte_array-2104k ± 0%104k ± 0%~(p=0.592 n=7+7)
ParseDeploy/decode_hex-275.0 ± 0%75.0 ± 0%~(all equal)
ParseFungibleToken/With_memory_metering-20.99k ± 0%0.99k ± 0%~(all equal)
ParseFungibleToken/Without_memory_metering-20.99k ± 0%0.99k ± 0%~(all equal)
ParseInfix-260.0 ± 0%60.0 ± 0%~(all equal)
QualifiedIdentifierCreation/One_level-20.00 0.00 ~(all equal)
QualifiedIdentifierCreation/Three_levels-22.00 ± 0%2.00 ± 0%~(all equal)
RuntimeFungibleTokenTransfer-22.05k ± 0%2.05k ± 0%−0.03%(p=0.042 n=7+6)
RuntimeResourceDictionaryValues-237.0k ± 0%37.0k ± 0%~(p=0.126 n=7+7)
RuntimeScriptNoop-2141 ± 0%141 ± 0%~(all equal)
ValueIsSubtypeOfSemaType-21.00 ± 0%1.00 ± 0%~(all equal)
 

@turbolent turbolent merged commit 9c3aadb into master Oct 28, 2022
@turbolent turbolent deleted the bastian/allow-interpreter-shared-state-reuse branch October 28, 2022 18:33
@SaveTheRbtz
Copy link
Contributor

cc: @pattyshack

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.

Reuse interpreted programs within a transaction
6 participants