Skip to content

Commit 1175e5f

Browse files
committed
revert string value
1 parent 9d13cda commit 1175e5f

File tree

19 files changed

+40
-100
lines changed

19 files changed

+40
-100
lines changed

gno.land/pkg/sdk/vm/convert.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func convertArgToGno(arg string, argT gno.Type) (tv gno.TypedValue) {
4040
arg))
4141
}
4242
case gno.StringType:
43-
tv.SetString(gno.NewStringValue(arg))
43+
tv.SetString(gno.StringValue(arg))
4444
return
4545
case gno.IntType:
4646
assertNoPlusPrefix(arg)

gno.land/pkg/sdk/vm/keeper.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func (vm *VMKeeper) Initialize(
100100
baseStore := ms.GetStore(vm.baseKey)
101101
iavlStore := ms.GetStore(vm.iavlKey)
102102

103-
alloc := gno.NewAllocator(maxAllocTx, nil)
103+
alloc := gno.NewAllocator(maxAllocTx)
104104
vm.gnoStore = gno.NewStore(alloc, baseStore, iavlStore)
105105
vm.gnoStore.SetNativeResolver(stdlibs.NativeResolver)
106106

@@ -711,7 +711,7 @@ func (vm *VMKeeper) QueryFuncs(ctx sdk.Context, pkgPath string) (fsigs FunctionS
711711
// TODO: modify query protocol to allow MsgEval.
712712
// TODO: then, rename to "Eval".
713713
func (vm *VMKeeper) QueryEval(ctx sdk.Context, pkgPath string, expr string) (res string, err error) {
714-
alloc := gno.NewAllocator(maxAllocQuery, nil)
714+
alloc := gno.NewAllocator(maxAllocQuery)
715715
gnostore := vm.newGnoTransactionStore(ctx) // throwaway (never committed)
716716
pkgAddr := gno.DerivePkgAddr(pkgPath)
717717
// Get Package.
@@ -768,7 +768,7 @@ func (vm *VMKeeper) QueryEval(ctx sdk.Context, pkgPath string, expr string) (res
768768
// TODO: modify query protocol to allow MsgEval.
769769
// TODO: then, rename to "EvalString".
770770
func (vm *VMKeeper) QueryEvalString(ctx sdk.Context, pkgPath string, expr string) (res string, err error) {
771-
alloc := gno.NewAllocator(maxAllocQuery, nil)
771+
alloc := gno.NewAllocator(maxAllocQuery)
772772
gnostore := vm.newGnoTransactionStore(ctx) // throwaway (never committed)
773773
pkgAddr := gno.DerivePkgAddr(pkgPath)
774774
// Get Package.

gnovm/cmd/benchops/store.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func benchmarkDiskStore() BenchStore {
4949
ms.LoadLatestVersion()
5050
msCache := ms.MultiCacheWrap()
5151

52-
alloc := gno.NewAllocator(maxAllocTx, nil)
52+
alloc := gno.NewAllocator(maxAllocTx)
5353
baseSDKStore := msCache.GetStore(baseKey)
5454
iavlSDKStore := msCache.GetStore(iavlKey)
5555
gStore := gno.NewStore(alloc, baseSDKStore, iavlSDKStore)

gnovm/pkg/gnolang/alloc.go

+6-14
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ const (
7474
allocHeapItem = _allocBase + _allocPointer + _allocTypedValue
7575
)
7676

77-
func NewAllocator(maxBytes int64, m *Machine) *Allocator {
77+
func NewAllocator(maxBytes int64) *Allocator {
7878
if maxBytes == 0 {
7979
return nil
8080
}
@@ -219,17 +219,9 @@ func (alloc *Allocator) AllocateHeapItem() {
219219
//----------------------------------------
220220
// constructor utilities.
221221

222-
func (alloc *Allocator) NewString(s string) *StringValue {
223-
// alloc *StringValue
224-
alloc.Allocate(allocString)
225-
226-
// alloc *ArrayValue
227-
baseArr := alloc.NewArrayFromData([]byte(s))
228-
sv := &StringValue{
229-
Base: baseArr,
230-
}
231-
sv.isNewBase = alloc != nil
232-
return sv
222+
func (alloc *Allocator) NewString(s string) StringValue {
223+
alloc.AllocateString(int64(len(s)))
224+
return StringValue(s)
233225
}
234226

235227
func (alloc *Allocator) NewListArray(n int) *ArrayValue {
@@ -409,8 +401,8 @@ func (fv *FuncValue) GetShallowSize() int64 {
409401
return 0
410402
}
411403

412-
func (sv *StringValue) GetShallowSize() int64 {
413-
return allocString
404+
func (sv StringValue) GetShallowSize() int64 {
405+
return allocString + allocStringByte*int64(len(sv))
414406
}
415407

416408
func (nv *NativeValue) GetShallowSize() int64 {

gnovm/pkg/gnolang/garbage_collector.go

-9
Original file line numberDiff line numberDiff line change
@@ -255,16 +255,7 @@ func (pv PointerValue) VisitAssociated(vis Visitor) (stop bool) {
255255
return
256256
}
257257

258-
// If the underlying data of the String is newly allocated,
259-
// include its size in the count.
260-
// If it's reused, do not count its size.
261258
func (sv StringValue) VisitAssociated(vis Visitor) (stop bool) {
262-
if sv.isNewBase {
263-
stop = vis(sv.Base)
264-
if stop {
265-
return
266-
}
267-
}
268259
return false
269260
}
270261

gnovm/pkg/gnolang/machine.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func NewMachineWithOptions(opts MachineOptions) *Machine {
146146
}
147147
alloc := opts.Alloc
148148
if alloc == nil {
149-
alloc = NewAllocator(opts.MaxAllocBytes, nil)
149+
alloc = NewAllocator(opts.MaxAllocBytes)
150150
}
151151
store := opts.Store
152152
if store == nil {

gnovm/pkg/gnolang/machine_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func TestRunMemPackageWithOverrides_revertToOld(t *testing.T) {
5555
v := m.Values[0]
5656
assert.NotNil(t, v)
5757
assert.Equal(t, StringKind, v.T.Kind())
58-
assert.Equal(t, NewStringValue("1"), v.V)
58+
assert.Equal(t, StringValue("1"), v.V)
5959
}
6060

6161
func TestMachineString(t *testing.T) {

gnovm/pkg/gnolang/package.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var Package = amino.RegisterPackage(amino.NewPackage(
1313
//----------------------------------------
1414
// Values
1515
&TypedValue{},
16-
&StringValue{},
16+
StringValue(""),
1717
BigintValue{},
1818
BigdecValue{},
1919
// DataByteValue{}

gnovm/pkg/gnolang/realm.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ func getChildObjects(val Value, more []Value) []Value {
848848
switch cv := val.(type) {
849849
case nil:
850850
return more
851-
case *StringValue:
851+
case StringValue:
852852
return more
853853
case BigintValue:
854854
return more
@@ -1101,7 +1101,7 @@ func copyValueWithRefs(val Value) Value {
11011101
switch cv := val.(type) {
11021102
case nil:
11031103
return nil
1104-
case *StringValue:
1104+
case StringValue:
11051105
return cv
11061106
case BigintValue:
11071107
return cv
@@ -1366,7 +1366,7 @@ func fillTypesOfValue(store Store, val Value) Value {
13661366
switch cv := val.(type) {
13671367
case nil: // do nothing
13681368
return cv
1369-
case *StringValue: // do nothing
1369+
case StringValue: // do nothing
13701370
return cv
13711371
case BigintValue: // do nothing
13721372
return cv

gnovm/pkg/gnolang/uverse.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ func makeUverseNode() {
592592

593593
// ------------------------------------------------------------
594594
// append(*NativeValue, StringValue)
595-
case *StringValue:
595+
case StringValue:
596596
if arg0Type.Elem().Kind() == Uint8Kind {
597597
// TODO this might be faster if reflect supports
598598
// appending this way without first converting to a slice.

gnovm/pkg/gnolang/values.go

+9-52
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ type Value interface {
2626

2727
// Fixed size primitive types are represented in TypedValue.N
2828
// for performance.
29+
func (StringValue) assertValue() {}
2930
func (BigintValue) assertValue() {}
3031
func (BigdecValue) assertValue() {}
3132
func (DataByteValue) assertValue() {}
3233
func (PointerValue) assertValue() {}
33-
func (*StringValue) assertValue() {}
3434
func (*ArrayValue) assertValue() {}
3535
func (*SliceValue) assertValue() {}
3636
func (*StructValue) assertValue() {}
@@ -50,7 +50,7 @@ const (
5050
)
5151

5252
var (
53-
_ Value = &StringValue{}
53+
_ Value = StringValue("")
5454
_ Value = BigintValue{}
5555
_ Value = BigdecValue{}
5656
_ Value = DataByteValue{}
@@ -72,50 +72,7 @@ var (
7272
// ----------------------------------------
7373
// StringValue
7474

75-
// StringValue represents a string.
76-
// Base is an *ArrayValue that holds
77-
// the string’s bytes, and isNewBase
78-
// is a flag set when the Base is
79-
// newly allocated.
80-
// If the Base is not newly allocated
81-
// at runtime, it can be considered as
82-
// reusing the original memory allocated
83-
// during preprocessing. In this case,
84-
// it does not count towards garbage
85-
// collection because it is not a new
86-
// allocation.
87-
type StringValue struct {
88-
isNewBase bool // if the underlying data is new allocated or not.
89-
Base *ArrayValue
90-
}
91-
92-
func NewStringValue(s string) *StringValue {
93-
bz := []byte(s)
94-
sv := &StringValue{
95-
Base: &ArrayValue{
96-
Data: bz,
97-
},
98-
}
99-
return sv
100-
}
101-
102-
func (sv StringValue) MarshalAmino() (string, error) {
103-
if sv.Base == nil {
104-
return "", nil
105-
}
106-
return string(sv.Base.Data), nil
107-
}
108-
109-
func (sv *StringValue) UnmarshalAmino(s string) error {
110-
bz := []byte(s)
111-
112-
sv.Base = &ArrayValue{
113-
Data: bz,
114-
}
115-
sv.isNewBase = true
116-
117-
return nil
118-
}
75+
type StringValue string
11976

12077
// ----------------------------------------
12178
// BigintValue
@@ -1211,7 +1168,7 @@ func (tv *TypedValue) GetBool() bool {
12111168
return *(*bool)(unsafe.Pointer(&tv.N))
12121169
}
12131170

1214-
func (tv *TypedValue) SetString(s *StringValue) {
1171+
func (tv *TypedValue) SetString(s StringValue) {
12151172
if debug {
12161173
if tv.T.Kind() != StringKind || isNative(tv.T) {
12171174
panic(fmt.Sprintf(
@@ -1230,10 +1187,10 @@ func (tv *TypedValue) GetString() string {
12301187
tv.T.String()))
12311188
}
12321189
}
1233-
if tv.V == nil || tv.V.(*StringValue).Base == nil {
1190+
if tv.V == nil {
12341191
return ""
12351192
}
1236-
return string(tv.V.(*StringValue).Base.Data)
1193+
return string(tv.V.(StringValue))
12371194
}
12381195

12391196
func (tv *TypedValue) SetInt(n int) {
@@ -2153,8 +2110,8 @@ func (tv *TypedValue) GetLength() int {
21532110
}
21542111
}
21552112
switch cv := tv.V.(type) {
2156-
case *StringValue:
2157-
return len(cv.Base.Data)
2113+
case StringValue:
2114+
return len(cv)
21582115
case *ArrayValue:
21592116
return cv.GetLength()
21602117
case *SliceValue:
@@ -2719,7 +2676,7 @@ func typedRune(r rune) TypedValue {
27192676
// NOTE: does not allocate; used for panics.
27202677
func typedString(s string) TypedValue {
27212678
tv := TypedValue{T: StringType}
2722-
tv.V = NewStringValue(s)
2679+
tv.V = StringValue(s)
27232680
return tv
27242681
}
27252682

gnovm/pkg/gnolang/values_string.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ func newSeenValues() *seenValues {
4949
return &seenValues{values: make([]Value, 0, defaultSeenValuesSize)}
5050
}
5151

52-
func (v *StringValue) String() string {
53-
return strconv.Quote(string(v.Base.Data))
52+
func (v StringValue) String() string {
53+
return strconv.Quote(string(v))
5454
}
5555

5656
func (bv BigintValue) String() string {

gnovm/pkg/test/test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ func (opts *TestOptions) runTestFiles(
284284

285285
var alloc *gno.Allocator
286286
if opts.Metrics {
287-
alloc = gno.NewAllocator(math.MaxInt64, nil)
287+
alloc = gno.NewAllocator(math.MaxInt64)
288288
}
289289
// reset store ops, if any - we only need them for some filetests.
290290
opts.TestStore.SetLogStoreOps(false)

gnovm/stdlibs/std/native.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func X_assertCallerIsRealm(m *gno.Machine) {
150150

151151
func typedString(s string) gno.TypedValue {
152152
tv := gno.TypedValue{T: gno.StringType}
153-
tv.SetString(gno.NewStringValue(s))
153+
tv.SetString(gno.StringValue(s))
154154
return tv
155155
}
156156

gnovm/tests/files/alloc_0.gno

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ func main() {
1616
}
1717

1818
// Output:
19-
// MemStats: Allocator{maxBytes:100000000, bytes:4856}
19+
// MemStats: Allocator{maxBytes:100000000, bytes:4862}

gnovm/tests/files/alloc_1.gno

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ func main() {
2121
}
2222

2323
// Output:
24-
// MemStats: Allocator{maxBytes:100000000, bytes:5888}
24+
// MemStats: Allocator{maxBytes:100000000, bytes:5904}

gnovm/tests/files/alloc_4.gno

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ func main() {
2222
}
2323

2424
// Output:
25-
// memstats in main after first GC: Allocator{maxBytes:50000, bytes:36131}
26-
// memstats in main after second GC: Allocator{maxBytes:50000, bytes:5525}
25+
// memstats in main after first GC: Allocator{maxBytes:50000, bytes:12389}
26+
// memstats in main after second GC: Allocator{maxBytes:50000, bytes:5309}

gnovm/tests/files/alloc_7.gno

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ func main() {
1313
}
1414

1515
// Output:
16-
// MemStats: Allocator{maxBytes:100000000, bytes:4700}
16+
// MemStats: Allocator{maxBytes:100000000, bytes:4496}

gnovm/tests/stdlibs/std/std.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ type RealmOverride struct {
2727

2828
func AssertOriginCall(m *gno.Machine) {
2929
if !isOriginCall(m) {
30-
m.Panic(typedString(gno.NewStringValue("invalid non-origin call")))
30+
m.Panic(typedString("invalid non-origin call"))
3131
}
3232
}
3333

34-
func typedString(s *gno.StringValue) gno.TypedValue {
34+
func typedString(s gno.StringValue) gno.TypedValue {
3535
tv := gno.TypedValue{T: gno.StringType}
3636
tv.SetString(s)
3737
return tv
@@ -71,7 +71,7 @@ func TestSkipHeights(m *gno.Machine, count int64) {
7171

7272
func X_callerAt(m *gno.Machine, n int) string {
7373
if n <= 0 {
74-
m.Panic(typedString(gno.NewStringValue("GetCallerAt requires positive arg")))
74+
m.Panic(typedString("GetCallerAt requires positive arg"))
7575
return ""
7676
}
7777
// Add 1 to n to account for the GetCallerAt (gno fn) frame.
@@ -80,7 +80,7 @@ func X_callerAt(m *gno.Machine, n int) string {
8080
// NOTE: the last frame's LastPackage
8181
// is set to the original non-frame
8282
// package, so need this check.
83-
m.Panic(typedString(gno.NewStringValue("frame not found")))
83+
m.Panic(typedString("frame not found"))
8484
return ""
8585
}
8686
if n == m.NumFrames()-1 {

0 commit comments

Comments
 (0)