Skip to content
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
7 changes: 7 additions & 0 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ var (
ErrIterationAborted = errors.New("iteration aborted")
ErrMapIncompatible = errors.New("map spec is incompatible with existing map")
errMapNoBTFValue = errors.New("map spec does not contain a BTF Value")

// pre-allocating these errors here since they may get called in hot code paths
// and cause unnecessary memory allocations
errMapLookupKeyNotExist = fmt.Errorf("lookup: %w", sysErrKeyNotExist)
)

// MapOptions control loading a map into the kernel.
Expand Down Expand Up @@ -695,6 +699,9 @@ func (m *Map) lookup(key interface{}, valueOut sys.Pointer, flags MapLookupFlags
}

if err = sys.MapLookupElem(&attr); err != nil {
if errors.Is(err, unix.ENOENT) {
return errMapLookupKeyNotExist
}
return fmt.Errorf("lookup: %w", wrapMapError(err))
}
return nil
Expand Down
11 changes: 11 additions & 0 deletions map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,17 @@ func TestMapLookupKeyTooSmall(t *testing.T) {
qt.Assert(t, qt.IsNotNil(m.Lookup(uint32(0), &small)))
}

func TestMapLookupKeyNotFoundAllocations(t *testing.T) {
m := createArray(t)
defer m.Close()
var key, out uint32 = 3, 0

allocs := testing.AllocsPerRun(5, func() {
_ = m.Lookup(&key, &out)
})
qt.Assert(t, qt.Equals(allocs, float64(0)))
}

func TestBatchAPIMapDelete(t *testing.T) {
if err := haveBatchAPI(); err != nil {
t.Skipf("batch api not available: %v", err)
Expand Down