From fdac2853afc629ff78dd95bac00beac5741f0554 Mon Sep 17 00:00:00 2001 From: yusing Date: Fri, 4 Apr 2025 00:46:24 +0800 Subject: [PATCH] chore: update set/map parallel logic --- internal/utils/functional/common.go | 3 +++ internal/utils/functional/map.go | 2 -- internal/utils/functional/set.go | 5 +++++ 3 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 internal/utils/functional/common.go diff --git a/internal/utils/functional/common.go b/internal/utils/functional/common.go new file mode 100644 index 0000000..a37b162 --- /dev/null +++ b/internal/utils/functional/common.go @@ -0,0 +1,3 @@ +package functional + +const minParallelSize = 2 diff --git a/internal/utils/functional/map.go b/internal/utils/functional/map.go index f628371..5dcfde1 100644 --- a/internal/utils/functional/map.go +++ b/internal/utils/functional/map.go @@ -11,8 +11,6 @@ type Map[KT comparable, VT any] struct { *xsync.MapOf[KT, VT] } -const minParallelSize = 4 - func NewMapOf[KT comparable, VT any](options ...func(*xsync.MapConfig)) Map[KT, VT] { return Map[KT, VT]{xsync.NewMapOf[KT, VT](options...)} } diff --git a/internal/utils/functional/set.go b/internal/utils/functional/set.go index c238ae8..f67040a 100644 --- a/internal/utils/functional/set.go +++ b/internal/utils/functional/set.go @@ -45,6 +45,11 @@ func (set Set[T]) RangeAll(f func(T)) { } func (set Set[T]) RangeAllParallel(f func(T)) { + if set.Size() < minParallelSize { + set.RangeAll(f) + return + } + var wg sync.WaitGroup set.Range(func(k T) bool {