forked from benhoyt/countwords
-
Notifications
You must be signed in to change notification settings - Fork 1
/
parallel.go
465 lines (409 loc) · 10.2 KB
/
parallel.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
package main
import (
"bufio"
"flag"
"fmt"
"io"
"log"
"os"
"runtime"
"runtime/pprof"
"runtime/trace"
"sort"
"unicode"
"unicode/utf8"
)
var (
procFactor = 1.0
bufSize int = 64 * 1024
minChunkSize int = 64 * 1024
tokenSplit = bufio.ScanWords
)
func main() {
cpuprofile := flag.String("cpuprofile", "", "write cpu profile to `file`")
memprofile := flag.String("memprofile", "", "write memory profile to `file`")
tracefile := flag.String("trace", "", "enable runtime tracing")
flag.IntVar(&bufSize, "buf-size", bufSize, "scan buffer size")
flag.IntVar(&minChunkSize, "min-chunk-size", minChunkSize, "minimum chunk size for concurrent input splitting")
flag.Float64Var(&procFactor, "proc-factor", procFactor, "processor over-scheduling factor; set to 0 to disable concurrent processing")
flag.Parse()
if *tracefile != "" {
f, err := os.Create(*tracefile)
if err == nil {
defer f.Close()
err = trace.Start(f)
}
if err != nil {
log.Fatalln("unable to start trace", err)
}
defer trace.Stop()
}
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal("could not create CPU profile: ", err)
}
defer f.Close() // error handling omitted for example
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatal("could not start CPU profile: ", err)
}
defer pprof.StopCPUProfile()
}
if err := errMain(); err != nil {
log.Fatal(err)
}
if *memprofile != "" {
f, err := os.Create(*memprofile)
if err != nil {
log.Fatal("could not create memory profile: ", err)
}
defer f.Close()
runtime.GC() // get up-to-date statistics
if err := pprof.WriteHeapProfile(f); err != nil {
log.Fatal("could not write memory profile: ", err)
}
}
}
func errMain() error {
var (
in = os.Stdin
out = os.Stdout
numProcs = runtime.GOMAXPROCS(-1)
numChunks = int(float64(numProcs) * procFactor)
)
// precompute token-aligned chunk boundaries
var chunks []chunk
if procFactor > 0 {
var err error
chunks, err = splitChunks(numChunks, minChunkSize, in, tokenSplit)
// try to fixup (things like illegal seek error) by soaking into a temp file and re-chunking
// TODO would be better if we had a portable form of errors.Is(err, syscall.EILSEQ)
if err != nil {
var tmp *os.File
tmp, err = os.CreateTemp("", "")
if err == nil {
defer os.Remove(tmp.Name())
if _, err = io.Copy(tmp, in); err == nil {
in.Close()
in = tmp
if _, err = in.Seek(0, os.SEEK_SET); err == nil {
chunks, err = splitChunks(numChunks, minChunkSize, in, tokenSplit)
}
}
}
}
if err != nil {
return err
}
}
// run serially if input not large enough or forced
if len(chunks) <= 1 {
counts, err := countTokens(in, tokenSplit)
if err == nil {
err = counts.report(out)
}
return err
}
// error stream from counter goroutines to parent watcher
errch := make(chan error, len(chunks))
// result channels form a binary tree, with each counter goroutine merging
// from up to two downstream children before sending its result upstream
results := make(map[int]chan result, len(chunks))
for i := range chunks {
results[i] = make(chan result, 1)
}
for i, c := range chunks {
go func(
chunk chunk,
resto chan<- result,
errto chan<- error,
// NOTE this isn't variadic because no dynamic select (reflect hack isn't worth it imo)
left, right <-chan result,
) {
defer close(resto)
// scan and count tokens within our chunk
counts, err := countTokens(chunk.sectionReader(in), tokenSplit)
if err != nil {
errto <- err
return
}
// wait for and merge results from both children
for left != nil || right != nil {
select {
case other, ok := <-left:
if !ok {
left = nil
} else {
counts.merge(other)
}
case other, ok := <-right:
if !ok {
right = nil
} else {
counts.merge(other)
}
}
}
resto <- counts
}(c, results[i], errch, results[2*i+1], results[2*i+2])
}
final := results[0]
// wait for error or final result
for final != nil || errch != nil {
select {
case err, ok := <-errch:
if !ok {
errch = nil
} else if err != nil {
return err
}
case counts := <-final:
final = nil
close(errch)
if err := counts.report(out); err != nil {
return err
}
}
}
return nil
}
type result map[string]*int
// countTokens is the core token scanning and counting routine
func countTokens(in io.Reader, split bufio.SplitFunc) (result, error) {
scanner := bufio.NewScanner(in)
scanner.Buffer(make([]byte, bufSize), bufSize)
scanner.Split(split)
counts := make(result)
var word []byte
for scanner.Scan() {
word = toLowerInto(scanner.Bytes(), word)
counts.count(word)
}
return counts, scanner.Err()
}
func (counts result) count(s []byte) {
if c := counts[string(s)]; c != nil {
*c++
} else {
n := 1
counts[string(s)] = &n
}
}
// report writes sorted counts to an output writer
func (counts result) report(out io.Writer) error {
type Count struct {
Word string
Count int
}
ordered := make([]Count, 0, len(counts))
for word, count := range counts {
ordered = append(ordered, Count{word, *count})
}
sort.Slice(ordered, func(i, j int) bool {
return ordered[i].Count > ordered[j].Count
})
for _, count := range ordered {
if _, err := fmt.Fprintf(out, "%v %v\n", count.Word, count.Count); err != nil {
return err
}
}
return nil
}
// merge another set of counts
// NOTE the other counts must no longer be used after this call
func (counts result) merge(other result) {
for word, count := range other {
if c := counts[word]; c != nil {
*c += *count
} else {
counts[word] = count
}
}
}
type chunk struct {
off int64
end int64
}
func (c chunk) size() int64 {
return c.end - c.off
}
func (c chunk) sectionReader(in io.ReaderAt) *io.SectionReader {
return io.NewSectionReader(in, c.off, c.size())
}
// splitChunks makes a best effort to generate numChunks ranges of at least
// minChunks size within the byte space of a random access reader.
// Chunks will always end at a valid token boundary as defined by split.
// Performs small reads to find token boundaries near chunk edges.
func splitChunks(numChunks, minSize int, in io.ReaderAt, split bufio.SplitFunc) ([]chunk, error) {
if numChunks < 2 {
return nil, nil
}
size, err := readerSize(in)
if err != nil {
return nil, err
}
if size == 0 {
return nil, fmt.Errorf("unable to determine the size of %T ReaderAt", in)
}
const (
minPeek = 64
maxAllow = minPeek * 8
)
if minSize < maxAllow {
minSize = maxAllow
}
chunkSize := size/int64(numChunks) + maxAllow
if minSize > 0 && chunkSize < int64(minSize) {
chunkSize = int64(minSize)
}
chunks := make([]chunk, 0, numChunks)
// NOTE extreme upper sanity bound should never be hit, since we should
// break because EOF long before 2x chunks
var off int64 = 0
var buf []byte
for sanity := numChunks * 2; sanity > 0; sanity-- {
// compute chunk end at next token boundary
end := off + chunkSize
var (
peek []byte
err error
)
for sz := int64(minPeek); peek == nil && err == nil; sz *= 2 {
if cap(buf) < int(sz) {
buf = make([]byte, int(sz))
}
if end -= sz; end < off {
end = off
}
peek, err = readChunk(buf[:sz], in, end, split)
}
if err == io.EOF {
end = size
} else if err != nil {
return nil, err
} else {
end += int64(len(peek))
}
chunks = append(chunks, chunk{off, end})
// keep splitting until EOF
if err == io.EOF {
break
}
off = end
}
return chunks, nil
}
// readChunk tries to read a token-aligned chunk of input from a given offset
// within a random access input into buf. If the split function declines to
// tokenize, returns nil and any split or read error. When successful, returns
// a non-empty sub-slice of buf that contains a usable chunk, along with io.EOF
// if the underlying ReadAt returned EOF.
//
// Uses a from of binary search to call split order-of-log(size) times,
// attempting to maximize the returned chunk size.
func readChunk(buf []byte, in io.ReaderAt, off int64, split bufio.SplitFunc) ([]byte, error) {
n, rerr := in.ReadAt(buf, off)
atEOF := rerr == io.EOF
if !atEOF && rerr != nil {
return nil, rerr
}
chunk := buf[:n]
lo, _, err := split(chunk, atEOF)
if err != nil {
return nil, err
} else if lo == 0 {
return nil, rerr
}
hi := len(chunk)
for lo < hi {
mid := lo/2 + hi/2
adv, _, err := split(chunk[mid:], atEOF)
if err != nil {
return nil, err
}
if adv > 0 {
lo = mid + adv
} else {
hi = mid
}
}
return chunk[:lo], rerr
}
// readerSize tries to determine the size of a random access reader, supporting
// in-memory implementations (like strings.Reader) and stat-able
// implementations like *os.File.
func readerSize(ra io.ReaderAt) (int64, error) {
type sizer interface {
Size() int64
}
if sz, ok := ra.(sizer); ok {
return sz.Size(), nil
}
type stater interface {
Stat() (os.FileInfo, error)
}
if st, ok := ra.(stater); ok {
info, err := st.Stat()
if err != nil {
return 0, err
}
return info.Size(), nil
}
return 0, nil
}
// toLowerInto is a copy of bytes.ToLower and an inlined/specialized copy of
// bytes.Map that works withing a reusable byte slice.
func toLowerInto(s, b []byte) []byte {
isASCII, hasUpper := true, false
for i := 0; i < len(s); i++ {
c := s[i]
if c >= utf8.RuneSelf {
isASCII = false
break
}
hasUpper = hasUpper || ('A' <= c && c <= 'Z')
}
b = b[:cap(b)]
if len(b) < len(s) {
b = make([]byte, len(s))
}
if isASCII { // optimize for ASCII-only byte slices.
if !hasUpper {
return append(b[:0], s...)
}
i := 0
for ; i < len(s); i++ {
c := s[i]
if 'A' <= c && c <= 'Z' {
c += 'a' - 'A'
}
b[i] = c
}
return b[:i]
}
nbytes := 0 // number of bytes encoded in b
for i := 0; i < len(s); {
wid := 1
r := rune(s[i])
if r >= utf8.RuneSelf {
r, wid = utf8.DecodeRune(s[i:])
}
r = unicode.ToLower(r)
if r >= 0 {
rl := utf8.RuneLen(r)
if rl < 0 {
rl = len(string(utf8.RuneError))
}
if max := len(b); nbytes+rl > max {
// Grow the buffer.
max = max*2 + utf8.UTFMax
nb := make([]byte, max)
copy(nb, b[0:nbytes])
b = nb
}
nbytes += utf8.EncodeRune(b[nbytes:], r)
}
i += wid
}
return b[0:nbytes]
}