From 97df4571350c581c9e14efddb5a05bf11c1389dd Mon Sep 17 00:00:00 2001 From: thc1006 <84045975+thc1006@users.noreply.github.com> Date: Sun, 20 Sep 2026 17:14:04 +0800 Subject: [PATCH] refactor(gthulhu): copy the applied set with maps.Clone and tidy comments Follow-up polish to the merged #17. No behavior change. Replace the hand-written applied-set copy in GetChangedStrategies with maps.Clone, use for range in the concurrency test, and shorten the strategyMap/appliedStrategyMap field comment so it is not longer than the two fields it documents. gofmt -s, go vet and go test -race are clean. Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com> --- plugin/gthulhu/gthulhu.go | 11 ++++------- plugin/gthulhu/gthulhu_test.go | 4 ++-- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/plugin/gthulhu/gthulhu.go b/plugin/gthulhu/gthulhu.go index b519178..1bf74f8 100644 --- a/plugin/gthulhu/gthulhu.go +++ b/plugin/gthulhu/gthulhu.go @@ -3,6 +3,7 @@ package gthulhu import ( "context" "log" + "maps" "sync" "time" @@ -66,9 +67,8 @@ type GthulhuPlugin struct { // Global vruntime minVruntime uint64 - // strategyMap is the latest desired strategy set (keyed by task id); - // appliedStrategyMap is the set last handed to the scheduler, so - // GetChangedStrategies can diff the two into a coalesced changed/removed set. + // strategyMap is the desired set; appliedStrategyMap is the last set handed + // to the scheduler. GetChangedStrategies diffs them into changed/removed. strategyMap map[int32]util.SchedulingStrategy appliedStrategyMap map[int32]util.SchedulingStrategy strategyMu sync.RWMutex @@ -419,9 +419,6 @@ func (g *GthulhuPlugin) GetChangedStrategies() ([]util.SchedulingStrategy, []uti } } - g.appliedStrategyMap = make(map[int32]util.SchedulingStrategy, len(g.strategyMap)) - for pid, strategy := range g.strategyMap { - g.appliedStrategyMap[pid] = strategy - } + g.appliedStrategyMap = maps.Clone(g.strategyMap) return changed, removed } diff --git a/plugin/gthulhu/gthulhu_test.go b/plugin/gthulhu/gthulhu_test.go index 1e077e9..e668dc9 100644 --- a/plugin/gthulhu/gthulhu_test.go +++ b/plugin/gthulhu/gthulhu_test.go @@ -293,11 +293,11 @@ func TestGetChangedStrategiesConcurrent(t *testing.T) { g.UpdateStrategyMap([]util.SchedulingStrategy{{PID: 1, Priority: 1}}) var wg sync.WaitGroup - for i := 0; i < 4; i++ { + for range 4 { wg.Add(1) go func() { defer wg.Done() - for j := 0; j < 500; j++ { + for range 500 { g.GetChangedStrategies() } }()