Skip to content

Commit

Permalink
Merge pull request #3710 from onflow/bastian/optimize-account-values
Browse files Browse the repository at this point in the history
Lazily compute fields of account type values
  • Loading branch information
turbolent authored Dec 9, 2024
2 parents 0963441 + 5cc3100 commit 5364220
Show file tree
Hide file tree
Showing 8 changed files with 271 additions and 197 deletions.
45 changes: 17 additions & 28 deletions interpreter/value_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,52 +55,41 @@ func NewAccountValue(
sema.AccountTypeAddressFieldName: address,
}

var storage Value
var contracts Value
var keys Value
var inbox Value
var capabilities Value

computeField := func(name string, inter *Interpreter, locationRange LocationRange) Value {
computeLazyStoredField := func(name string) Value {
switch name {
case sema.AccountTypeStorageFieldName:
if storage == nil {
storage = storageConstructor()
}
return storage
return storageConstructor()

case sema.AccountTypeContractsFieldName:
if contracts == nil {
contracts = contractsConstructor()
}
return contracts
return contractsConstructor()

case sema.AccountTypeKeysFieldName:
if keys == nil {
keys = keysConstructor()
}
return keys
return keysConstructor()

case sema.AccountTypeInboxFieldName:
if inbox == nil {
inbox = inboxConstructor()
}
return inbox
return inboxConstructor()

case sema.AccountTypeCapabilitiesFieldName:
if capabilities == nil {
capabilities = capabilitiesConstructor()
}
return capabilities
return capabilitiesConstructor()
}

return nil
}

computeField := func(name string, _ *Interpreter, _ LocationRange) Value {
switch name {
case sema.AccountTypeBalanceFieldName:
return accountBalanceGet()

case sema.AccountTypeAvailableBalanceFieldName:
return accountAvailableBalanceGet()
}

return nil
field := computeLazyStoredField(name)
if field != nil {
fields[name] = field
}
return field
}

var str string
Expand Down
43 changes: 32 additions & 11 deletions interpreter/value_account_accountcapabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,35 @@ func NewAccountAccountCapabilitiesValue(
issueWithTypeFunction BoundFunctionGenerator,
) *SimpleCompositeValue {

var accountCapabilities *SimpleCompositeValue

fields := map[string]Value{}

computeLazyStoredField := func(name string) Value {
switch name {
case sema.Account_AccountCapabilitiesTypeGetControllerFunctionName:
return getControllerFunction(accountCapabilities)
case sema.Account_AccountCapabilitiesTypeGetControllersFunctionName:
return getControllersFunction(accountCapabilities)
case sema.Account_AccountCapabilitiesTypeForEachControllerFunctionName:
return forEachControllerFunction(accountCapabilities)
case sema.Account_AccountCapabilitiesTypeIssueFunctionName:
return issueFunction(accountCapabilities)
case sema.Account_AccountCapabilitiesTypeIssueWithTypeFunctionName:
return issueWithTypeFunction(accountCapabilities)
}

return nil
}

computeField := func(name string, _ *Interpreter, _ LocationRange) Value {
field := computeLazyStoredField(name)
if field != nil {
fields[name] = field
}
return field
}

var str string
stringer := func(interpreter *Interpreter, seenReferences SeenReferences, locationRange LocationRange) string {
if str == "" {
Expand All @@ -51,24 +80,16 @@ func NewAccountAccountCapabilitiesValue(
return str
}

accountCapabilities := NewSimpleCompositeValue(
accountCapabilities = NewSimpleCompositeValue(
gauge,
account_AccountCapabilitiesTypeID,
account_AccountCapabilitiesStaticType,
account_AccountCapabilitiesFieldNames,
nil,
nil,
fields,
computeField,
nil,
stringer,
)

accountCapabilities.Fields = map[string]Value{
sema.Account_AccountCapabilitiesTypeGetControllerFunctionName: getControllerFunction(accountCapabilities),
sema.Account_AccountCapabilitiesTypeGetControllersFunctionName: getControllersFunction(accountCapabilities),
sema.Account_AccountCapabilitiesTypeForEachControllerFunctionName: forEachControllerFunction(accountCapabilities),
sema.Account_AccountCapabilitiesTypeIssueFunctionName: issueFunction(accountCapabilities),
sema.Account_AccountCapabilitiesTypeIssueWithTypeFunctionName: issueWithTypeFunction(accountCapabilities),
}

return accountCapabilities
}
54 changes: 31 additions & 23 deletions interpreter/value_account_capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ import (

var account_CapabilitiesTypeID = sema.AccountCapabilitiesType.ID()
var account_CapabilitiesStaticType StaticType = PrimitiveStaticTypeAccount_Capabilities
var account_CapabilitiesFieldNames = []string{
sema.Account_CapabilitiesTypeStorageFieldName,
sema.Account_CapabilitiesTypeAccountFieldName,
}

func NewAccountCapabilitiesValue(
gauge common.MemoryGauge,
Expand All @@ -42,27 +46,39 @@ func NewAccountCapabilitiesValue(
accountCapabilitiesConstructor func() Value,
) Value {

var storageCapabilities Value
var accountCapabilities Value
var capabilities *SimpleCompositeValue

fields := map[string]Value{}

computeField := func(name string, inter *Interpreter, locationRange LocationRange) Value {
computeLazyStoredField := func(name string) Value {
switch name {
case sema.Account_CapabilitiesTypeStorageFieldName:
if storageCapabilities == nil {
storageCapabilities = storageCapabilitiesConstructor()
}
return storageCapabilities

return storageCapabilitiesConstructor()
case sema.Account_CapabilitiesTypeAccountFieldName:
if accountCapabilities == nil {
accountCapabilities = accountCapabilitiesConstructor()
}
return accountCapabilities
return accountCapabilitiesConstructor()
case sema.Account_CapabilitiesTypeGetFunctionName:
return getFunction(capabilities)
case sema.Account_CapabilitiesTypeBorrowFunctionName:
return borrowFunction(capabilities)
case sema.Account_CapabilitiesTypeExistsFunctionName:
return existsFunction(capabilities)
case sema.Account_CapabilitiesTypePublishFunctionName:
return publishFunction(capabilities)
case sema.Account_CapabilitiesTypeUnpublishFunctionName:
return unpublishFunction(capabilities)
}

return nil
}

computeField := func(name string, _ *Interpreter, _ LocationRange) Value {
field := computeLazyStoredField(name)
if field != nil {
fields[name] = field
}
return field
}

var str string
stringer := func(interpreter *Interpreter, seenReferences SeenReferences, locationRange LocationRange) string {
if str == "" {
Expand All @@ -73,24 +89,16 @@ func NewAccountCapabilitiesValue(
return str
}

capabilities := NewSimpleCompositeValue(
capabilities = NewSimpleCompositeValue(
gauge,
account_CapabilitiesTypeID,
account_CapabilitiesStaticType,
nil,
nil,
account_CapabilitiesFieldNames,
fields,
computeField,
nil,
stringer,
)

capabilities.Fields = map[string]Value{
sema.Account_CapabilitiesTypeGetFunctionName: getFunction(capabilities),
sema.Account_CapabilitiesTypeBorrowFunctionName: borrowFunction(capabilities),
sema.Account_CapabilitiesTypeExistsFunctionName: existsFunction(capabilities),
sema.Account_CapabilitiesTypePublishFunctionName: publishFunction(capabilities),
sema.Account_CapabilitiesTypeUnpublishFunctionName: unpublishFunction(capabilities),
}

return capabilities
}
51 changes: 33 additions & 18 deletions interpreter/value_account_contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,42 @@ func NewAccountContractsValue(
namesGetter ContractNamesGetter,
) Value {

computeField := func(
name string,
interpreter *Interpreter,
locationRange LocationRange,
) Value {
var accountContracts *SimpleCompositeValue

fields := map[string]Value{}

computeLazyStoredField := func(name string) Value {
switch name {
case sema.Account_ContractsTypeNamesFieldName:
return namesGetter(interpreter, locationRange)
case sema.Account_ContractsTypeAddFunctionName:
return addFunction(accountContracts)
case sema.Account_ContractsTypeGetFunctionName:
return getFunction(accountContracts)
case sema.Account_ContractsTypeBorrowFunctionName:
return borrowFunction(accountContracts)
case sema.Account_ContractsTypeRemoveFunctionName:
return removeFunction(accountContracts)
case sema.Account_ContractsTypeUpdateFunctionName:
return updateFunction(accountContracts)
case sema.Account_ContractsTypeTryUpdateFunctionName:
return tryUpdateFunction(accountContracts)
}

return nil
}

computeField := func(name string, inter *Interpreter, locationRange LocationRange) Value {
switch name {
case sema.Account_ContractsTypeNamesFieldName:
return namesGetter(inter, locationRange)
}

field := computeLazyStoredField(name)
if field != nil {
fields[name] = field
}
return field
}

var str string
stringer := func(interpreter *Interpreter, seenReferences SeenReferences, locationRange LocationRange) string {
if str == "" {
Expand All @@ -67,25 +91,16 @@ func NewAccountContractsValue(
return str
}

accountContracts := NewSimpleCompositeValue(
accountContracts = NewSimpleCompositeValue(
gauge,
account_ContractsTypeID,
account_ContractsStaticType,
account_ContractsFieldNames,
nil,
fields,
computeField,
nil,
stringer,
)

accountContracts.Fields = map[string]Value{
sema.Account_ContractsTypeAddFunctionName: addFunction(accountContracts),
sema.Account_ContractsTypeGetFunctionName: getFunction(accountContracts),
sema.Account_ContractsTypeBorrowFunctionName: borrowFunction(accountContracts),
sema.Account_ContractsTypeRemoveFunctionName: removeFunction(accountContracts),
sema.Account_ContractsTypeUpdateFunctionName: updateFunction(accountContracts),
sema.Account_ContractsTypeTryUpdateFunctionName: tryUpdateFunction(accountContracts),
}

return accountContracts
}
40 changes: 30 additions & 10 deletions interpreter/value_account_inbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (

var account_InboxTypeID = sema.Account_InboxType.ID()
var account_InboxStaticType StaticType = PrimitiveStaticTypeAccount_Inbox
var account_InboxFieldNames []string = nil

// NewAccountInboxValue constructs an Account.Inbox value.
func NewAccountInboxValue(
Expand All @@ -39,6 +40,31 @@ func NewAccountInboxValue(
claimFunction BoundFunctionGenerator,
) Value {

var accountInbox *SimpleCompositeValue

fields := map[string]Value{}

computeLazyStoredField := func(name string) Value {
switch name {
case sema.Account_InboxTypePublishFunctionName:
return publishFunction(accountInbox)
case sema.Account_InboxTypeUnpublishFunctionName:
return unpublishFunction(accountInbox)
case sema.Account_InboxTypeClaimFunctionName:
return claimFunction(accountInbox)
}

return nil
}

computeField := func(name string, _ *Interpreter, _ LocationRange) Value {
field := computeLazyStoredField(name)
if field != nil {
fields[name] = field
}
return field
}

var str string
stringer := func(interpreter *Interpreter, seenReferences SeenReferences, locationRange LocationRange) string {
if str == "" {
Expand All @@ -49,22 +75,16 @@ func NewAccountInboxValue(
return str
}

accountInbox := NewSimpleCompositeValue(
accountInbox = NewSimpleCompositeValue(
gauge,
account_InboxTypeID,
account_InboxStaticType,
nil,
nil,
nil,
account_InboxFieldNames,
fields,
computeField,
nil,
stringer,
)

accountInbox.Fields = map[string]Value{
sema.Account_InboxTypePublishFunctionName: publishFunction(accountInbox),
sema.Account_InboxTypeUnpublishFunctionName: unpublishFunction(accountInbox),
sema.Account_InboxTypeClaimFunctionName: claimFunction(accountInbox),
}

return accountInbox
}
Loading

0 comments on commit 5364220

Please sign in to comment.