detached ce28c36

fix(hints): cap OCR processes per scan

commit
ce28c3608856048f341730bb6e56b14f5be52534
author
brice · · 2026-10-19 15:28 UTC
parent
71feaa7
signature
GPG VerifiedCD88F9E564E0102646FB6077A1A07C7C7C3E349D · arch-gpg Verified for the committer email
2 files · +58 -1
M internal/adapters/hints/ocr.go +13 -1
@@ -154,12 +154,24 @@ func bandsOf(height int) []band {
154154 func (w *worker) recognize(ctx context.Context, jobs []*bandJob) {
155155 ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
156156 defer cancel()
157+ // One scan-wide pool: at most ocrWorkers tesseract processes run at once,
158+ // whatever the number of windows.
159+ sem := make(chan struct{}, max(ocrWorkers, 1))
157160 var wg sync.WaitGroup
158161 for _, j := range jobs {
159162 if j.hit {
160163 continue
161164 }
162- wg.Go(func() { j.words, j.err = readBand(ctx, &w.ocr, j) })
165+ select {
166+ case sem <- struct{}{}:
167+ case <-ctx.Done():
168+ j.err = ctx.Err()
169+ continue
170+ }
171+ wg.Go(func() {
172+ defer func() { <-sem }()
173+ j.words, j.err = readBand(ctx, &w.ocr, j)
174+ })
163175 }
164176 wg.Wait()
165177 }
@@ -154,12 +154,24 @@ func bandsOf(height int) []band {
154 func (w *worker) recognize(ctx context.Context, jobs []*bandJob) {154 func (w *worker) recognize(ctx context.Context, jobs []*bandJob) {
155 ctx, cancel := context.WithTimeout(ctx, 15*time.Second)155 ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
156 defer cancel()156 defer cancel()
157+ // One scan-wide pool: at most ocrWorkers tesseract processes run at once,
158+ // whatever the number of windows.
159+ sem := make(chan struct{}, max(ocrWorkers, 1))
157 var wg sync.WaitGroup160 var wg sync.WaitGroup
158 for _, j := range jobs {161 for _, j := range jobs {
159 if j.hit {162 if j.hit {
160 continue163 continue
161 }164 }
162- wg.Go(func() { j.words, j.err = readBand(ctx, &w.ocr, j) })165+ select {
166+ case sem <- struct{}{}:
167+ case <-ctx.Done():
168+ j.err = ctx.Err()
169+ continue
170+ }
171+ wg.Go(func() {
172+ defer func() { <-sem }()
173+ j.words, j.err = readBand(ctx, &w.ocr, j)
174+ })
163 }175 }
164 wg.Wait()176 wg.Wait()
165 }177 }
M internal/adapters/hints/ocr_test.go +45 -0
@@ -8,6 +8,7 @@ import (
88 "io"
99 "os"
1010 "os/exec"
11+ "path/filepath"
1112 "reflect"
1213 "strings"
1314 "testing"
@@ -191,3 +192,47 @@ func TestPGMMatchesPNG(t *testing.T) {
191192 t.Fatalf("PNG %d words, PGM %d words", len(res[0]), len(res[1]))
192193 }
193194 }
195+
196+// TestRecognizeCapsProcesses runs the bands of several windows through a
197+// stand-in tesseract that logs when each process starts and ends.
198+func TestRecognizeCapsProcesses(t *testing.T) {
199+ dir := t.TempDir()
200+ logPath := filepath.Join(dir, "log")
201+ script := filepath.Join(dir, "tesseract")
202+ body := "#!/bin/sh\ncat >/dev/null\necho s >> " + logPath + "\nsleep 0.1\necho e >> " + logPath + "\nprintf 'level\\tpage_num\\tblock_num\\tpar_num\\tline_num\\tword_num\\tleft\\ttop\\twidth\\theight\\tconf\\ttext\\n'\n"
203+ if err := os.WriteFile(script, []byte(body), 0o700); err != nil {
204+ t.Fatal(err)
205+ }
206+ old := ocrWorkers
207+ ocrWorkers = 2
208+ t.Cleanup(func() { ocrWorkers = old })
209+ w := &worker{ocr: ocrState{done: true, binary: script, lang: "eng"}}
210+ var jobs []*bandJob
211+ for range 8 { // several windows' bands
212+ g := image.NewGray(image.Rect(0, 0, 8, 8))
213+ jobs = append(jobs, &bandJob{ext: g.Rect, own: [2]int{0, 8}, gray: g})
214+ }
215+ w.recognize(context.Background(), jobs)
216+ data, err := os.ReadFile(logPath)
217+ if err != nil {
218+ t.Fatal(err)
219+ }
220+ cur, peak, starts := 0, 0, 0
221+ for _, l := range strings.Fields(string(data)) {
222+ if l == "s" {
223+ cur++
224+ starts++
225+ peak = max(peak, cur)
226+ } else {
227+ cur--
228+ }
229+ }
230+ if starts != 8 || peak > 2 {
231+ t.Fatalf("starts=%d peak=%d", starts, peak)
232+ }
233+ for _, j := range jobs {
234+ if j.err != nil {
235+ t.Fatal(j.err)
236+ }
237+ }
238+}
@@ -8,6 +8,7 @@ import (
8 "io"8 "io"
9 "os"9 "os"
10 "os/exec"10 "os/exec"
11+ "path/filepath"
11 "reflect"12 "reflect"
12 "strings"13 "strings"
13 "testing"14 "testing"
@@ -191,3 +192,47 @@ func TestPGMMatchesPNG(t *testing.T) {
191 t.Fatalf("PNG %d words, PGM %d words", len(res[0]), len(res[1]))192 t.Fatalf("PNG %d words, PGM %d words", len(res[0]), len(res[1]))
192 }193 }
193 }194 }
195+
196+// TestRecognizeCapsProcesses runs the bands of several windows through a
197+// stand-in tesseract that logs when each process starts and ends.
198+func TestRecognizeCapsProcesses(t *testing.T) {
199+ dir := t.TempDir()
200+ logPath := filepath.Join(dir, "log")
201+ script := filepath.Join(dir, "tesseract")
202+ body := "#!/bin/sh\ncat >/dev/null\necho s >> " + logPath + "\nsleep 0.1\necho e >> " + logPath + "\nprintf 'level\\tpage_num\\tblock_num\\tpar_num\\tline_num\\tword_num\\tleft\\ttop\\twidth\\theight\\tconf\\ttext\\n'\n"
203+ if err := os.WriteFile(script, []byte(body), 0o700); err != nil {
204+ t.Fatal(err)
205+ }
206+ old := ocrWorkers
207+ ocrWorkers = 2
208+ t.Cleanup(func() { ocrWorkers = old })
209+ w := &worker{ocr: ocrState{done: true, binary: script, lang: "eng"}}
210+ var jobs []*bandJob
211+ for range 8 { // several windows' bands
212+ g := image.NewGray(image.Rect(0, 0, 8, 8))
213+ jobs = append(jobs, &bandJob{ext: g.Rect, own: [2]int{0, 8}, gray: g})
214+ }
215+ w.recognize(context.Background(), jobs)
216+ data, err := os.ReadFile(logPath)
217+ if err != nil {
218+ t.Fatal(err)
219+ }
220+ cur, peak, starts := 0, 0, 0
221+ for _, l := range strings.Fields(string(data)) {
222+ if l == "s" {
223+ cur++
224+ starts++
225+ peak = max(peak, cur)
226+ } else {
227+ cur--
228+ }
229+ }
230+ if starts != 8 || peak > 2 {
231+ t.Fatalf("starts=%d peak=%d", starts, peak)
232+ }
233+ for _, j := range jobs {
234+ if j.err != nil {
235+ t.Fatal(j.err)
236+ }
237+ }
238+}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 25

Tags 10

↑↓ moveenter openesc close