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

Add more purity annotations to built-in and standard library function types #2125

Merged
merged 2 commits into from
Nov 7, 2022

Conversation

turbolent
Copy link
Member

Work towards #2084

Description

Updating the NFT Storefront contracts identified that some more built-in and standard library functions are impure, but could be view: onflow/nft-storefront#74 (comment)

Add explicit purity annotations to more built-in and standard library function types, make functions view when possible.


  • 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 requested a review from a team November 7, 2022 18:35
@turbolent turbolent requested a review from SupunS as a code owner November 7, 2022 18:35
@turbolent turbolent self-assigned this Nov 7, 2022
@turbolent turbolent requested a review from dsainati1 as a code owner November 7, 2022 18:35
@@ -145,8 +149,8 @@ func NewChecker(
}

functionActivations := &FunctionActivations{}
functionActivations.EnterFunction(&FunctionType{
Copy link
Member Author

Choose a reason for hiding this comment

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

Can save an allocation for each new checker

sema.ByteArrayType,
),
},
sema.ToBigEndianBytesFunctionType,
Copy link
Member Author

Choose a reason for hiding this comment

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

Use existing function type

@@ -3059,10 +3059,7 @@ var typeFunction = NewUnmeteredHostFunctionValue(
staticType := ConvertSemaToStaticType(invocation.Interpreter, ty)
return NewTypeValue(invocation.Interpreter, staticType)
},
&sema.FunctionType{
Copy link
Member Author

Choose a reason for hiding this comment

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

Move to sema, define a function type like for all other run-time type constructors

@@ -661,8 +662,10 @@ func OptionalTypeMapFunctionType(typ Type) *FunctionType {
TypeParameter: typeParameter,
}

const functionPurity = FunctionPurityImpure
Copy link
Member Author

Choose a reason for hiding this comment

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

Ensure the purities stay in sync

Copy link
Contributor

@dsainati1 dsainati1 left a comment

Choose a reason for hiding this comment

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

Thanks for updating these.

One quick suggestion, AccountForEachFunctionType is defined in publicaccount_type.go and should be impure. It is in practice because that's the default, but since we are making the default explicit elsewhere in this PR it would be nice to update that function as well.

@turbolent
Copy link
Member Author

@dsainati1 Good point! Added in 46069fb

@codecov
Copy link

codecov bot commented Nov 7, 2022

Codecov Report

Merging #2125 (46069fb) into feature/stable-cadence (ce3978a) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@                   Coverage Diff                   @@
##           feature/stable-cadence    #2125   +/-   ##
=======================================================
  Coverage                   77.99%   77.99%           
=======================================================
  Files                         306      306           
  Lines                       64134    64138    +4     
=======================================================
+ Hits                        50021    50027    +6     
+ Misses                      12353    12352    -1     
+ Partials                     1760     1759    -1     
Flag Coverage Δ
unittests 77.99% <100.00%> (+<0.01%) ⬆️

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

Impacted Files Coverage Δ
runtime/interpreter/value.go 70.39% <ø> (ø)
runtime/sema/authaccount_contracts.go 100.00% <ø> (ø)
runtime/stdlib/account.go 88.77% <ø> (ø)
runtime/stdlib/block.go 88.17% <ø> (ø)
runtime/stdlib/random.go 88.23% <ø> (ø)
runtime/stdlib/test.go 21.86% <ø> (-0.23%) ⬇️
runtime/interpreter/interpreter.go 89.12% <100.00%> (ø)
runtime/sema/authaccount_type.go 100.00% <100.00%> (ø)
runtime/sema/checker.go 92.33% <100.00%> (ø)
runtime/sema/publicaccount_type.go 100.00% <100.00%> (ø)
... and 1 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

github-actions bot commented Nov 7, 2022

Cadence Benchstat comparison

This branch with compared with the base branch onflow:feature/stable-cadence commit ce3978a
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-2120µs ± 1%120µs ± 1%~(p=0.073 n=7+6)
ContractInterfaceFungibleToken-241.4µs ± 3%41.1µs ± 1%~(p=0.310 n=6+6)
InterpretRecursionFib-22.46ms ± 2%2.39ms ± 1%−2.79%(p=0.004 n=6+6)
NewInterpreter/new_interpreter-21.12µs ± 1%1.12µs ± 2%~(p=0.794 n=6+6)
NewInterpreter/new_sub-interpreter-2600ns ± 1%597ns ± 1%~(p=0.366 n=7+6)
ParseArray-28.45ms ± 6%8.24ms ± 0%~(p=0.639 n=7+5)
ParseDeploy/byte_array-212.7ms ± 2%12.5ms ± 2%~(p=0.181 n=7+6)
ParseDeploy/decode_hex-21.23ms ± 1%1.22ms ± 2%~(p=0.128 n=7+7)
ParseFungibleToken/With_memory_metering-2193µs ± 4%191µs ± 1%~(p=0.259 n=7+7)
ParseFungibleToken/Without_memory_metering-2154µs ± 1%155µs ± 4%~(p=0.366 n=6+7)
ParseInfix-27.22µs ± 1%7.16µs ± 0%−0.92%(p=0.001 n=7+6)
QualifiedIdentifierCreation/One_level-22.35ns ± 0%2.55ns ± 0%+8.74%(p=0.001 n=7+6)
QualifiedIdentifierCreation/Three_levels-2141ns ± 0%139ns ± 2%−1.84%(p=0.022 n=6+7)
RuntimeFungibleTokenTransfer-2594µs ±40%584µs ±30%~(p=1.000 n=7+7)
RuntimeResourceDictionaryValues-25.20ms ± 1%5.21ms ± 2%~(p=1.000 n=7+6)
RuntimeScriptNoop-221.5µs ±30%20.1µs ±26%~(p=0.456 n=7+7)
SuperTypeInference/arrays-2315ns ± 2%315ns ± 3%~(p=0.710 n=7+7)
SuperTypeInference/composites-2130ns ± 0%130ns ± 0%+0.17%(p=0.017 n=6+7)
SuperTypeInference/integers-293.9ns ± 0%94.8ns ± 0%+0.88%(p=0.001 n=7+7)
ValueIsSubtypeOfSemaType-293.5ns ± 2%95.3ns ± 2%+1.96%(p=0.038 n=7+7)
 
alloc/opdelta
CheckContractInterfaceFungibleTokenConformance-251.6kB ± 0%51.6kB ± 0%~(all equal)
ContractInterfaceFungibleToken-224.9kB ± 0%24.9kB ± 0%~(p=0.538 n=7+6)
InterpretRecursionFib-21.00MB ± 0%1.00MB ± 0%~(p=1.000 n=6+6)
NewInterpreter/new_interpreter-2752B ± 0%752B ± 0%~(all equal)
NewInterpreter/new_sub-interpreter-2200B ± 0%200B ± 0%~(all equal)
ParseArray-22.75MB ± 3%2.68MB ± 2%~(p=0.318 n=7+7)
ParseDeploy/byte_array-24.29MB ± 0%4.18MB ± 3%−2.58%(p=0.008 n=6+7)
ParseDeploy/decode_hex-2214kB ± 0%214kB ± 0%~(p=0.580 n=7+7)
ParseFungibleToken/With_memory_metering-229.8kB ± 0%29.8kB ± 0%~(p=0.878 n=7+7)
ParseFungibleToken/Without_memory_metering-229.8kB ± 0%29.8kB ± 0%~(p=1.000 n=7+7)
ParseInfix-21.93kB ± 0%1.93kB ± 0%~(p=0.831 n=7+7)
QualifiedIdentifierCreation/One_level-20.00B 0.00B ~(all equal)
QualifiedIdentifierCreation/Three_levels-264.0B ± 0%64.0B ± 0%~(all equal)
RuntimeFungibleTokenTransfer-2103kB ± 1%103kB ± 1%~(p=0.128 n=7+7)
RuntimeResourceDictionaryValues-22.28MB ± 0%2.28MB ± 0%~(p=0.165 n=7+7)
RuntimeScriptNoop-28.48kB ± 0%8.49kB ± 0%~(p=0.333 n=7+7)
SuperTypeInference/arrays-296.0B ± 0%96.0B ± 0%~(all equal)
SuperTypeInference/composites-20.00B 0.00B ~(all equal)
SuperTypeInference/integers-20.00B 0.00B ~(all equal)
ValueIsSubtypeOfSemaType-248.0B ± 0%48.0B ± 0%~(all equal)
 
allocs/opdelta
CheckContractInterfaceFungibleTokenConformance-2905 ± 0%905 ± 0%~(all equal)
ContractInterfaceFungibleToken-2435 ± 0%435 ± 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.098 n=6+7)
ParseDeploy/byte_array-2104k ± 0%104k ± 0%−0.03%(p=0.042 n=6+7)
ParseDeploy/decode_hex-275.0 ± 0%75.0 ± 0%~(all equal)
ParseFungibleToken/With_memory_metering-21.00k ± 0%1.00k ± 0%~(all equal)
ParseFungibleToken/Without_memory_metering-21.00k ± 0%1.00k ± 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.06k ± 0%2.06k ± 0%−0.19%(p=0.001 n=7+7)
RuntimeResourceDictionaryValues-237.0k ± 0%37.0k ± 0%~(p=0.939 n=7+5)
RuntimeScriptNoop-2140 ± 0%140 ± 0%~(all equal)
SuperTypeInference/arrays-23.00 ± 0%3.00 ± 0%~(all equal)
SuperTypeInference/composites-20.00 0.00 ~(all equal)
SuperTypeInference/integers-20.00 0.00 ~(all equal)
ValueIsSubtypeOfSemaType-21.00 ± 0%1.00 ± 0%~(all equal)
 

@turbolent turbolent requested review from dsainati1 and a team November 7, 2022 20:32
Copy link
Contributor

@dsainati1 dsainati1 left a comment

Choose a reason for hiding this comment

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

LGTM!

@turbolent turbolent requested a review from a team November 7, 2022 22:02
@@ -193,6 +195,7 @@ Returns nil if no contract/contract interface with the given name exists in the
`

var AuthAccountContractsTypeRemoveFunctionType = &FunctionType{
Purity: FunctionPurityImpure,
Copy link
Member

Choose a reason for hiding this comment

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

Doesn't have to be in this PR, but maybe we could replace the struct literals with a constructor to enforce the proper purity status

@turbolent turbolent merged commit 3394b66 into feature/stable-cadence Nov 7, 2022
@turbolent turbolent deleted the bastian/more-view-functions branch November 7, 2022 22: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.

3 participants