Use a lock and channel free parallelization for subseq scoring

This commit is contained in:
Kovid Goyal
2025-06-06 06:39:09 +05:30
parent 3feea5b279
commit 3059c87bd0
2 changed files with 42 additions and 12 deletions

View File

@@ -12,6 +12,7 @@ import (
"slices"
"strconv"
"strings"
"sync"
"golang.org/x/exp/constraints"
)
@@ -86,6 +87,42 @@ func Format_stacktrace_on_panic(r any) (text string, err error) {
return strings.TrimSpace(text), err
}
// Run the specified function in parallel over chunks from the specified range.
// If the function panics, it is turned into a regular error.
func Run_in_parallel_over_range(num_procs int, f func(int, int) error, start, limit int) (err error) {
num_items := limit - start
if num_procs <= 0 {
num_procs = runtime.NumCPU()
}
num_procs = max(1, min(num_procs, num_items))
chunk_sz := max(1, num_items/num_procs)
var wg sync.WaitGroup
echan := make(chan error, num_procs)
for start < limit {
end := min(start+chunk_sz, limit)
wg.Add(1)
go func(start, end int) {
defer func() {
if r := recover(); r != nil {
stacktrace, e := Format_stacktrace_on_panic(r)
echan <- fmt.Errorf("%s\n\n%w", stacktrace, e)
}
wg.Done()
}()
if err := f(start, end); err != nil {
echan <- err
}
}(start, end)
start = end
}
wg.Wait()
close(echan)
for qerr := range echan {
return qerr
}
return
}
func Map[T any, O any](f func(x T) O, s []T) []O {
ans := make([]O, 0, len(s))
for _, x := range s {