From 136c3ae94a075515fcea990d252b72bcbcc33fa6 Mon Sep 17 00:00:00 2001 From: Changxin Miao Date: Sun, 19 Jan 2025 10:52:54 +0800 Subject: [PATCH] Simplify Signed-off-by: Changxin Miao --- pkg/meta/base.go | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/pkg/meta/base.go b/pkg/meta/base.go index 1e4d6276c5b6..e682bd615e07 100644 --- a/pkg/meta/base.go +++ b/pkg/meta/base.go @@ -26,6 +26,7 @@ import ( "path" "reflect" "runtime" + "slices" "sort" "strings" "sync" @@ -395,21 +396,18 @@ func (r *baseMeta) txBatchLock(inodes ...Ino) func() { r.txLock(uint(inodes[0])) return func() { r.txUnlock(uint(inodes[0])) } default: // for rename and more - sort.Slice(inodes, func(i, j int) bool { return inodes[i]%nlocks < inodes[j]%nlocks }) - lockedKeys := make(map[uint]struct{}, 2) - lockedInodes := make([]uint, 0, len(inodes)) - for _, ino := range inodes { - ino := uint(ino) - if _, locked := lockedKeys[ino%nlocks]; locked { - continue // Go does not support recursive locks - } - lockedKeys[ino%nlocks] = struct{}{} - r.txLock(ino) - lockedInodes = append(lockedInodes, ino) + inodeSlots := make([]uint, len(inodes)) + for i, ino := range inodes { + inodeSlots[i] = uint(ino) % nlocks + } + slices.Sort(inodeSlots) + inodeSlots = slices.Compact(inodeSlots) // Go does not support recursive locks + for _, idx := range inodeSlots { + r.txlocks[idx].Lock() } return func() { - for _, ino := range lockedInodes { - r.txUnlock(ino) + for _, idx := range inodeSlots { + r.txlocks[idx].Unlock() } } }