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

Atree register inlining for v1.0 #3048

Conversation

turbolent
Copy link
Member

@turbolent turbolent commented Jan 26, 2024

Description

Rebase of feature/atree-register-inlining-v0.42 on top of master.

I attempted merging master and resolving conflicts multiple times, but the sheer amount of conflicts and differences made it impossible for me. Rebasing allowed making semantically equivalent changes, step-by-step.


  • 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

fxamacker and others added 30 commits January 25, 2024 13:55
This commit updates Cadence to use new Atree API
- Array.Get()
- OrderedMap.Get()
- MaxInlineArrayElementSize()
- MaxInlineMapKeySize()

For more info, see Atree PRs:
- onflow/atree#314
- onflow/atree#316
- onflow/atree#318
This commit updates Cadence to use new Atree API
- Array.SlabID()
- OrderedMap.SlabID()
- SlabID
- SlabIndex
- etc.

For more info, see Atree PRs:
- onflow/atree#322
- onflow/atree#323
- onflow/atree#324
Currently, Array.SlabID() and OrderedMap.SlabID() are used as
identifier to track resources, etc because slab IDs are guaranteed
to unique. However, atree slab ID should be only used to retrieve
slabs (registers) from storage.

Also, when Atree register inlining is implemented in the future, some
resources may not be stored in separate slabs, so they will not have
slab IDs anymore.

This commit uses Array.ValueID() and OrderedMap.ValueID() to uniquely
identify resources.
This commit updates Cadence to use newer version of onflow/atree
with register inlining and data deduplication features.

- Updated StorableDecoder callbacks

- Updated StringAtreeValue to implement atree.ComparableStorable

- Updated SomeStorable to implement atree.ContainerStorable

- Updated these structs to implement TypeInfo interface:
  * compositeTypeInfo
  * EmptyTypeInfo
  * VariableSizedStaticType
  * ConstantSizedStaticType
  * DictionaryStaticType

- Updated decodeStorable() to decode atree inlined array, map, and
  compact map

- Updated to use ReadOnly iterators when possible

- Updated to use NonReadOnly iterators
Co-authored-by: Bastian Müller <bastian@turbolent.com>
Co-authored-by: Bastian Müller <bastian@turbolent.com>
Co-authored-by: Supun Setunga <supun.setunga@gmail.com>
Currently (in master), there are 4 cases of SomeStorable encoding:
- SomeStorable and its content are encoded inline
- SomeStorable is encoded inline while its content is encoded
  in separate slab when content size exceeds size limit
- SomeStorable and its content are encoded in separate slab
  when content size fits withinin size limit but SomeStorable
  plus its content size exceeds size limit
- SomeStorable and its content are encoded across multiple slabs
  when there are multiple nested SomeStorable and some of them
  exceeds size limit

Given this, adding Atree Inlining feature (onflow/atree PR 342) makes encoding
even more complicated for SomeStorable when it contains inlined array or map.

This commit simplifies encoding by removing the last 2 cases, so SomeStorable can be
encoded in two ways:
- if innermost value is too large, innermost value is encoded in a separate slab
  while SomeStorable wrapper is encoded inline with reference to slab containing
  innermost value.
- otherwise, SomeStorable with innermost value is encoded inline.

The above applies to both immutable innermost value (such as StringValue),
and mutable innermost value (such as ArrayValue).

Additionally, this commit also adds new efficient encoding for SomeStorable
when it has multiple nested levels (SomeStorable containing SomeStorable).
Co-authored-by: Bastian Müller <bastian@turbolent.com>
Cadence smoke test is reporting false alarm about
"slab was not reachable".

To summarize, storage.CheckHealth() should be called after
array.DeepRemove(), rather than in the middle of array.DeepRemove().

CheckHealth() is called in the middle of array.DeepRemove() when:
- array.DeepRemove() calls childArray1 and childArray2 DeepRemove()
- DeepRemove() calls maybeValidateAtreeValue()
- maybeValidateAtreeValue() calls CheckHealth()

The details are more complex than this oversimplified summary.

For more context, see comments/discussion on GitHub at
#2882 (comment)
Cadence smoke test reported another false alarm about
"slab was not reachable".

To summarize, storage.CheckHealth() should be called after
DictionaryValue.Transfer() with remove flag, rather than
in the middle of DictionaryValue.Transfer() with remove flag.

The details are more complex than this oversimplified summary.

For more context, see GitHub comments at
#2882 (comment)
This commit adds a new parameter atRoot to Transfer().  Atree storage
is validated only if both remove and atRoot parameters are true in
ArrayValue.Transfer(), DictionaryValue.Transfer(), and
CompositeValue.Transfer().

Currently in ArrayValue.Transfer(), DictionaryValue.Transfer(),
and CompositeValue.Transfer() if remove parameter is true:
- atree storage is validated
- container slab is removed

However, when transferring nested ArrayValue, DictionaryValue,
and CompositeValue with remove flag, atree storage validation can
fail because child slab is removed while parent container is not
reset completely yet.

This validation problem is similar to the previously fixed
atree storage validation problem during nested DeepRemove().
If remove parameter is true in Transfer(), iterated values are
removed so non-readonly iterators need to be used.
fxamacker and others added 4 commits January 30, 2024 15:42
Entitlement migration was broken because new dictionary value
was created with zero address using NewDictionaryValue().

This commit fixes entitlement migration by creating new dictionary
value with owner address using NewDictionaryValueWithAddress().
@turbolent
Copy link
Member Author

turbolent commented Jan 30, 2024

@fxamacker Great work fixing the migrations! 👏

Out of curiosity: why were the migrations working in v0.42 without those equality functions? 🤔

@fxamacker
Copy link
Member

Why doesn't CompositeStaticType implement TypeInfo?

@turbolent CompositeStaticType doesn't implement atree.TypeInfo is because we only creates atree.OrderedMap with compositeTypeInfo for CompositeValue.

dictionary, err := atree.NewMap(
config.Storage,
atree.Address(address),
atree.NewDefaultDigesterBuilder(),
NewCompositeTypeInfo(
interpreter,
location,
qualifiedIdentifier,
kind,
),
)

Copy link

codecov bot commented Jan 30, 2024

Codecov Report

Attention: Patch coverage is 84.70982% with 137 lines in your changes are missing coverage. Please review.

Project coverage is 80.62%. Comparing base (40b4d69) to head (fccfa81).

Files Patch % Lines
runtime/interpreter/value.go 89.30% 48 Missing and 3 partials ⚠️
runtime/interpreter/decode.go 65.90% 25 Missing and 5 partials ⚠️
runtime/interpreter/encode.go 66.66% 10 Missing and 2 partials ⚠️
migrations/legacy_intersection_type.go 70.00% 5 Missing and 1 partial ⚠️
runtime/convertValues.go 82.35% 4 Missing and 2 partials ⚠️
runtime/interpreter/statictype.go 86.66% 4 Missing ⚠️
runtime/interpreter/storagemapkey.go 80.00% 4 Missing ⚠️
runtime/interpreter/value_link.go 33.33% 4 Missing ⚠️
runtime/interpreter/value_function.go 0.00% 3 Missing ⚠️
migrations/legacy_primitivestatic_type.go 75.00% 2 Missing ⚠️
... and 9 more
Additional details and impacted files
@@                           Coverage Diff                            @@
##           feature/atree-register-inlining-v1.0    #3048      +/-   ##
========================================================================
+ Coverage                                 80.60%   80.62%   +0.02%     
========================================================================
  Files                                       379      379              
  Lines                                     91151    91604     +453     
========================================================================
+ Hits                                      73468    73854     +386     
- Misses                                    15049    15110      +61     
- Partials                                   2634     2640       +6     
Flag Coverage Δ
unittests 80.62% <84.70%> (+0.02%) ⬆️

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

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@fxamacker
Copy link
Member

Out of curiosity: why were the migrations working in v0.42 without those equality functions?

Those equality functions are for Cadence 1.0 migration and are not part of Cadence v0.42 migration with atree inlining.

@turbolent turbolent self-assigned this Jan 31, 2024
@turbolent turbolent mentioned this pull request Jan 31, 2024
6 tasks
@turbolent
Copy link
Member Author

The only remaining TODO here is to fix one of the subtests ("resource, move from array to array") in TestNestedContainerMutationAfterMove, which I skipped for now.

It's been a while, but as far as I can remember I added this test case in the atree register inlining feature to "prepare" for the changes to come, to basically have a regression test for "complex" operations on containers, like moving children between different containers.

I think I need to update it to the new reference semantics in 1.0, i.e. references becoming invalid when the resource is moved.

@SupunS does this sound about right?

@turbolent
Copy link
Member Author

Adjusted and unskipped the test case in 6713e43

@SupunS
Copy link
Member

SupunS commented Feb 1, 2024

adjustment for the test 6713e43 looks correct 👍

Copy link
Member

@fxamacker fxamacker left a comment

Choose a reason for hiding this comment

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

Nice! 👍 I reviewed mostly non-test code against the other branch. I manually looked for any commits that might have been missed because the git range-diff had issues (maybe my bad).

There are some places in value.go that can use readonly iterators, such as ArrayValue.IsImportable(). I can open a separate PR for those, which would also address issue #2836.

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.

Took a look at the diff of diffs and it looks similar enough to me.

runtime/storage_test.go Show resolved Hide resolved
This commit bumps atree version to benefit from deduplication
of atree inlined Cadence dictionary type info.

For more info, see
onflow/atree#369
…inlining-v1.0

  Bump atree version in Cadence v1.0 for new features (e.g. deduplication of Cadence dictionary type info)
Currently, we are not sure if all uses can be guaranteed to
be readonly in two functions, so this commit uses atree
non-readonly iterators in:
- CompositeValue.ForEachFieldName
- DictionaryValue.IterateKeys

Also added TODO to determine if all uses can be guaranteed to be
readonly for these, which would allow us to revert this change.
…rators

Use atree readonly iterators when mutation of values is not needed
@turbolent turbolent merged commit b591a0d into feature/atree-register-inlining-v1.0 Apr 8, 2024
8 of 11 checks passed
@turbolent turbolent deleted the bastian/atree-register-inlining-v1.0 branch April 8, 2024 18:29
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.

4 participants