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

Decouple interpreter values from interpreter – Part 4 #3766

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

turbolent
Copy link
Member

Work towards #3693

Description

Move the core functionality of the value implementations of Bool and Int out of the interpreter package and into a new values package. This demonstrates how we could decouple at least some of the more basic values from the interpreter and use them in the VM.

If this approach looks good, we can also refactor the other number value types, and some other primitive value types like e.g. String.

I'm happy with the values package itself, but honestly not really happy with how the integration looks in the interpreter package's BoolValue and IntValue types, especially all the unwrapping and wrapping, casting, type checking, error handling, etc. Maybe this is just temporary, and once we have refactored the value types in the interpreter package fully from the interpreter this ugliness will go away.

Another idea I had was not to create a whole new values.Value type hierarchy with methods, but instead just have global functions, e.g. instead of having IntValue.LessEqual(other ComparableValue), just have IntLessEqual(IntValue, IntValue).

Alternatively we would have to duplicate this code in the VM, which seems worse to me (e.g. potential for mismatch).

Let's discuss this in the Implemention Sync.


  • 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 jsproz February 13, 2025 21:02
@turbolent turbolent self-assigned this Feb 13, 2025
@turbolent turbolent requested a review from SupunS as a code owner February 13, 2025 21:02
Copy link

Cadence Benchstat comparison

This branch with compared with the base branch onflow:master commit 14086f2
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

Comment on lines +22 to +28
"math/big"
"unsafe"

"github.com/onflow/atree"

"github.com/onflow/cadence/common"
"github.com/onflow/cadence/format"
Copy link
Member

Choose a reason for hiding this comment

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

No interpreter imports, nice! 👌

Comment on lines +94 to +97
o, ok := other.(IntValue)
if !ok {
return nil, InvalidOperandsError{}
}
Copy link
Member

@SupunS SupunS Feb 14, 2025

Choose a reason for hiding this comment

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

We could get rid of some of these type-casts by making the interfaces (NumberValue, IntegerValue) generic. e.g: https://github.com/onflow/cadence/compare/bastian/decouple-values-from-interpreter-4...supun/bind-interface-to-type?expand=1#diff-4ba970fa0343b83d635e65319f40746490752a9e69d40072f87221672ce16c81R23-R29

But the downside is, the interface, say IntegerValue, can only be used to define the structure of all the integer values. e.g:

var _ IntegerValue[IntValue] = IntValue{} // Ensures the `IntValue` implements `IntegerValue`

But cannot be actually used in the code, because IntegerValue interface type would always have to be used with a T, making the type too-sepicific.

var someValue Value = ...

 integerValue := someValue.(IntegerValue[IntValue]) // This becomes pointless since `IntegerValue[IntValue]` is not generic enough

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