From 3f2d0b46c3f39c815f41e1282fc3af154ae000b4 Mon Sep 17 00:00:00 2001 From: kwakubiney Date: Tue, 28 Nov 2023 09:38:55 +0000 Subject: [PATCH] internal: replace internal memoize with sync.OnceValues Signed-off-by: kwakubiney --- btf/btf_test.go | 3 ++- cpu.go | 5 ++--- internal/memoize.go | 26 -------------------------- internal/testutils/cgroup.go | 4 ++-- internal/tracefs/kprobe.go | 3 ++- internal/version.go | 3 ++- map.go | 3 ++- 7 files changed, 12 insertions(+), 35 deletions(-) delete mode 100644 internal/memoize.go diff --git a/btf/btf_test.go b/btf/btf_test.go index 843acadfe..a0bf98b00 100644 --- a/btf/btf_test.go +++ b/btf/btf_test.go @@ -7,6 +7,7 @@ import ( "fmt" "io" "os" + "sync" "testing" "github.com/cilium/ebpf/internal" @@ -36,7 +37,7 @@ type specAndRawBTF struct { spec *Spec } -var vmlinuxTestdata = internal.Memoize(func() (specAndRawBTF, error) { +var vmlinuxTestdata = sync.OnceValues(func() (specAndRawBTF, error) { b, err := internal.ReadAllCompressed("testdata/vmlinux.btf.gz") if err != nil { return specAndRawBTF{}, err diff --git a/cpu.go b/cpu.go index 1397d6de9..07e959efd 100644 --- a/cpu.go +++ b/cpu.go @@ -4,11 +4,10 @@ import ( "fmt" "os" "strings" - - "github.com/cilium/ebpf/internal" + "sync" ) -var possibleCPU = internal.Memoize(func() (int, error) { +var possibleCPU = sync.OnceValues(func() (int, error) { return parseCPUsFromFile("/sys/devices/system/cpu/possible") }) diff --git a/internal/memoize.go b/internal/memoize.go deleted file mode 100644 index 3de0a3fb9..000000000 --- a/internal/memoize.go +++ /dev/null @@ -1,26 +0,0 @@ -package internal - -import ( - "sync" -) - -type memoizedFunc[T any] struct { - once sync.Once - fn func() (T, error) - result T - err error -} - -func (mf *memoizedFunc[T]) do() (T, error) { - mf.once.Do(func() { - mf.result, mf.err = mf.fn() - }) - return mf.result, mf.err -} - -// Memoize the result of a function call. -// -// fn is only ever called once, even if it returns an error. -func Memoize[T any](fn func() (T, error)) func() (T, error) { - return (&memoizedFunc[T]{fn: fn}).do -} diff --git a/internal/testutils/cgroup.go b/internal/testutils/cgroup.go index ab3183392..bf7fbc6c9 100644 --- a/internal/testutils/cgroup.go +++ b/internal/testutils/cgroup.go @@ -4,13 +4,13 @@ import ( "errors" "os" "strings" + "sync" "testing" - "github.com/cilium/ebpf/internal" "github.com/cilium/ebpf/internal/unix" ) -var cgroup2Path = internal.Memoize(func() (string, error) { +var cgroup2Path = sync.OnceValues(func() (string, error) { mounts, err := os.ReadFile("/proc/mounts") if err != nil { return "", err diff --git a/internal/tracefs/kprobe.go b/internal/tracefs/kprobe.go index 1b45a9a74..897740fec 100644 --- a/internal/tracefs/kprobe.go +++ b/internal/tracefs/kprobe.go @@ -8,6 +8,7 @@ import ( "path/filepath" "runtime" "strings" + "sync" "syscall" "github.com/cilium/ebpf/internal" @@ -110,7 +111,7 @@ func sanitizeTracefsPath(path ...string) (string, error) { // Since kernel 4.1 tracefs should be mounted by default at /sys/kernel/tracing, // but may be also be available at /sys/kernel/debug/tracing if debugfs is mounted. // The available tracefs paths will depends on distribution choices. -var getTracefsPath = internal.Memoize(func() (string, error) { +var getTracefsPath = sync.OnceValues(func() (string, error) { for _, p := range []struct { path string fsType int64 diff --git a/internal/version.go b/internal/version.go index 9b17ffb44..acd4650af 100644 --- a/internal/version.go +++ b/internal/version.go @@ -2,6 +2,7 @@ package internal import ( "fmt" + "sync" "github.com/cilium/ebpf/internal/unix" ) @@ -79,7 +80,7 @@ func (v Version) Kernel() uint32 { } // KernelVersion returns the version of the currently running kernel. -var KernelVersion = Memoize(func() (Version, error) { +var KernelVersion = sync.OnceValues(func() (Version, error) { return detectKernelVersion() }) diff --git a/map.go b/map.go index dc2524590..7781a4897 100644 --- a/map.go +++ b/map.go @@ -10,6 +10,7 @@ import ( "path/filepath" "reflect" "strings" + "sync" "time" "unsafe" @@ -907,7 +908,7 @@ func (m *Map) nextKey(key interface{}, nextKeyOut sys.Pointer) error { return nil } -var mmapProtectedPage = internal.Memoize(func() ([]byte, error) { +var mmapProtectedPage = sync.OnceValues(func() ([]byte, error) { return unix.Mmap(-1, 0, os.Getpagesize(), unix.PROT_NONE, unix.MAP_ANON|unix.MAP_SHARED) })