Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

map: avoid allocations during batch lookup of common types #1254

Merged
merged 3 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/sysenc/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func UnsafeBuffer(ptr unsafe.Pointer) Buffer {

// SyscallOutput prepares a Buffer for a syscall to write into.
//
// size is the length of the desired buffer in bytes.
// The buffer may point at the underlying memory of dst, in which case [Unmarshal]
// becomes a no-op.
//
Expand Down
17 changes: 7 additions & 10 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -1048,15 +1048,14 @@ func (m *Map) batchLookup(cmd sys.Cmd, cursor *BatchCursor, keysOut, valuesOut i
if count != valuesValue.Len() {
return 0, fmt.Errorf("keysOut and valuesOut must be the same length")
}
keyBuf := make([]byte, count*int(m.keySize))
keyPtr := sys.NewSlicePointer(keyBuf)
valueBuf := make([]byte, count*int(m.fullValueSize))
valuePtr := sys.NewSlicePointer(valueBuf)

keyBuf := sysenc.SyscallOutput(keysOut, count*int(m.keySize))
valueBuf := sysenc.SyscallOutput(valuesOut, count*int(m.fullValueSize))

attr := sys.MapLookupBatchAttr{
MapFd: m.fd.Uint(),
Keys: keyPtr,
Values: valuePtr,
Keys: keyBuf.Pointer(),
Values: valueBuf.Pointer(),
Count: uint32(count),
InBatch: sys.NewSlicePointer(inBatch),
OutBatch: sys.NewSlicePointer(cursor.opaque),
Expand All @@ -1073,12 +1072,10 @@ func (m *Map) batchLookup(cmd sys.Cmd, cursor *BatchCursor, keysOut, valuesOut i
return 0, sysErr
}

err := sysenc.Unmarshal(keysOut, keyBuf)
if err != nil {
if err := keyBuf.Unmarshal(keysOut); err != nil {
return 0, err
}
err = sysenc.Unmarshal(valuesOut, valueBuf)
if err != nil {
if err := valueBuf.Unmarshal(valuesOut); err != nil {
return 0, err
}

Expand Down
61 changes: 52 additions & 9 deletions map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1093,9 +1093,8 @@ func TestMapIteratorAllocations(t *testing.T) {

var k, v uint32
iter := arr.Iterate()
// Allocate any necessary temporary buffers.
qt.Assert(t, qt.IsTrue(iter.Next(&k, &v)))

// AllocsPerRun warms up the function for us.
allocs := testing.AllocsPerRun(1, func() {
if !iter.Next(&k, &v) {
t.Fatal("Next failed")
Expand All @@ -1105,6 +1104,35 @@ func TestMapIteratorAllocations(t *testing.T) {
qt.Assert(t, qt.Equals(allocs, float64(0)))
}

func TestMapBatchLookupAllocations(t *testing.T) {
testutils.SkipIfNotSupported(t, haveBatchAPI())

arr, err := NewMap(&MapSpec{
Type: Array,
KeySize: 4,
ValueSize: 4,
MaxEntries: 10,
})
if err != nil {
t.Fatal(err)
}
defer arr.Close()

var cursor BatchCursor
tmp := make([]uint32, 2)
input := any(tmp)

// AllocsPerRun warms up the function for us.
allocs := testing.AllocsPerRun(1, func() {
_, err := arr.BatchLookup(&cursor, input, input, nil)
if err != nil {
t.Fatal(err)
}
})

qt.Assert(t, qt.Equals(allocs, 0))
}

func TestMapIterateHashKeyOneByteFull(t *testing.T) {
hash, err := NewMap(&MapSpec{
Type: Hash,
Expand Down Expand Up @@ -2040,6 +2068,7 @@ func BenchmarkIterate(b *testing.B) {
var k, v uint64

b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
iter := m.Iterate()
Expand All @@ -2056,6 +2085,7 @@ func BenchmarkIterate(b *testing.B) {
var k, v uint64

b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
b.StopTimer()
Expand All @@ -2081,12 +2111,18 @@ func BenchmarkIterate(b *testing.B) {
v := make([]uint64, m.MaxEntries())

b.ReportAllocs()
b.ResetTimer()

var cursor BatchCursor
for i := 0; i < b.N; i++ {
_, err := m.BatchLookup(&cursor, k, v, nil)
if err != nil && !errors.Is(err, ErrKeyNotExist) {
b.Fatal(err)
var cursor BatchCursor
for {
_, err := m.BatchLookup(&cursor, k, v, nil)
if errors.Is(err, ErrKeyNotExist) {
break
}
if err != nil {
b.Fatal(err)
}
}
}
})
Expand All @@ -2096,6 +2132,7 @@ func BenchmarkIterate(b *testing.B) {
v := make([]uint64, m.MaxEntries())

b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
b.StopTimer()
Expand All @@ -2105,15 +2142,21 @@ func BenchmarkIterate(b *testing.B) {
b.StartTimer()

var cursor BatchCursor
_, err := m.BatchLookupAndDelete(&cursor, k, v, nil)
if err != nil && !errors.Is(err, ErrKeyNotExist) {
b.Fatal(err)
for {
_, err := m.BatchLookupAndDelete(&cursor, k, v, nil)
if errors.Is(err, ErrKeyNotExist) {
break
}
if err != nil {
b.Fatal(err)
}
}
}
})

b.Run("BatchDelete", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
b.StopTimer()
Expand Down