Skip to content

Commit

Permalink
Optimize DetectBest execution (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
moredure authored Nov 20, 2021
1 parent ac37679 commit b7413ea
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
19 changes: 15 additions & 4 deletions detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,22 @@ var (

// DetectBest returns the Result with highest Confidence.
func (d *Detector) DetectBest(b []byte) (r *Result, err error) {
var all []Result
if all, err = d.DetectAll(b); err == nil {
r = &all[0]
input := newRecognizerInput(b, d.stripTag)
outputChan := make(chan recognizerOutput)
for _, r := range d.recognizers {
go matchHelper(r, input, outputChan)
}
var output Result
for i := 0; i < len(d.recognizers); i++ {
o := <-outputChan
if output.Confidence < o.Confidence {
output = Result(o)
}
}
if output.Confidence == 0 {
return nil, NotDetectedError
}
return
return &output, nil
}

// DetectAll returns all Results which have non-zero Confidence. The Results are sorted by Confidence in descending order.
Expand Down
10 changes: 10 additions & 0 deletions detector_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package chardet_test

import (
"bytes"
"github.com/gogs/chardet"
"io"
"os"
Expand Down Expand Up @@ -58,3 +59,12 @@ func TestDetector(t *testing.T) {
}
}
}

func BenchmarkDetectBest(b *testing.B) {
textDetector := chardet.NewTextDetector()
aaaa := bytes.Repeat([]byte("A"), 1024)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
textDetector.DetectBest(aaaa)
}
}

0 comments on commit b7413ea

Please sign in to comment.