Skip to content

Commit a055de0

Browse files
committed
The algorithm for distributing tasks by goroutines has been updated
1 parent bbe704c commit a055de0

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

fn.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ var (
1313

1414
// The maxParallelTasks is the maximum number of parallel tasks.
1515
maxParallelTasks = runtime.NumCPU() * 3
16+
17+
// The minLoadPerGoroutine is the minimum slice size for processing
18+
// in an individual goroutine. Essentially, it delineates the threshold
19+
// at which it becomes worthwhile to divide the slice processing amongst
20+
// multiple goroutines. If each goroutine isn't handling a sufficiently
21+
// large subslice, the overhead of goroutine creation and management
22+
// may outweigh the benefits of concurrent processing. This variable
23+
// specifies the minimum number of iterations per goroutine to ensure
24+
// an efficient division of labor.
25+
minLoadPeGoroutine = 1024
1626
)
1727

1828
// Logicable is a special data type from which to determine the state of Trit
@@ -215,11 +225,11 @@ func All[T Logicable](t ...T) Trit {
215225
found := &logicFoundValue{value: True}
216226

217227
// If the length of the slice is less than or equal to
218-
// the number of parallel tasks, then we do not need
228+
// the minLoadPeGoroutine, then we do not need
219229
// to use goroutines.
220230
if l := len(t); l == 0 {
221231
return False
222-
} else if l < p*2 {
232+
} else if l/p < minLoadPeGoroutine {
223233
for _, v := range t {
224234
trit := logicToTrit(v)
225235
if trit.IsFalse() || trit.IsUnknown() {
@@ -283,11 +293,11 @@ func Any[T Logicable](t ...T) Trit {
283293
found := &logicFoundValue{value: False}
284294

285295
// If the length of the slice is less than or equal to
286-
// the number of parallel tasks, then we do not need
296+
// the minLoadPeGoroutine, then we do not need
287297
// to use goroutines.
288298
if l := len(t); l == 0 {
289299
return False
290-
} else if l < p*2 {
300+
} else if l/p < minLoadPeGoroutine {
291301
for _, v := range t {
292302
trit := logicToTrit(v)
293303
if trit.IsTrue() {
@@ -529,11 +539,11 @@ func Known[T Logicable](ts ...T) Trit {
529539
found := &logicFoundValue{value: True}
530540

531541
// If the length of the slice is less than or equal to
532-
// the number of parallel tasks, then we do not need
542+
// the minLoadPeGoroutine, then we do not need
533543
// to use goroutines.
534544
if l := len(ts); l == 0 {
535545
return False
536-
} else if l < p*2 {
546+
} else if l/p < minLoadPeGoroutine {
537547
for _, t := range ts {
538548
trit := logicToTrit(t)
539549
if trit == Unknown {

fn_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ func TestConvert(t *testing.T) {
167167
// TestAll tests the All function.
168168
func TestAll(t *testing.T) {
169169
ParallelTasks(2)
170+
minLoadPeGoroutine = 20
170171
tests := []struct {
171172
name string
172173
in []Trit
@@ -217,6 +218,7 @@ func TestAll(t *testing.T) {
217218
// TestAny tests the Any function.
218219
func TestAny(t *testing.T) {
219220
ParallelTasks(2)
221+
minLoadPeGoroutine = 20
220222
tests := []struct {
221223
name string
222224
in []Trit
@@ -893,6 +895,7 @@ func TestNeq(t *testing.T) {
893895
// TestKnown tests the Known function.
894896
func TestKnown(t *testing.T) {
895897
ParallelTasks(2)
898+
minLoadPeGoroutine = 20
896899
tests := []struct {
897900
name string
898901
in []Trit

0 commit comments

Comments
 (0)