github.com/livekit/protocol@v1.39.3/utils/parallel.go (about) 1 // Copyright 2023 LiveKit, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package utils 16 17 import ( 18 "runtime" 19 "sync" 20 21 "go.uber.org/atomic" 22 ) 23 24 // ParallelExec will executes the given function with each element of vals, if len(vals) >= parallelThreshold, 25 // will execute them in parallel, with the given step size. So fn must be thread-safe. 26 func ParallelExec[T any](vals []T, parallelThreshold, step uint64, fn func(T)) { 27 if uint64(len(vals)) < parallelThreshold { 28 for _, v := range vals { 29 fn(v) 30 } 31 return 32 } 33 34 // parallel - enables much more efficient multi-core utilization 35 start := atomic.NewUint64(0) 36 end := uint64(len(vals)) 37 38 var wg sync.WaitGroup 39 numCPU := runtime.NumCPU() 40 if numCPU > len(vals) { 41 numCPU = len(vals) 42 } 43 wg.Add(numCPU) 44 for p := 0; p < numCPU; p++ { 45 go func() { 46 defer wg.Done() 47 for { 48 n := start.Add(step) 49 if n >= end+step { 50 return 51 } 52 53 for i := n - step; i < n && i < end; i++ { 54 fn(vals[i]) 55 } 56 } 57 }() 58 } 59 wg.Wait() 60 }