From 0c859addce27674bf31d4d1f7dfb3a7f080b2a6d Mon Sep 17 00:00:00 2001 From: Ettore Di Giacinto Date: Sat, 17 May 2025 22:10:10 +0200 Subject: [PATCH] chore: memoize detected GPUs Signed-off-by: Ettore Di Giacinto --- pkg/xsysinfo/gpu.go | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/pkg/xsysinfo/gpu.go b/pkg/xsysinfo/gpu.go index 17b2ec7834b9..bfcf9a59d191 100644 --- a/pkg/xsysinfo/gpu.go +++ b/pkg/xsysinfo/gpu.go @@ -2,18 +2,29 @@ package xsysinfo import ( "strings" + "sync" "github.com/jaypipes/ghw" "github.com/jaypipes/ghw/pkg/gpu" ) +var ( + gpuCache []*gpu.GraphicsCard + gpuCacheOnce sync.Once + gpuCacheErr error +) + func GPUs() ([]*gpu.GraphicsCard, error) { - gpu, err := ghw.GPU() - if err != nil { - return nil, err - } + gpuCacheOnce.Do(func() { + gpu, err := ghw.GPU() + if err != nil { + gpuCacheErr = err + return + } + gpuCache = gpu.GraphicsCards + }) - return gpu.GraphicsCards, nil + return gpuCache, gpuCacheErr } func TotalAvailableVRAM() (uint64, error) {