Skip to content

Commit 156dfcd

Browse files
committed
Convert slice to array pointer vs copy
Signed-off-by: Sam Batschelet <[email protected]>
1 parent c688a54 commit 156dfcd

File tree

2 files changed

+17
-18
lines changed

2 files changed

+17
-18
lines changed

chain/processor.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type fetchData struct {
2020

2121
type txData struct {
2222
tx *Transaction
23-
storage map[tstate.Key][]byte
23+
storage map[*tstate.Key][]byte
2424
}
2525

2626
type Processor struct {
@@ -49,9 +49,9 @@ func (p *Processor) Prefetch(ctx context.Context, db Database) {
4949
defer span.End()
5050

5151
// Store required keys for each set
52-
alreadyFetched := make(map[tstate.Key]*fetchData, len(p.blk.GetTxs()))
52+
alreadyFetched := make(map[*tstate.Key]*fetchData, len(p.blk.GetTxs()))
5353
for _, tx := range p.blk.GetTxs() {
54-
storage := map[tstate.Key][]byte{}
54+
storage := map[*tstate.Key][]byte{}
5555
for _, k := range tx.StateKeys(sm) {
5656
sk := tstate.ToStateKeyArray(k)
5757
if v, ok := alreadyFetched[sk]; ok {

tstate/tstate.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
)
1717

1818
type op struct {
19-
k Key
19+
k *Key
2020

2121
pastExists bool
2222
pastV []byte
@@ -39,14 +39,14 @@ type Key [MapKeyLength]byte
3939

4040
// TState defines a struct for storing temporary state.
4141
type TState struct {
42-
changedKeys map[Key]*tempStorage
43-
fetchCache map[Key]*cacheItem // in case we evict and want to re-fetch
42+
changedKeys map[*Key]*tempStorage
43+
fetchCache map[*Key]*cacheItem // in case we evict and want to re-fetch
4444

4545
// We don't differentiate between read and write scope because it is very
4646
// uncommon for a user to write something without first reading what is
4747
// there.
4848
scope [][]byte // stores a list of managed keys in the TState struct
49-
scopeStorage map[Key][]byte
49+
scopeStorage map[*Key][]byte
5050

5151
// Ops is a record of all operations performed on [TState]. Tracking
5252
// operations allows for reverting state to a certain point-in-time.
@@ -57,9 +57,9 @@ type TState struct {
5757
// maps to have an initial size of [storageSize] and [changedSize] respectively.
5858
func New(changedSize int) *TState {
5959
return &TState{
60-
changedKeys: make(map[Key]*tempStorage, changedSize),
60+
changedKeys: make(map[*Key]*tempStorage, changedSize),
6161

62-
fetchCache: map[Key]*cacheItem{},
62+
fetchCache: map[*Key]*cacheItem{},
6363

6464
ops: make([]*op, 0, changedSize),
6565
}
@@ -79,7 +79,7 @@ func (ts *TState) GetValue(ctx context.Context, key []byte) ([]byte, error) {
7979
return v, nil
8080
}
8181

82-
func (ts *TState) getValue(_ context.Context, key Key) ([]byte, bool, bool) {
82+
func (ts *TState) getValue(_ context.Context, key *Key) ([]byte, bool, bool) {
8383
if v, ok := ts.changedKeys[key]; ok {
8484
if v.removed {
8585
return nil, true, false
@@ -97,7 +97,7 @@ func (ts *TState) getValue(_ context.Context, key Key) ([]byte, bool, bool) {
9797
// FetchAndSetScope then sets the scope of ts to [keys]. If a key exists in
9898
// ts.fetchCache set the key's value to the value from cache.
9999
func (ts *TState) FetchAndSetScope(ctx context.Context, keys [][]byte, db Database) error {
100-
ts.scopeStorage = map[Key][]byte{}
100+
ts.scopeStorage = map[*Key][]byte{}
101101

102102
for _, key := range keys {
103103
k := ToStateKeyArray(key)
@@ -123,7 +123,7 @@ func (ts *TState) FetchAndSetScope(ctx context.Context, keys [][]byte, db Databa
123123
}
124124

125125
// SetReadScope sets the readscope of ts to [keys].
126-
func (ts *TState) SetScope(_ context.Context, keys [][]byte, storage map[Key][]byte) {
126+
func (ts *TState) SetScope(_ context.Context, keys [][]byte, storage map[*Key][]byte) {
127127
ts.scope = keys
128128
ts.scopeStorage = storage
129129
}
@@ -221,22 +221,21 @@ func (ts *TState) WriteChanges(
221221
defer span.End()
222222

223223
for key, tstorage := range ts.changedKeys {
224+
k := key[:]
224225
if !tstorage.removed {
225-
if err := db.Insert(ctx, key[:], tstorage.v); err != nil {
226+
if err := db.Insert(ctx, k, tstorage.v); err != nil {
226227
return err
227228
}
228229
continue
229230
}
230-
if err := db.Remove(ctx, key[:]); err != nil {
231+
if err := db.Remove(ctx, k); err != nil {
231232
return err
232233
}
233234
}
234235
return nil
235236
}
236237

237238
// ToStateKeyArray converts a byte slice to byte array.
238-
func ToStateKeyArray(key []byte) Key {
239-
var k Key
240-
copy(k[:], key)
241-
return k
239+
func ToStateKeyArray(key []byte) *Key {
240+
return (*Key)(key)
242241
}

0 commit comments

Comments
 (0)