@@ -212,6 +212,22 @@ func SetMaxProcs(max int) {
212212 maxProcs = max
213213}
214214
215+ func numThreads (maxThreads int ) int {
216+ numThreads := maxProcs
217+
218+ // take into consideration the possility that application reduced
219+ // GOMAXPROCS after |maxProcs| was initialized
220+ numProcs := runtime .GOMAXPROCS (0 )
221+ if maxProcs > numProcs {
222+ numThreads = numProcs
223+ }
224+
225+ if maxThreads > 0 && numThreads > maxThreads {
226+ return maxThreads
227+ }
228+ return numThreads
229+ }
230+
215231var cgo_pairingSizeOf = C .blst_pairing_sizeof ()
216232var cgo_p1Generator = * C .blst_p1_generator ()
217233var cgo_p2Generator = * C .blst_p2_generator ()
@@ -633,13 +649,8 @@ func coreAggregateVerifyPkInG1(sigFn sigGetterP2, sigGroupcheck bool,
633649 }
634650
635651 numCores := runtime .GOMAXPROCS (0 )
636- numThreads := maxProcs
637- if numThreads > numCores {
638- numThreads = numCores
639- }
640- if numThreads > n {
641- numThreads = n
642- }
652+ numThreads := numThreads (n )
653+
643654 // Each thread will determine next message to process by atomically
644655 // incrementing curItem, process corresponding pk,msg[,aug] tuple and
645656 // repeat until n is exceeded. The resulting accumulations will be
@@ -830,14 +841,8 @@ func multipleAggregateVerifyPkInG1(paramsFn mulAggGetterPkInG1,
830841 useHash = optional [0 ]
831842 }
832843
833- numCores := runtime .GOMAXPROCS (0 )
834- numThreads := maxProcs
835- if numThreads > numCores {
836- numThreads = numCores
837- }
838- if numThreads > n {
839- numThreads = n
840- }
844+ numThreads := numThreads (n )
845+
841846 // Each thread will determine next message to process by atomically
842847 // incrementing curItem, process corresponding pk,msg[,aug] tuple and
843848 // repeat until n is exceeded. The resulting accumulations will be
@@ -1235,13 +1240,8 @@ func coreAggregateVerifyPkInG2(sigFn sigGetterP1, sigGroupcheck bool,
12351240 }
12361241
12371242 numCores := runtime .GOMAXPROCS (0 )
1238- numThreads := maxProcs
1239- if numThreads > numCores {
1240- numThreads = numCores
1241- }
1242- if numThreads > n {
1243- numThreads = n
1244- }
1243+ numThreads := numThreads (n )
1244+
12451245 // Each thread will determine next message to process by atomically
12461246 // incrementing curItem, process corresponding pk,msg[,aug] tuple and
12471247 // repeat until n is exceeded. The resulting accumulations will be
@@ -1432,14 +1432,8 @@ func multipleAggregateVerifyPkInG2(paramsFn mulAggGetterPkInG2,
14321432 useHash = optional [0 ]
14331433 }
14341434
1435- numCores := runtime .GOMAXPROCS (0 )
1436- numThreads := maxProcs
1437- if numThreads > numCores {
1438- numThreads = numCores
1439- }
1440- if numThreads > n {
1441- numThreads = n
1442- }
1435+ numThreads := numThreads (n )
1436+
14431437 // Each thread will determine next message to process by atomically
14441438 // incrementing curItem, process corresponding pk,msg[,aug] tuple and
14451439 // repeat until n is exceeded. The resulting accumulations will be
@@ -1742,14 +1736,8 @@ func (_ *P1Affine) BatchUncompress(in [][]byte) []*P1Affine {
17421736 points := make ([]P1Affine , n )
17431737 pointsPtrs := make ([]* P1Affine , n )
17441738
1745- numCores := runtime .GOMAXPROCS (0 )
1746- numThreads := maxProcs
1747- if numThreads > numCores {
1748- numThreads = numCores
1749- }
1750- if numThreads > n {
1751- numThreads = n
1752- }
1739+ numThreads := numThreads (n )
1740+
17531741 // Each thread will determine next message to process by atomically
17541742 // incrementing curItem, process corresponding point, and
17551743 // repeat until n is exceeded. Each thread will send a result (true for
@@ -2122,11 +2110,7 @@ func P1AffinesMult(pointsIf interface{}, scalarsIf interface{}, nbits int) *P1 {
21222110 panic (fmt .Sprintf ("unsupported type %T" , val ))
21232111 }
21242112
2125- numThreads := maxProcs
2126- numCores := runtime .GOMAXPROCS (0 )
2127- if numCores < maxProcs {
2128- numThreads = numCores
2129- }
2113+ numThreads := numThreads (0 )
21302114
21312115 if numThreads < 2 || npoints < 32 {
21322116 sz := int (C .blst_p1s_mult_pippenger_scratch_sizeof (C .size_t (npoints ))) / 8
@@ -2338,14 +2322,7 @@ func P1AffinesValidate(pointsIf interface{}) bool {
23382322 panic (fmt .Sprintf ("unsupported type %T" , val ))
23392323 }
23402324
2341- numCores := runtime .GOMAXPROCS (0 )
2342- numThreads := maxProcs
2343- if numThreads > numCores {
2344- numThreads = numCores
2345- }
2346- if numThreads > npoints {
2347- numThreads = npoints
2348- }
2325+ numThreads := numThreads (npoints )
23492326
23502327 if numThreads < 2 {
23512328 for i := 0 ; i < npoints ; i ++ {
@@ -2499,14 +2476,8 @@ func (_ *P2Affine) BatchUncompress(in [][]byte) []*P2Affine {
24992476 points := make ([]P2Affine , n )
25002477 pointsPtrs := make ([]* P2Affine , n )
25012478
2502- numCores := runtime .GOMAXPROCS (0 )
2503- numThreads := maxProcs
2504- if numThreads > numCores {
2505- numThreads = numCores
2506- }
2507- if numThreads > n {
2508- numThreads = n
2509- }
2479+ numThreads := numThreads (n )
2480+
25102481 // Each thread will determine next message to process by atomically
25112482 // incrementing curItem, process corresponding point, and
25122483 // repeat until n is exceeded. Each thread will send a result (true for
@@ -2879,11 +2850,7 @@ func P2AffinesMult(pointsIf interface{}, scalarsIf interface{}, nbits int) *P2 {
28792850 panic (fmt .Sprintf ("unsupported type %T" , val ))
28802851 }
28812852
2882- numThreads := maxProcs
2883- numCores := runtime .GOMAXPROCS (0 )
2884- if numCores < maxProcs {
2885- numThreads = numCores
2886- }
2853+ numThreads := numThreads (0 )
28872854
28882855 if numThreads < 2 || npoints < 32 {
28892856 sz := int (C .blst_p2s_mult_pippenger_scratch_sizeof (C .size_t (npoints ))) / 8
@@ -3095,14 +3062,7 @@ func P2AffinesValidate(pointsIf interface{}) bool {
30953062 panic (fmt .Sprintf ("unsupported type %T" , val ))
30963063 }
30973064
3098- numCores := runtime .GOMAXPROCS (0 )
3099- numThreads := maxProcs
3100- if numThreads > numCores {
3101- numThreads = numCores
3102- }
3103- if numThreads > npoints {
3104- numThreads = npoints
3105- }
3065+ numThreads := numThreads (npoints )
31063066
31073067 if numThreads < 2 {
31083068 for i := 0 ; i < npoints ; i ++ {
0 commit comments