Skip to content

Commit

Permalink
Merge pull request #1831 from onflow/bastian/optimize-interpreter-config
Browse files Browse the repository at this point in the history
Optimize interpreter configuration, reuse it in runtime environment
  • Loading branch information
turbolent authored Aug 19, 2022
2 parents 4041d52 + cb6c0f2 commit 38e80fb
Show file tree
Hide file tree
Showing 55 changed files with 1,240 additions and 1,724 deletions.
14 changes: 6 additions & 8 deletions encoding/json/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -736,17 +736,15 @@ func exportFromScript(t *testing.T, code string) cadence.Value {
inter, err := interpreter.NewInterpreter(
interpreter.ProgramFromChecker(checker),
checker.Location,
interpreter.WithUUIDHandler(
func() (uint64, error) {
&interpreter.Config{
UUIDHandler: func() (uint64, error) {
uuid++
return uuid, nil
},
),
interpreter.WithAtreeStorageValidationEnabled(true),
interpreter.WithAtreeValueValidationEnabled(true),
interpreter.WithStorage(
interpreter.NewInMemoryStorage(nil),
),
AtreeStorageValidationEnabled: true,
AtreeValueValidationEnabled: true,
Storage: interpreter.NewInMemoryStorage(nil),
},
)
require.NoError(t, err)

Expand Down
105 changes: 42 additions & 63 deletions runtime/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,47 +71,43 @@ func PrepareProgram(code string, location common.Location, codes map[common.Loca

var checkers = map[common.Location]*sema.Checker{}

func DefaultCheckerInterpreterOptions(
func DefaultCheckerOptions(
checkers map[common.Location]*sema.Checker,
codes map[common.Location]string,
) (
[]sema.Option,
[]interpreter.Option,
) {
) []sema.Option {
return []sema.Option{
sema.WithImportHandler(
func(checker *sema.Checker, importedLocation common.Location, _ ast.Range) (sema.Import, error) {
if importedLocation == stdlib.CryptoChecker.Location {
return sema.ElaborationImport{
Elaboration: stdlib.CryptoChecker.Elaboration,
}, nil
}

stringLocation, ok := importedLocation.(common.StringLocation)
if !ok {
return nil, &sema.CheckerError{
Location: checker.Location,
Codes: codes,
Errors: []error{
fmt.Errorf("cannot import `%s`. only files are supported", importedLocation),
},
}
}

importedChecker, ok := checkers[importedLocation]
if !ok {
importedProgram, _ := PrepareProgramFromFile(stringLocation, codes)
importedChecker, _ = checker.SubChecker(importedProgram, importedLocation)
checkers[importedLocation] = importedChecker
}

sema.WithImportHandler(
func(checker *sema.Checker, importedLocation common.Location, _ ast.Range) (sema.Import, error) {
if importedLocation == stdlib.CryptoChecker.Location {
return sema.ElaborationImport{
Elaboration: importedChecker.Elaboration,
Elaboration: stdlib.CryptoChecker.Elaboration,
}, nil
},
),
},
[]interpreter.Option{}
}

stringLocation, ok := importedLocation.(common.StringLocation)
if !ok {
return nil, &sema.CheckerError{
Location: checker.Location,
Codes: codes,
Errors: []error{
fmt.Errorf("cannot import `%s`. only files are supported", importedLocation),
},
}
}

importedChecker, ok := checkers[importedLocation]
if !ok {
importedProgram, _ := PrepareProgramFromFile(stringLocation, codes)
importedChecker, _ = checker.SubChecker(importedProgram, importedLocation)
checkers[importedLocation] = importedChecker
}

return sema.ElaborationImport{
Elaboration: importedChecker.Elaboration,
}, nil
},
),
}
}

// PrepareChecker prepares and initializes a checker with a given code as a string,
Expand All @@ -124,11 +120,7 @@ func PrepareChecker(
must func(error),
) (*sema.Checker, func(error)) {

defaultCheckerOptions, _ :=
DefaultCheckerInterpreterOptions(
checkers,
codes,
)
defaultCheckerOptions := DefaultCheckerOptions(checkers, codes)

defaultCheckerOptions = append(
defaultCheckerOptions,
Expand Down Expand Up @@ -176,38 +168,25 @@ func PrepareInterpreter(filename string, debugger *interpreter.Debugger) (*inter

storage := interpreter.NewInMemoryStorage(nil)

_, defaultInterpreterOptions :=
DefaultCheckerInterpreterOptions(
checkers,
codes,
)

// NOTE: storage option must be provided *before* the predeclared values option,
// as predeclared values may rely on storage

interpreterOptions := []interpreter.Option{
interpreter.WithStorage(storage),
interpreter.WithUUIDHandler(func() (uint64, error) {
config := &interpreter.Config{
Storage: storage,
UUIDHandler: func() (uint64, error) {
defer func() { uuid++ }()
return uuid, nil
}),
interpreter.WithDebugger(debugger),
interpreter.WithImportLocationHandler(
func(inter *interpreter.Interpreter, location common.Location) interpreter.Import {
panic("Importing programs is not supported yet")
},
),
},
Debugger: debugger,
ImportLocationHandler: func(inter *interpreter.Interpreter, location common.Location) interpreter.Import {
panic("Importing programs is not supported yet")
},
}

interpreterOptions = append(
interpreterOptions,
defaultInterpreterOptions...,
)

inter, err := interpreter.NewInterpreter(
interpreter.ProgramFromChecker(checker),
checker.Location,
interpreterOptions...,
config,
)
must(err)

Expand Down
4 changes: 3 additions & 1 deletion runtime/cmd/decode-state-values/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,9 @@ func load() {
inter, err := interpreter.NewInterpreter(
nil,
nil,
interpreter.WithStorage(interpreterStorage),
&interpreter.Config{
Storage: interpreterStorage,
},
)
if err != nil {
log.Fatalf("Failed to create interpreter: %s", err)
Expand Down
4 changes: 2 additions & 2 deletions runtime/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ type Config struct {
TracingEnabled bool
// ResourceOwnerChangeCallbackEnabled configures if the resource owner change callback is enabled.
ResourceOwnerChangeHandlerEnabled bool
// InvalidatedResourceValidationEnabled configures if invalidated resource validation is enabled.
InvalidatedResourceValidationEnabled bool
// CoverageReportingEnabled configures if coverage reporting is enabled.
CoverageReportingEnabled bool
// StackDepthLimit specifies the maximum depth for call stacks.
StackDepthLimit uint64
}
2 changes: 1 addition & 1 deletion runtime/convertValues.go
Original file line number Diff line number Diff line change
Expand Up @@ -1523,7 +1523,7 @@ func importPublicKey(
getLocationRange,
publicKeyValue,
signAlgoValue,
inter.PublicKeyValidationHandler,
inter.Config.PublicKeyValidationHandler,
), nil
}

Expand Down
8 changes: 5 additions & 3 deletions runtime/convertValues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4923,9 +4923,11 @@ func newTestInterpreter(tb testing.TB) *interpreter.Interpreter {
inter, err := interpreter.NewInterpreter(
nil,
TestLocation,
interpreter.WithStorage(storage),
interpreter.WithAtreeValueValidationEnabled(true),
interpreter.WithAtreeStorageValidationEnabled(true),
&interpreter.Config{
Storage: storage,
AtreeValueValidationEnabled: true,
AtreeStorageValidationEnabled: true,
},
)
require.NoError(tb, err)

Expand Down
Loading

0 comments on commit 38e80fb

Please sign in to comment.