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

Cache normalized string #2777

Closed
Closed
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
5 changes: 3 additions & 2 deletions runtime/convertValues.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,12 @@ func exportValueWithInterpreter(
case interpreter.BoolValue:
return cadence.NewMeteredBool(inter, bool(v)), nil
case *interpreter.StringValue:
str := v.Str(inter)
return cadence.NewMeteredString(
inter,
common.NewCadenceStringMemoryUsage(len(v.Str)),
common.NewCadenceStringMemoryUsage(len(str)),
func() string {
return v.Str
return str
},
)
case interpreter.CharacterValue:
Expand Down
3 changes: 2 additions & 1 deletion runtime/interpreter/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,8 @@ func (v *StringValue) Encode(e *atree.Encoder) error {
if err != nil {
return err
}
return e.CBOR.EncodeString(v.Str)
// TODO: write normalized? no memory gauge available
return e.CBOR.EncodeString(v._str)
}

// Encode encodes the value as a CBOR string
Expand Down
61 changes: 35 additions & 26 deletions runtime/interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,7 @@ func (interpreter *Interpreter) visitCondition(condition ast.Condition, kind ast
var message string
if messageExpression != nil {
messageValue := interpreter.evalExpression(messageExpression)
message = messageValue.(*StringValue).Str
message = messageValue.(*StringValue).Str(interpreter)
}

panic(ConditionError{
Expand Down Expand Up @@ -2571,7 +2571,7 @@ func newFromStringFunction(ty sema.Type, parser stringValueParser) fromStringFun
panic(errors.NewUnreachableError())
}
inter := invocation.Interpreter
return parser(inter, argument.Str)
return parser(inter, argument.Str(inter))
},
)
return fromStringFunctionValue{
Expand Down Expand Up @@ -3407,6 +3407,8 @@ func dictionaryTypeFunction(invocation Invocation) Value {
}

func referenceTypeFunction(invocation Invocation) Value {
inter := invocation.Interpreter

entitlementValues, ok := invocation.Arguments[0].(*ArrayValue)
if !ok {
panic(errors.NewUnreachableError())
Expand All @@ -3423,22 +3425,23 @@ func referenceTypeFunction(invocation Invocation) Value {

if entitlementsCount > 0 {
authorization = NewEntitlementSetAuthorization(
invocation.Interpreter,
inter,
func() []common.TypeID {
entitlements := make([]common.TypeID, 0, entitlementsCount)
entitlementValues.Iterate(invocation.Interpreter, func(element Value) (resume bool) {
entitlementValues.Iterate(inter, func(element Value) (resume bool) {
entitlementString, isString := element.(*StringValue)
if !isString {
errInIteration = true
return false
}

_, err := lookupEntitlement(invocation.Interpreter, entitlementString.Str)
entitlementStr := entitlementString.Str(inter)
_, err := lookupEntitlement(inter, entitlementStr)
if err != nil {
errInIteration = true
return false
}
entitlements = append(entitlements, common.TypeID(entitlementString.Str))
entitlements = append(entitlements, common.TypeID(entitlementStr))

return true
})
Expand All @@ -3454,11 +3457,11 @@ func referenceTypeFunction(invocation Invocation) Value {
}

return NewSomeValueNonCopying(
invocation.Interpreter,
inter,
NewTypeValue(
invocation.Interpreter,
inter,
NewReferenceStaticType(
invocation.Interpreter,
inter,
authorization,
typeValue.Type,
),
Expand All @@ -3467,43 +3470,47 @@ func referenceTypeFunction(invocation Invocation) Value {
}

func compositeTypeFunction(invocation Invocation) Value {
inter := invocation.Interpreter

typeIDValue, ok := invocation.Arguments[0].(*StringValue)
if !ok {
panic(errors.NewUnreachableError())
}
typeID := typeIDValue.Str
typeID := typeIDValue.Str(inter)

composite, err := lookupComposite(invocation.Interpreter, typeID)
composite, err := lookupComposite(inter, typeID)
if err != nil {
return Nil
}

return NewSomeValueNonCopying(
invocation.Interpreter,
inter,
NewTypeValue(
invocation.Interpreter,
ConvertSemaToStaticType(invocation.Interpreter, composite),
inter,
ConvertSemaToStaticType(inter, composite),
),
)
}

func interfaceTypeFunction(invocation Invocation) Value {
inter := invocation.Interpreter

typeIDValue, ok := invocation.Arguments[0].(*StringValue)
if !ok {
panic(errors.NewUnreachableError())
}
typeID := typeIDValue.Str
typeID := typeIDValue.Str(inter)

interfaceType, err := lookupInterface(invocation.Interpreter, typeID)
interfaceType, err := lookupInterface(inter, typeID)
if err != nil {
return Nil
}

return NewSomeValueNonCopying(
invocation.Interpreter,
inter,
NewTypeValue(
invocation.Interpreter,
ConvertSemaToStaticType(invocation.Interpreter, interfaceType),
inter,
ConvertSemaToStaticType(inter, interfaceType),
),
)
}
Expand Down Expand Up @@ -3552,6 +3559,8 @@ func functionTypeFunction(invocation Invocation) Value {
}

func intersectionTypeFunction(invocation Invocation) Value {
inter := invocation.Interpreter

intersectionIDs, ok := invocation.Arguments[0].(*ArrayValue)
if !ok {
panic(errors.NewUnreachableError())
Expand All @@ -3566,21 +3575,21 @@ func intersectionTypeFunction(invocation Invocation) Value {
semaIntersections = make([]*sema.InterfaceType, 0, count)

var invalidIntersectionID bool
intersectionIDs.Iterate(invocation.Interpreter, func(typeID Value) bool {
intersectionIDs.Iterate(inter, func(typeID Value) bool {
typeIDValue, ok := typeID.(*StringValue)
if !ok {
panic(errors.NewUnreachableError())
}

intersectedInterface, err := lookupInterface(invocation.Interpreter, typeIDValue.Str)
intersectedInterface, err := lookupInterface(inter, typeIDValue.Str(inter))
if err != nil {
invalidIntersectionID = true
return true
}

staticIntersections = append(
staticIntersections,
ConvertSemaToStaticType(invocation.Interpreter, intersectedInterface).(*InterfaceStaticType),
ConvertSemaToStaticType(inter, intersectedInterface).(*InterfaceStaticType),
)
semaIntersections = append(semaIntersections, intersectedInterface)

Expand All @@ -3597,7 +3606,7 @@ func intersectionTypeFunction(invocation Invocation) Value {

var invalidIntersectionType bool
sema.CheckIntersectionType(
invocation.Interpreter,
inter,
semaIntersections,
func(_ func(*ast.IntersectionType) error) {
invalidIntersectionType = true
Expand All @@ -3611,11 +3620,11 @@ func intersectionTypeFunction(invocation Invocation) Value {
}

return NewSomeValueNonCopying(
invocation.Interpreter,
inter,
NewTypeValue(
invocation.Interpreter,
inter,
NewIntersectionStaticType(
invocation.Interpreter,
inter,
staticIntersections,
),
),
Expand Down
Loading
Loading