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

Improve resource tracking for optional binding #2044

Merged
merged 2 commits into from Oct 11, 2022

Conversation

turbolent
Copy link
Member

@turbolent turbolent commented Oct 7, 2022

Closes dapperlabs/cadence-private-issues#57

Port of dapperlabs/cadence-internal#104

Description

Optional-bindings, if-statements with variable declarations as a test, were implemented incorrectly.
The variable declaration was correctly only declared inside of the then-branch.
However, incorrectly, the value(s) were also only considered evaluated in the then branch. This produced incorrect resource-tracking errors, requiring users to unnecessarily invalidate the value in the else-branch, even though it should have been already definitely invalidated.

The variable declaration was initially only considered evaluated in the then-branch to support a special case: failable casing of resources (r as? @R). The failable cast was always definitely invalidating the casted variable, so it needed to be only occur in the then-branch. The proper way to handle this case is to always evaluate the variable declaration's value (as described above), but specifically handle the special construct of failable casting, and consider the casted variable invalidated in the then branch (the cast succeeded), and consider it not-invalidated in the else-branch (the cast failed).

This is a breaking change, as it also now makes the unnecessary invalidation in the else-branch illegal.


  • 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

@turbolent turbolent added Bugfix Language Breaking Change Breaks Cadence contracts deployed on Mainnet labels Oct 7, 2022
@turbolent turbolent requested a review from a team October 7, 2022 21:28
@turbolent turbolent self-assigned this Oct 7, 2022
@codecov
Copy link

codecov bot commented Oct 7, 2022

Codecov Report

Merging #2044 (8b6f324) into feature/stable-cadence (2d2a84e) will decrease coverage by 0.00%.
The diff coverage is 100.00%.

@@                    Coverage Diff                     @@
##           feature/stable-cadence    #2044      +/-   ##
==========================================================
- Coverage                   77.70%   77.70%   -0.01%     
==========================================================
  Files                         304      304              
  Lines                       63108    63125      +17     
==========================================================
+ Hits                        49037    49050      +13     
- Misses                      12360    12364       +4     
  Partials                     1711     1711              
Flag Coverage Δ
unittests 77.70% <100.00%> (-0.01%) ⬇️

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

Impacted Files Coverage Δ
runtime/sema/check_casting_expression.go 98.42% <100.00%> (+0.01%) ⬆️
runtime/sema/check_conditional.go 95.13% <100.00%> (+0.40%) ⬆️
runtime/sema/check_variable_declaration.go 97.20% <100.00%> (+0.06%) ⬆️
runtime/sema/errors.go 73.47% <0.00%> (-0.37%) ⬇️

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

github-actions bot commented Oct 7, 2022

Cadence Benchstat comparison

This branch with compared with the base branch onflow:feature/stable-cadence commit 2d2a84e
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-2171µs ± 3%169µs ± 3%~(p=0.535 n=7+7)
ContractInterfaceFungibleToken-251.7µs ± 3%51.1µs ± 3%~(p=0.445 n=6+7)
InterpretRecursionFib-23.20ms ± 3%3.19ms ± 3%~(p=0.818 n=6+6)
NewInterpreter/new_interpreter-21.48µs ± 1%1.46µs ± 2%~(p=0.128 n=7+7)
NewInterpreter/new_sub-interpreter-2828ns ± 2%849ns ± 8%~(p=0.445 n=6+7)
ParseArray-210.1ms ± 2%10.3ms ± 7%~(p=1.000 n=7+7)
ParseDeploy/byte_array-215.4ms ± 3%15.2ms ± 4%~(p=0.366 n=6+7)
ParseDeploy/decode_hex-21.46ms ± 2%1.48ms ± 4%~(p=0.383 n=7+7)
ParseFungibleToken/With_memory_metering-2242µs ± 3%234µs ± 3%−3.32%(p=0.011 n=7+7)
ParseFungibleToken/Without_memory_metering-2191µs ± 3%187µs ± 1%−1.76%(p=0.022 n=7+6)
ParseInfix-28.65µs ± 2%8.54µs ± 3%~(p=0.097 n=7+7)
QualifiedIdentifierCreation/One_level-22.82ns ± 1%2.81ns ± 1%~(p=0.137 n=7+7)
QualifiedIdentifierCreation/Three_levels-2175ns ± 3%169ns ± 3%−3.23%(p=0.011 n=7+7)
RuntimeFungibleTokenTransfer-2797µs ±20%700µs ±28%~(p=0.128 n=7+7)
RuntimeResourceDictionaryValues-26.44ms ± 6%6.35ms ± 2%~(p=0.731 n=7+6)
RuntimeScriptNoop-224.2µs ±33%23.5µs ±38%~(p=0.710 n=7+7)
ValueIsSubtypeOfSemaType-2114ns ± 3%111ns ± 1%−2.63%(p=0.008 n=7+6)
 
alloc/opdelta
CheckContractInterfaceFungibleTokenConformance-261.8kB ± 0%61.4kB ± 0%−0.60%(p=0.002 n=6+6)
ContractInterfaceFungibleToken-224.1kB ± 0%24.1kB ± 0%~(all equal)
InterpretRecursionFib-21.23MB ± 0%1.23MB ± 0%~(p=0.409 n=7+7)
NewInterpreter/new_interpreter-2848B ± 0%848B ± 0%~(all equal)
NewInterpreter/new_sub-interpreter-2264B ± 0%264B ± 0%~(all equal)
ParseArray-22.78MB ± 4%2.73MB ± 4%~(p=0.097 n=7+7)
ParseDeploy/byte_array-24.27MB ± 4%4.20MB ± 3%~(p=0.097 n=7+7)
ParseDeploy/decode_hex-2214kB ± 0%214kB ± 0%~(p=0.056 n=7+7)
ParseFungibleToken/With_memory_metering-229.8kB ± 0%29.8kB ± 0%~(p=0.364 n=7+7)
ParseFungibleToken/Without_memory_metering-229.9kB ± 0%29.9kB ± 0%~(p=0.121 n=6+5)
ParseInfix-21.93kB ± 0%1.93kB ± 0%~(p=0.214 n=7+7)
QualifiedIdentifierCreation/One_level-20.00B 0.00B ~(all equal)
QualifiedIdentifierCreation/Three_levels-264.0B ± 0%64.0B ± 0%~(all equal)
RuntimeFungibleTokenTransfer-2109kB ± 1%108kB ± 1%~(p=0.302 n=7+7)
RuntimeResourceDictionaryValues-22.27MB ± 0%2.28MB ± 0%~(p=0.456 n=7+7)
RuntimeScriptNoop-28.86kB ± 0%8.84kB ± 0%~(p=0.302 n=7+7)
ValueIsSubtypeOfSemaType-248.0B ± 0%48.0B ± 0%~(all equal)
 
allocs/opdelta
CheckContractInterfaceFungibleTokenConformance-21.04k ± 0%1.03k ± 0%−0.39%(p=0.001 n=7+7)
ContractInterfaceFungibleToken-2431 ± 0%431 ± 0%~(all equal)
InterpretRecursionFib-222.5k ± 0%22.5k ± 0%~(all equal)
NewInterpreter/new_interpreter-214.0 ± 0%14.0 ± 0%~(all equal)
NewInterpreter/new_sub-interpreter-25.00 ± 0%5.00 ± 0%~(all equal)
ParseArray-269.6k ± 0%69.6k ± 0%~(p=0.286 n=7+7)
ParseDeploy/byte_array-2104k ± 0%104k ± 0%~(p=0.286 n=7+7)
ParseDeploy/decode_hex-275.0 ± 0%75.0 ± 0%~(all equal)
ParseFungibleToken/With_memory_metering-21.01k ± 0%1.01k ± 0%~(all equal)
ParseFungibleToken/Without_memory_metering-21.01k ± 0%1.01k ± 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.16k ± 0%2.16k ± 0%~(p=0.245 n=7+6)
RuntimeResourceDictionaryValues-236.9k ± 0%36.9k ± 0%~(p=0.720 n=7+7)
RuntimeScriptNoop-2145 ± 0%145 ± 0%~(all equal)
ValueIsSubtypeOfSemaType-21.00 ± 0%1.00 ± 0%~(all equal)
 

@turbolent turbolent requested a review from a team October 8, 2022 00:03
@turbolent turbolent merged commit abc0b07 into feature/stable-cadence Oct 11, 2022
@turbolent turbolent deleted the bastian/fix-optional-binding branch October 11, 2022 17:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bugfix Language Breaking Change Breaks Cadence contracts deployed on Mainnet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants