Some more optimizations. Also I realized that print uses stderr instead of stdout, fixed that.
This commit is contained in:
parent
d481fa8dfe
commit
ef3c887b72
31
main.go
31
main.go
@ -30,9 +30,9 @@ func main() {
|
||||
|
||||
// Make a channel and a thread for each CPU core
|
||||
var cores = runtime.NumCPU()
|
||||
var linechan = make(chan []byte)
|
||||
var workchannel = make(chan []byte)
|
||||
for i := 0; i < cores; i++ {
|
||||
go scannerThread(linechan, []byte(needle), &wg)
|
||||
go scannerThread(workchannel, []byte(needle), &wg)
|
||||
wg.Add(1)
|
||||
}
|
||||
|
||||
@ -54,37 +54,36 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
linechan <- append(buf[:nread], remainder...)
|
||||
workchannel <- append(buf[:nread], remainder...)
|
||||
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
close(linechan)
|
||||
|
||||
close(workchannel)
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func scannerThread(linechannel chan []byte, needle []byte, wg *sync.WaitGroup) {
|
||||
var line []byte
|
||||
func scannerThread(workchannel chan []byte, needle []byte, wg *sync.WaitGroup) {
|
||||
var batch []byte
|
||||
var ok bool
|
||||
var start, end int
|
||||
var linelen int
|
||||
var i int
|
||||
defer wg.Done()
|
||||
for {
|
||||
line, ok = <-linechannel
|
||||
batch, ok = <-workchannel
|
||||
if !ok {
|
||||
return // channel closed. we're done
|
||||
}
|
||||
|
||||
// This loop is for every result found, when there are no more results it stops
|
||||
for i = bytes.Index(line, needle); i != -1; i = bytes.Index(line, needle) {
|
||||
for i = bytes.Index(batch, needle); i != -1; i = bytes.Index(batch, needle) {
|
||||
start, end = i, i
|
||||
// needle was found, but where?
|
||||
for { // find the start
|
||||
if line[start] == lf {
|
||||
if batch[start] == lf {
|
||||
start++ // the line feed is from the previous line, so skip it
|
||||
break
|
||||
} else if start == 0 {
|
||||
@ -94,18 +93,22 @@ func scannerThread(linechannel chan []byte, needle []byte, wg *sync.WaitGroup) {
|
||||
}
|
||||
|
||||
for { // find the end
|
||||
if line[end] == lf || end == linelen-1 {
|
||||
if batch[end] == lf {
|
||||
end++ // include the line feed in the line
|
||||
// https://stackoverflow.com/questions/26857582/in-a-go-slice-why-does-slohi-end-at-element-hi-1
|
||||
break
|
||||
} else if end == linelen {
|
||||
break
|
||||
}
|
||||
end++
|
||||
}
|
||||
|
||||
print(string(line[start:end]) + "\n")
|
||||
os.Stdout.Write(batch[start:end])
|
||||
|
||||
// Chop all of the bytes before the result off so it doesn't get
|
||||
// searched again
|
||||
line = line[end:]
|
||||
linelen = len(line)
|
||||
batch = batch[end:]
|
||||
linelen = len(batch)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user