Skip to content

Commit 3002fd4

Browse files
committed
Inline all byte to string conversions
Signed-off-by: Sam Batschelet <sam.batschelet@avalabs.org>
1 parent 81d0cb9 commit 3002fd4

File tree

2 files changed

+19
-24
lines changed

2 files changed

+19
-24
lines changed

tstate/tstate.go

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
)
1616

1717
type op struct {
18-
k string
18+
k []byte
1919

2020
pastExists bool
2121
pastV []byte
@@ -67,22 +67,21 @@ func (ts *TState) GetValue(ctx context.Context, key []byte) ([]byte, error) {
6767
if !ts.checkScope(ctx, key) {
6868
return nil, ErrKeyNotSpecified
6969
}
70-
k := string(key)
71-
v, _, exists := ts.getValue(ctx, k)
70+
v, _, exists := ts.getValue(ctx, key)
7271
if !exists {
7372
return nil, database.ErrNotFound
7473
}
7574
return v, nil
7675
}
7776

78-
func (ts *TState) getValue(_ context.Context, key string) ([]byte, bool, bool) {
79-
if v, ok := ts.changedKeys[key]; ok {
77+
func (ts *TState) getValue(_ context.Context, key []byte) ([]byte, bool, bool) {
78+
if v, ok := ts.changedKeys[string(key)]; ok {
8079
if v.removed {
8180
return nil, true, false
8281
}
8382
return v.v, true, true
8483
}
85-
v, ok := ts.scopeStorage[key]
84+
v, ok := ts.scopeStorage[string(key)]
8685
if !ok {
8786
return nil, false, false
8887
}
@@ -95,23 +94,22 @@ func (ts *TState) getValue(_ context.Context, key string) ([]byte, bool, bool) {
9594
func (ts *TState) FetchAndSetScope(ctx context.Context, keys [][]byte, db Database) error {
9695
ts.scopeStorage = map[string][]byte{}
9796
for _, key := range keys {
98-
k := string(key)
99-
if val, ok := ts.fetchCache[k]; ok {
97+
if val, ok := ts.fetchCache[string(key)]; ok {
10098
if val.Exists {
101-
ts.scopeStorage[k] = val.Value
99+
ts.scopeStorage[string(key)] = val.Value
102100
}
103101
continue
104102
}
105103
v, err := db.GetValue(ctx, key)
106104
if errors.Is(err, database.ErrNotFound) {
107-
ts.fetchCache[k] = &cacheItem{Exists: false}
105+
ts.fetchCache[string(key)] = &cacheItem{Exists: false}
108106
continue
109107
}
110108
if err != nil {
111109
return err
112110
}
113-
ts.fetchCache[k] = &cacheItem{Value: v, Exists: true}
114-
ts.scopeStorage[k] = v
111+
ts.fetchCache[string(key)] = &cacheItem{Value: v, Exists: true}
112+
ts.scopeStorage[string(key)] = v
115113
}
116114
ts.scope = keys
117115
return nil
@@ -139,15 +137,14 @@ func (ts *TState) Insert(ctx context.Context, key []byte, value []byte) error {
139137
if !ts.checkScope(ctx, key) {
140138
return ErrKeyNotSpecified
141139
}
142-
k := string(key)
143-
past, changed, exists := ts.getValue(ctx, k)
140+
past, changed, exists := ts.getValue(ctx, key)
144141
ts.ops = append(ts.ops, &op{
145-
k: k,
142+
k: key,
146143
pastExists: exists,
147144
pastV: past,
148145
pastChanged: changed,
149146
})
150-
ts.changedKeys[k] = &tempStorage{value, false}
147+
ts.changedKeys[string(key)] = &tempStorage{value, false}
151148
return nil
152149
}
153150

@@ -156,18 +153,17 @@ func (ts *TState) Remove(ctx context.Context, key []byte) error {
156153
if !ts.checkScope(ctx, key) {
157154
return ErrKeyNotSpecified
158155
}
159-
k := string(key)
160-
past, changed, exists := ts.getValue(ctx, k)
156+
past, changed, exists := ts.getValue(ctx, key)
161157
if !exists {
162158
return nil
163159
}
164160
ts.ops = append(ts.ops, &op{
165-
k: k,
161+
k: key,
166162
pastExists: true,
167163
pastV: past,
168164
pastChanged: changed,
169165
})
170-
ts.changedKeys[k] = &tempStorage{nil, true}
166+
ts.changedKeys[string(key)] = &tempStorage{nil, true}
171167
return nil
172168
}
173169

@@ -188,13 +184,13 @@ func (ts *TState) Rollback(_ context.Context, restorePoint int) {
188184
//
189185
// remove: Removed key that was modified for first time in run
190186
if !op.pastChanged {
191-
delete(ts.changedKeys, op.k)
187+
delete(ts.changedKeys, string(op.k))
192188
continue
193189
}
194190
// insert: Modified key for the nth time
195191
//
196192
// remove: Removed key that was previously modified in run
197-
ts.changedKeys[op.k] = &tempStorage{op.pastV, !op.pastExists}
193+
ts.changedKeys[string(op.k)] = &tempStorage{op.pastV, !op.pastExists}
198194
}
199195
ts.ops = ts.ops[:restorePoint]
200196
}

tstate/tstate_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,6 @@ func BenchmarkInsert(b *testing.B) {
342342
}
343343
}
344344

345-
346345
func BenchmarkGetValue(b *testing.B) {
347346
for _, size := range []int{4, 8, 16, 32, 64, 128} {
348347
b.Run(fmt.Sprintf("get_%d_keys", size), func(b *testing.B) {
@@ -438,4 +437,4 @@ func randomBytes(size int) []byte {
438437
bytes := make([]byte, size)
439438
rand.Read(bytes)
440439
return bytes
441-
}
440+
}

0 commit comments

Comments
 (0)