cadence icon indicating copy to clipboard operation
cadence copied to clipboard

Storage Iteration functions for accounts

Open dsainati1 opened this issue 2 years ago • 2 comments

Part of https://github.com/onflow/cadence/issues/208

Description

This adds three new functions to the AuthAccount object forEachPublic, forEachPrivate and forEachStored (as well as just forEachPublic to PublicAccount) that iterate over all the paths (and the types of the objects stored there) in the respective domains of the account.

Each of these functions takes a function parameter of type (X, Type): Bool (where X is the type of the path being iterated over by the function in question (PublicPath, PrivatePath and StoragePath respectively)), which is applied to every element stored in that account under the domain in question. The first parameter holds the path of the element, and the second holds its runtime type.

These paths and types are loaded from storage one by one, to avoid loading the entirety of storage into memory in the case of large accounts.

The specific order in which the objects are iterated over is undefined, as is the behavior when a path is added or removed from storage, whether by linking, unlinking, saving or loading. The behavior is, however, deterministic; we make no guarantees about what we do but we do the same thing every time.


  • [X] Targeted PR against master branch
  • [X] Linked to Github issue with discussion and accepted design OR link to spec that describes this work
  • [X] Code follows the standards mentioned here
  • [X] Updated relevant documentation
  • [X] Re-reviewed Files changed in the Github PR explorer
  • [X] Added appropriate labels

dsainati1 avatar Jul 27 '22 19:07 dsainati1

These paths and types are loaded from storage one by one.

Why do we load full value here ? Shouldn't we load type info only ? This can cause big usability problems.

bluesign avatar Jul 27 '22 21:07 bluesign

We do load only the runtime type info here, the actual stored value itself is not loaded.

dsainati1 avatar Jul 27 '22 21:07 dsainati1

Codecov Report

Merging #1851 (13f2999) into master (2982f32) will increase coverage by 0.03%. The diff coverage is 96.26%.

@@            Coverage Diff             @@
##           master    #1851      +/-   ##
==========================================
+ Coverage   77.78%   77.82%   +0.03%     
==========================================
  Files         294      294              
  Lines       62403    62508     +105     
==========================================
+ Hits        48540    48644     +104     
  Misses      12137    12137              
- Partials     1726     1727       +1     
Flag Coverage Δ
unittests 77.82% <96.26%> (+0.03%) :arrow_up:

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

Impacted Files Coverage Δ
runtime/interpreter/interpreter.go 89.63% <89.74%> (+<0.01%) :arrow_up:
runtime/interpreter/account.go 89.69% <100.00%> (+0.80%) :arrow_up:
runtime/interpreter/value.go 72.14% <100.00%> (+0.02%) :arrow_up:
runtime/sema/authaccount_type.go 100.00% <100.00%> (ø)
runtime/sema/publicaccount_type.go 100.00% <100.00%> (ø)
runtime/interpreter/storage.go 69.04% <0.00%> (-1.59%) :arrow_down:
runtime/sema/check_event_declaration.go 100.00% <0.00%> (+5.76%) :arrow_up:

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

codecov[bot] avatar Aug 17 '22 15:08 codecov[bot]

Cadence Benchstat comparison

This branch with compared with the base branch onflow:master commit 2982f325bbba9b25613fac43f331cf66ce1b49cb 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-2136µs ± 1%137µs ± 2%~(p=0.209 n=7+7)
ContractInterfaceFungibleToken-240.2µs ± 1%40.1µs ± 1%~(p=0.456 n=7+7)
InterpretRecursionFib-23.59ms ± 1%3.55ms ± 6%~(p=0.165 n=7+7)
NewInterpreter/new_interpreter-21.18µs ± 1%1.18µs ± 1%~(p=0.312 n=6+7)
NewInterpreter/new_sub-interpreter-22.43µs ± 1%2.44µs ± 2%~(p=0.731 n=6+7)
ParseArray-28.62ms ± 1%8.71ms ± 2%~(p=0.240 n=6+6)
ParseDeploy/byte_array-213.0ms ± 2%13.7ms ± 6%+4.93%(p=0.017 n=7+7)
ParseDeploy/decode_hex-21.14ms ± 1%1.15ms ± 2%~(p=0.138 n=6+7)
ParseFungibleToken/With_memory_metering-2210µs ± 2%206µs ± 1%−1.65%(p=0.022 n=7+6)
ParseFungibleToken/Without_memory_metering-2159µs ± 1%161µs ± 2%+1.27%(p=0.015 n=6+6)
ParseInfix-27.77µs ± 2%7.78µs ± 1%~(p=0.589 n=6+6)
QualifiedIdentifierCreation/One_level-22.68ns ± 0%2.35ns ± 0%−12.45%(p=0.001 n=7+7)
QualifiedIdentifierCreation/Three_levels-2138ns ± 0%137ns ± 2%~(p=0.181 n=7+6)
RuntimeFungibleTokenTransfer-21.03ms ±21%0.85ms ± 2%~(p=0.181 n=7+6)
RuntimeResourceDictionaryValues-26.42ms ± 2%6.40ms ± 3%~(p=0.805 n=7+7)
ValueIsSubtypeOfSemaType-290.1ns ± 1%89.3ns ± 3%~(p=0.534 n=6+7)
 
alloc/opdelta
CheckContractInterfaceFungibleTokenConformance-262.5kB ± 0%62.5kB ± 0%~(p=1.000 n=7+7)
ContractInterfaceFungibleToken-224.4kB ± 0%24.4kB ± 0%~(p=0.462 n=7+7)
InterpretRecursionFib-21.51MB ± 0%1.51MB ± 0%~(p=0.294 n=7+6)
NewInterpreter/new_interpreter-2928B ± 0%928B ± 0%~(all equal)
NewInterpreter/new_sub-interpreter-21.46kB ± 0%1.46kB ± 0%~(all equal)
ParseArray-22.79MB ± 2%2.82MB ± 2%~(p=0.079 n=7+7)
ParseDeploy/byte_array-24.33MB ± 0%4.42MB ± 3%~(p=0.836 n=6+7)
ParseDeploy/decode_hex-2214kB ± 0%214kB ± 0%~(p=0.560 n=7+7)
ParseFungibleToken/With_memory_metering-236.3kB ± 0%36.3kB ± 0%~(p=0.640 n=7+7)
ParseFungibleToken/Without_memory_metering-236.3kB ± 0%36.3kB ± 0%~(p=1.000 n=7+7)
ParseInfix-22.17kB ± 0%2.17kB ± 0%~(p=0.709 n=7+7)
QualifiedIdentifierCreation/One_level-20.00B 0.00B ~(all equal)
QualifiedIdentifierCreation/Three_levels-264.0B ± 0%64.0B ± 0%~(all equal)
RuntimeFungibleTokenTransfer-2185kB ± 0%186kB ± 0%+0.52%(p=0.001 n=7+7)
RuntimeResourceDictionaryValues-22.23MB ± 0%2.23MB ± 0%~(p=0.318 n=7+7)
ValueIsSubtypeOfSemaType-248.0B ± 0%48.0B ± 0%~(all equal)
 
allocs/opdelta
CheckContractInterfaceFungibleTokenConformance-21.03k ± 0%1.03k ± 0%~(all equal)
ContractInterfaceFungibleToken-2428 ± 0%428 ± 0%~(all equal)
InterpretRecursionFib-233.5k ± 0%33.5k ± 0%~(all equal)
NewInterpreter/new_interpreter-213.0 ± 0%13.0 ± 0%~(all equal)
NewInterpreter/new_sub-interpreter-242.0 ± 0%42.0 ± 0%~(all equal)
ParseArray-270.0k ± 0%70.0k ± 0%~(p=1.000 n=7+7)
ParseDeploy/byte_array-2105k ± 0%105k ± 0%~(p=1.000 n=7+7)
ParseDeploy/decode_hex-277.0 ± 0%77.0 ± 0%~(all equal)
ParseFungibleToken/With_memory_metering-21.06k ± 0%1.06k ± 0%~(all equal)
ParseFungibleToken/Without_memory_metering-21.06k ± 0%1.06k ± 0%~(all equal)
ParseInfix-266.0 ± 0%66.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-23.50k ± 0%3.52k ± 0%+0.29%(p=0.001 n=7+6)
RuntimeResourceDictionaryValues-237.1k ± 0%37.1k ± 0%+0.01%(p=0.001 n=7+7)
ValueIsSubtypeOfSemaType-21.00 ± 0%1.00 ± 0%~(all equal)
 

github-actions[bot] avatar Aug 17 '22 15:08 github-actions[bot]