From 159388cce8ca7c4ff0441c63c12631674182185c Mon Sep 17 00:00:00 2001 From: Ettore Di Giacinto Date: Sun, 18 May 2025 08:55:44 +0200 Subject: [PATCH] chore: memoize detected GPUs (#5385) Signed-off-by: Ettore Di Giacinto --- pkg/xsysinfo/gpu.go | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/pkg/xsysinfo/gpu.go b/pkg/xsysinfo/gpu.go index 17b2ec78..bfcf9a59 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" ) -func GPUs() ([]*gpu.GraphicsCard, error) { - gpu, err := ghw.GPU() - if err != nil { - return nil, err - } +var ( + gpuCache []*gpu.GraphicsCard + gpuCacheOnce sync.Once + gpuCacheErr error +) - return gpu.GraphicsCards, nil +func GPUs() ([]*gpu.GraphicsCard, error) { + gpuCacheOnce.Do(func() { + gpu, err := ghw.GPU() + if err != nil { + gpuCacheErr = err + return + } + gpuCache = gpu.GraphicsCards + }) + + return gpuCache, gpuCacheErr } func TotalAvailableVRAM() (uint64, error) {