Skip to content

Commit

Permalink
Merge branch 'feature/memory-metering' of github.com:onflow/cadence i…
Browse files Browse the repository at this point in the history
…nto memory-kind-last
  • Loading branch information
dsainati1 committed Apr 6, 2022
2 parents 84d30e0 + b8efa6a commit 2145b20
Show file tree
Hide file tree
Showing 33 changed files with 625 additions and 254 deletions.
14 changes: 14 additions & 0 deletions runtime/ast/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,20 @@ type FunctionBlock struct {
PostConditions *Conditions `json:",omitempty"`
}

func NewFunctionBlock(
memoryGauge common.MemoryGauge,
block *Block,
preConditions *Conditions,
postConditions *Conditions,
) *FunctionBlock {
common.UseMemory(memoryGauge, common.FunctionBlockMemoryUsage)
return &FunctionBlock{
Block: block,
PreConditions: preConditions,
PostConditions: postConditions,
}
}

func (b *FunctionBlock) IsEmpty() bool {
return b == nil ||
(b.Block.IsEmpty() &&
Expand Down
2 changes: 1 addition & 1 deletion runtime/ast/composite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func TestCompositeDeclaration_MarshalJSON(t *testing.T) {
},
},
},
Members: NewMembers([]Declaration{}),
Members: NewUnmeteredMembers([]Declaration{}),
DocString: "test",
Range: Range{
StartPos: Position{Offset: 7, Line: 8, Column: 9},
Expand Down
2 changes: 1 addition & 1 deletion runtime/ast/interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestInterfaceDeclaration_MarshalJSON(t *testing.T) {
Identifier: "AB",
Pos: Position{Offset: 1, Line: 2, Column: 3},
},
Members: NewMembers([]Declaration{}),
Members: NewUnmeteredMembers([]Declaration{}),
DocString: "test",
Range: Range{
StartPos: Position{Offset: 7, Line: 8, Column: 9},
Expand Down
2 changes: 1 addition & 1 deletion runtime/ast/memberindices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func TestMemberIndices(t *testing.T) {
Identifier: Identifier{Identifier: "C"},
}

members := NewMembers(
members := NewUnmeteredMembers(
[]Declaration{
specialFunctionB,
enumCaseA,
Expand Down
7 changes: 6 additions & 1 deletion runtime/ast/members.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ type Members struct {
indices memberIndices
}

func NewMembers(declarations []Declaration) *Members {
func NewMembers(memoryGauge common.MemoryGauge, declarations []Declaration) *Members {
common.UseMemory(memoryGauge, common.NewMembersMemoryUsage(len(declarations)))
return NewUnmeteredMembers(declarations)
}

func NewUnmeteredMembers(declarations []Declaration) *Members {
return &Members{
declarations: declarations,
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/ast/members_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestMembers_MarshalJSON(t *testing.T) {

t.Parallel()

members := NewMembers([]Declaration{})
members := NewUnmeteredMembers([]Declaration{})

actual, err := json.Marshal(members)
require.NoError(t, err)
Expand Down
18 changes: 18 additions & 0 deletions runtime/ast/parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,31 @@

package ast

import "github.com/onflow/cadence/runtime/common"

type Parameter struct {
Label string
Identifier Identifier
TypeAnnotation *TypeAnnotation
Range
}

func NewParameter(
gauge common.MemoryGauge,
label string,
identifier Identifier,
typeAnnotation *TypeAnnotation,
astRange Range,
) *Parameter {
common.UseMemory(gauge, common.ParameterMemoryUsage)
return &Parameter{
Label: label,
Identifier: identifier,
TypeAnnotation: typeAnnotation,
Range: astRange,
}
}

// EffectiveArgumentLabel returns the effective argument label that
// an argument in a call must use:
// If no argument label is declared for parameter,
Expand Down
18 changes: 17 additions & 1 deletion runtime/ast/parameterlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@

package ast

import "sync"
import (
"sync"

"github.com/onflow/cadence/runtime/common"
)

type ParameterList struct {
once sync.Once
Expand All @@ -27,6 +31,18 @@ type ParameterList struct {
Range
}

func NewParameterList(
gauge common.MemoryGauge,
parameters []*Parameter,
astRange Range,
) *ParameterList {
common.UseMemory(gauge, common.ParameterListMemoryUsage)
return &ParameterList{
Parameters: parameters,
Range: astRange,
}
}

// EffectiveArgumentLabels returns the effective argument labels that
// the arguments of a call must use:
// If no argument label is declared for parameter,
Expand Down
2 changes: 2 additions & 0 deletions runtime/ast/position.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ type Range struct {
EndPos Position
}

var EmptyRange = Range{}

func (e Range) StartPosition() Position {
return e.StartPos
}
Expand Down
3 changes: 2 additions & 1 deletion runtime/ast/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ type Program struct {
indices programIndices
}

func NewProgram(declarations []Declaration) *Program {
func NewProgram(memoryGauge common.MemoryGauge, declarations []Declaration) *Program {
common.UseMemory(memoryGauge, common.ProgramMemoryUsage)
return &Program{
declarations: declarations,
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/ast/program_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestProgram_MarshalJSON(t *testing.T) {

t.Parallel()

program := NewProgram([]Declaration{})
program := NewProgram(nil, []Declaration{})

actual, err := json.Marshal(program)
require.NoError(t, err)
Expand Down
1 change: 1 addition & 0 deletions runtime/ast/programindices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func TestProgramIndices(t *testing.T) {
}

program := NewProgram(
nil,
[]Declaration{
importB,
pragmaA,
Expand Down
10 changes: 10 additions & 0 deletions runtime/ast/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"encoding/json"

"github.com/turbolent/prettier"

"github.com/onflow/cadence/runtime/common"
)

// Transfer represents the operation in variable declarations
Expand All @@ -32,6 +34,14 @@ type Transfer struct {
Pos Position `json:"-"`
}

func NewTransfer(memoryGauge common.MemoryGauge, operation TransferOperation, position Position) *Transfer {
common.UseMemory(memoryGauge, common.TransferMemoryUsage)
return &Transfer{
Operation: operation,
Pos: position,
}
}

func (f Transfer) StartPosition() Position {
return f.Pos
}
Expand Down

0 comments on commit 2145b20

Please sign in to comment.