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

Improve allocations in checker #2127

Merged
merged 5 commits into from
Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions runtime/interpreter/function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ func TestFunctionStaticType(t *testing.T) {
}

hostFunctionType := &sema.FunctionType{
Parameters: []*sema.Parameter{},
ReturnTypeAnnotation: sema.NewTypeAnnotation(sema.BoolType),
}

Expand All @@ -69,7 +68,6 @@ func TestFunctionStaticType(t *testing.T) {
}

hostFunctionType := &sema.FunctionType{
Parameters: []*sema.Parameter{},
ReturnTypeAnnotation: sema.NewTypeAnnotation(sema.BoolType),
}

Expand Down
35 changes: 20 additions & 15 deletions runtime/interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import (
//

var emptyFunctionType = &sema.FunctionType{
ReturnTypeAnnotation: &sema.TypeAnnotation{
ReturnTypeAnnotation: sema.TypeAnnotation{
Type: sema.VoidType,
},
}
Expand Down Expand Up @@ -942,7 +942,7 @@ func (interpreter *Interpreter) declareNonEnumCompositeValue(
constructorType := &sema.FunctionType{
IsConstructor: true,
Parameters: compositeType.ConstructorParameters,
ReturnTypeAnnotation: &sema.TypeAnnotation{
ReturnTypeAnnotation: sema.TypeAnnotation{
Type: compositeType,
},
RequiredArgumentCount: nil,
Expand Down Expand Up @@ -2725,24 +2725,29 @@ func init() {
}

returnType := invocation.Interpreter.MustConvertStaticToSemaType(typeValue.Type)
parameterTypes := make([]*sema.Parameter, 0, parameters.Count())
parameters.Iterate(invocation.Interpreter, func(param Value) bool {
semaType := invocation.Interpreter.MustConvertStaticToSemaType(param.(TypeValue).Type)
parameterTypes = append(
parameterTypes,
&sema.Parameter{
TypeAnnotation: sema.NewTypeAnnotation(semaType),
},
)

// Continue iteration
return true
})
var resultParameters []sema.Parameter
parameterCount := parameters.Count()
if parameterCount > 0 {
resultParameters = make([]sema.Parameter, 0, parameterCount)
parameters.Iterate(invocation.Interpreter, func(param Value) bool {
semaType := invocation.Interpreter.MustConvertStaticToSemaType(param.(TypeValue).Type)
resultParameters = append(
resultParameters,
sema.Parameter{
TypeAnnotation: sema.NewTypeAnnotation(semaType),
},
)

// Continue iteration
return true
})
}
functionStaticType := NewFunctionStaticType(
invocation.Interpreter,
&sema.FunctionType{
ReturnTypeAnnotation: sema.NewTypeAnnotation(returnType),
Parameters: parameterTypes,
Parameters: resultParameters,
},
)
return NewUnmeteredTypeValue(functionStaticType)
Expand Down
2 changes: 1 addition & 1 deletion runtime/interpreter/statictype.go
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ func (t FunctionStaticType) ParameterTypes(interpreter *Interpreter) []StaticTyp

func (t FunctionStaticType) ReturnType(interpreter *Interpreter) StaticType {
var returnType StaticType
if t.Type.ReturnTypeAnnotation != nil {
if t.Type.ReturnTypeAnnotation.Type != nil {
returnType = ConvertSemaToStaticType(interpreter, t.Type.ReturnTypeAnnotation.Type)
}

Expand Down
2 changes: 1 addition & 1 deletion runtime/interpreter/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3709,7 +3709,7 @@ func TestValue_ConformsToStaticType(t *testing.T) {
t.Parallel()

functionType := &sema.FunctionType{
Parameters: []*sema.Parameter{
Parameters: []sema.Parameter{
{
TypeAnnotation: sema.NewTypeAnnotation(sema.IntType),
},
Expand Down
2 changes: 1 addition & 1 deletion runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ func validateArgumentParams(
decoder ArgumentDecoder,
locationRange interpreter.LocationRange,
arguments [][]byte,
parameters []*sema.Parameter,
parameters []sema.Parameter,
) (
[]interpreter.Value,
error,
Expand Down
4 changes: 2 additions & 2 deletions runtime/sema/account_contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Returns nil if no contract/contract interface with the given name exists in the
`

var AccountContractsTypeGetFunctionType = &FunctionType{
Parameters: []*Parameter{
Parameters: []Parameter{
{
Identifier: "name",
TypeAnnotation: NewTypeAnnotation(
Expand Down Expand Up @@ -63,7 +63,7 @@ var AccountContractsTypeBorrowFunctionType = func() *FunctionType {
TypeParameters: []*TypeParameter{
typeParameter,
},
Parameters: []*Parameter{
Parameters: []Parameter{
{
Identifier: "name",
TypeAnnotation: NewTypeAnnotation(StringType),
Expand Down
6 changes: 3 additions & 3 deletions runtime/sema/authaccount_contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ Returns the deployed contract.
`

var AuthAccountContractsTypeAddFunctionType = &FunctionType{
Parameters: []*Parameter{
Parameters: []Parameter{
{
Identifier: "name",
TypeAnnotation: NewTypeAnnotation(
Expand Down Expand Up @@ -144,7 +144,7 @@ Returns the deployed contract for the updated contract.
`

var AuthAccountContractsTypeUpdateExperimentalFunctionType = &FunctionType{
Parameters: []*Parameter{
Parameters: []Parameter{
{
Identifier: "name",
TypeAnnotation: NewTypeAnnotation(
Expand Down Expand Up @@ -172,7 +172,7 @@ Returns nil if no contract/contract interface with the given name exists in the
`

var AuthAccountContractsTypeRemoveFunctionType = &FunctionType{
Parameters: []*Parameter{
Parameters: []Parameter{
{
Identifier: "name",
TypeAnnotation: NewTypeAnnotation(StringType),
Expand Down
38 changes: 19 additions & 19 deletions runtime/sema/authaccount_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ var AuthAccountForEachPrivateFunctionType = AccountForEachFunctionType(PrivatePa
var AuthAccountForEachStoredFunctionType = AccountForEachFunctionType(StoragePathType)

var AuthAccountTypeAddPublicKeyFunctionType = &FunctionType{
Parameters: []*Parameter{
Parameters: []Parameter{
{
Label: ArgumentLabelNotRequired,
Identifier: "key",
Expand All @@ -312,7 +312,7 @@ Adds the given byte representation of a public key to the account's keys
`

var AuthAccountTypeRemovePublicKeyFunctionType = &FunctionType{
Parameters: []*Parameter{
Parameters: []Parameter{
{
Label: ArgumentLabelNotRequired,
Identifier: "index",
Expand Down Expand Up @@ -341,7 +341,7 @@ var AuthAccountTypeSaveFunctionType = func() *FunctionType {
TypeParameters: []*TypeParameter{
typeParameter,
},
Parameters: []*Parameter{
Parameters: []Parameter{
{
Label: ArgumentLabelNotRequired,
Identifier: "value",
Expand Down Expand Up @@ -381,7 +381,7 @@ var AuthAccountTypeLoadFunctionType = func() *FunctionType {
TypeParameters: []*TypeParameter{
typeParameter,
},
Parameters: []*Parameter{
Parameters: []Parameter{
{
Label: "from",
Identifier: "path",
Expand All @@ -407,7 +407,7 @@ The path must be a storage path, i.e., only the domain ` + "`storage`" + ` is al
`

var AuthAccountTypeTypeFunctionType = &FunctionType{
Parameters: []*Parameter{
Parameters: []Parameter{
{
Label: "at",
Identifier: "path",
Expand Down Expand Up @@ -446,7 +446,7 @@ var AuthAccountTypeCopyFunctionType = func() *FunctionType {
TypeParameters: []*TypeParameter{
typeParameter,
},
Parameters: []*Parameter{
Parameters: []Parameter{
{
Label: "from",
Identifier: "path",
Expand Down Expand Up @@ -489,7 +489,7 @@ var AuthAccountTypeBorrowFunctionType = func() *FunctionType {
TypeParameters: []*TypeParameter{
typeParameter,
},
Parameters: []*Parameter{
Parameters: []Parameter{
{
Label: "from",
Identifier: "path",
Expand Down Expand Up @@ -534,7 +534,7 @@ var AuthAccountTypeLinkFunctionType = func() *FunctionType {
TypeParameters: []*TypeParameter{
typeParameter,
},
Parameters: []*Parameter{
Parameters: []Parameter{
{
Label: ArgumentLabelNotRequired,
Identifier: "newCapabilityPath",
Expand Down Expand Up @@ -571,7 +571,7 @@ The link is latent. The target value might be stored after the link is created,
`

var AuthAccountTypeUnlinkFunctionType = &FunctionType{
Parameters: []*Parameter{
Parameters: []Parameter{
{
Label: ArgumentLabelNotRequired,
Identifier: "capabilityPath",
Expand Down Expand Up @@ -599,7 +599,7 @@ var AuthAccountTypeGetCapabilityFunctionType = func() *FunctionType {
TypeParameters: []*TypeParameter{
typeParameter,
},
Parameters: []*Parameter{
Parameters: []Parameter{
{
Label: ArgumentLabelNotRequired,
Identifier: "capabilityPath",
Expand All @@ -621,7 +621,7 @@ Returns the capability at the given private or public path, or nil if it does no
`

var AccountTypeGetLinkTargetFunctionType = &FunctionType{
Parameters: []*Parameter{
Parameters: []Parameter{
{
Label: ArgumentLabelNotRequired,
Identifier: "capabilityPath",
Expand Down Expand Up @@ -683,7 +683,7 @@ var AuthAccountKeysType = func() *CompositeType {
}()

var AuthAccountKeysTypeAddFunctionType = &FunctionType{
Parameters: []*Parameter{
Parameters: []Parameter{
{
Identifier: AccountKeyPublicKeyField,
TypeAnnotation: NewTypeAnnotation(PublicKeyType),
Expand All @@ -702,7 +702,7 @@ var AuthAccountKeysTypeAddFunctionType = &FunctionType{
}

var AccountKeysTypeGetFunctionType = &FunctionType{
Parameters: []*Parameter{
Parameters: []Parameter{
{
Identifier: AccountKeyKeyIndexField,
TypeAnnotation: NewTypeAnnotation(IntType),
Expand All @@ -716,7 +716,7 @@ var AccountKeysTypeGetFunctionType = &FunctionType{
var AccountKeysTypeForEachFunctionType = func() *FunctionType {
// ((AccountKey): Bool)
iterFunctionType := &FunctionType{
Parameters: []*Parameter{
Parameters: []Parameter{
{
TypeAnnotation: NewTypeAnnotation(AccountKeyType),
},
Expand All @@ -725,7 +725,7 @@ var AccountKeysTypeForEachFunctionType = func() *FunctionType {
}

return &FunctionType{
Parameters: []*Parameter{
Parameters: []Parameter{
{
Label: ArgumentLabelNotRequired,
Identifier: "function",
Expand All @@ -739,7 +739,7 @@ var AccountKeysTypeForEachFunctionType = func() *FunctionType {
var AccountKeysTypeCountFieldType = UInt64Type

var AuthAccountKeysTypeRevokeFunctionType = &FunctionType{
Parameters: []*Parameter{
Parameters: []Parameter{
{
Identifier: AccountKeyKeyIndexField,
TypeAnnotation: NewTypeAnnotation(IntType),
Expand Down Expand Up @@ -819,7 +819,7 @@ Publishes the argument value under the given name, to be later claimed by the sp
`

var AuthAccountTypeInboxPublishFunctionType = &FunctionType{
Parameters: []*Parameter{
Parameters: []Parameter{
{
Label: ArgumentLabelNotRequired,
Identifier: "value",
Expand Down Expand Up @@ -854,7 +854,7 @@ var AuthAccountTypeInboxUnpublishFunctionType = func() *FunctionType {
TypeParameters: []*TypeParameter{
typeParameter,
},
Parameters: []*Parameter{
Parameters: []Parameter{
{
Label: ArgumentLabelNotRequired,
Identifier: "name",
Expand Down Expand Up @@ -888,7 +888,7 @@ var AuthAccountTypeInboxClaimFunctionType = func() *FunctionType {
TypeParameters: []*TypeParameter{
typeParameter,
},
Parameters: []*Parameter{
Parameters: []Parameter{
{
Label: ArgumentLabelNotRequired,
Identifier: "name",
Expand Down
22 changes: 11 additions & 11 deletions runtime/sema/check_composite_declaration.go
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@ func (checker *Checker) declareEnumConstructor(
func EnumConstructorType(compositeType *CompositeType) *FunctionType {
return &FunctionType{
IsConstructor: true,
Parameters: []*Parameter{
Parameters: []Parameter{
{
Identifier: EnumRawValueFieldName,
TypeAnnotation: NewTypeAnnotation(compositeType.EnumRawType),
Expand Down Expand Up @@ -902,9 +902,9 @@ func (checker *Checker) checkMemberStorability(members *StringMemberOrderedMap)
})
}

func (checker *Checker) initializerParameters(initializers []*ast.SpecialFunctionDeclaration) []*Parameter {
func (checker *Checker) initializerParameters(initializers []*ast.SpecialFunctionDeclaration) []Parameter {
// TODO: support multiple overloaded initializers
var parameters []*Parameter
var parameters []Parameter

initializerCount := len(initializers)
if initializerCount > 0 {
Expand Down Expand Up @@ -1260,8 +1260,8 @@ func (checker *Checker) memberSatisfied(compositeMember, interfaceMember *Member

// Functions are covariant in their return type

if compositeMemberFunctionType.ReturnTypeAnnotation != nil &&
interfaceMemberFunctionType.ReturnTypeAnnotation != nil {
if compositeMemberFunctionType.ReturnTypeAnnotation.Type != nil &&
interfaceMemberFunctionType.ReturnTypeAnnotation.Type != nil {

if !IsSubType(
compositeMemberFunctionType.ReturnTypeAnnotation.Type,
Expand All @@ -1271,10 +1271,10 @@ func (checker *Checker) memberSatisfied(compositeMember, interfaceMember *Member
}
}

if (compositeMemberFunctionType.ReturnTypeAnnotation != nil &&
interfaceMemberFunctionType.ReturnTypeAnnotation == nil) ||
(compositeMemberFunctionType.ReturnTypeAnnotation == nil &&
interfaceMemberFunctionType.ReturnTypeAnnotation != nil) {
if (compositeMemberFunctionType.ReturnTypeAnnotation.Type != nil &&
interfaceMemberFunctionType.ReturnTypeAnnotation.Type == nil) ||
(compositeMemberFunctionType.ReturnTypeAnnotation.Type == nil &&
interfaceMemberFunctionType.ReturnTypeAnnotation.Type != nil) {

return false
}
Expand Down Expand Up @@ -1781,7 +1781,7 @@ func (checker *Checker) checkInitializers(
fields []*ast.FieldDeclaration,
containerType Type,
containerDocString string,
initializerParameters []*Parameter,
initializerParameters []Parameter,
containerKind ContainerKind,
initializationInfo *InitializationInfo,
) {
Expand Down Expand Up @@ -1852,7 +1852,7 @@ func (checker *Checker) checkSpecialFunction(
specialFunction *ast.SpecialFunctionDeclaration,
containerType Type,
containerDocString string,
parameters []*Parameter,
parameters []Parameter,
containerKind ContainerKind,
initializationInfo *InitializationInfo,
) {
Expand Down
2 changes: 1 addition & 1 deletion runtime/sema/check_event_declaration.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
// as determined by `isValidEventParameterType`.
func (checker *Checker) checkEventParameters(
parameterList *ast.ParameterList,
parameters []*Parameter,
parameters []Parameter,
) {

parameterTypeValidationResults := map[*Member]bool{}
Expand Down
Loading