github.com/livekit/protocol@v1.16.1-0.20240517185851-47e4c6bba773/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 wg.Add(numCPU) 41 for p := 0; p < numCPU; p++ { 42 go func() { 43 defer wg.Done() 44 for { 45 n := start.Add(step) 46 if n >= end+step { 47 return 48 } 49 50 for i := n - step; i < n && i < end; i++ { 51 fn(vals[i]) 52 } 53 } 54 }() 55 } 56 wg.Wait() 57 }