Skip to content

Commit

Permalink
Use unsafe.Sizeof instead of magic number
Browse files Browse the repository at this point in the history
Previously, the constant expression used hardcoded numbers.
  • Loading branch information
fxamacker committed Oct 2, 2023
1 parent 53a716f commit 7a2aeb1
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"sort"
"strings"
"sync"
"unsafe"

"github.com/fxamacker/cbor/v2"
)
Expand All @@ -37,20 +38,20 @@ const LedgerBaseStorageSlabPrefix = "$"
// By contrast, SlabID is affected by inlining because it identifies
// a slab in storage. Given this, ValueID should be used for
// resource tracking, etc.
type ValueID [16]byte
type ValueID [unsafe.Sizeof(Address{}) + unsafe.Sizeof(SlabIndex{})]byte

var emptyValueID = ValueID{}

func slabIDToValueID(sid SlabID) ValueID {
var id ValueID
copy(id[:], sid.address[:])
copy(id[8:], sid.index[:])
n := copy(id[:], sid.address[:])
copy(id[n:], sid.index[:])
return id
}

func (vid ValueID) equal(sid SlabID) bool {
return bytes.Equal(vid[:8], sid.address[:]) &&
bytes.Equal(vid[8:], sid.index[:])
return bytes.Equal(vid[:len(sid.address)], sid.address[:]) &&
bytes.Equal(vid[len(sid.address):], sid.index[:])
}

func (vid ValueID) String() string {
Expand Down

0 comments on commit 7a2aeb1

Please sign in to comment.