Skip to content

Commit

Permalink
feat:add pool to init once
Browse files Browse the repository at this point in the history
  • Loading branch information
CocaineCong committed Jul 12, 2024
1 parent 7e04654 commit 1101219
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
3 changes: 1 addition & 2 deletions pkg/util/distance/calc_distance.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@ func CalcFloatDistance(dim int64, left, right []float32, metric string) ([]float
waitGroup.Done()
}
// avoid too many goroutines by ants pool
pool := conc.NewDefaultPool[any]()
defer pool.Release()
pool := GetCalcPool()
for i := int64(0); i < leftNum; i++ {
waitGroup.Add(1)
index := i
Expand Down
31 changes: 31 additions & 0 deletions pkg/util/distance/pool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Licensed to the LF AI & Data foundation under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package distance

var (
calcPool *conc.Pool[any]
calcPoolInitOnce = new(sync.Once)
)

func initCalcPool() {
calcPool = conc.NewDefaultPool[any]()
}

func GetCalcPool() *conc.Pool[any] {
calcPoolInitOnce.Do(initCalcPool)
return calcPool
}

0 comments on commit 1101219

Please sign in to comment.