-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
170 lines (159 loc) · 2.98 KB
/
main.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
package main
import (
"bytes"
"fmt"
"io"
"os"
"runtime"
"slices"
)
type entry struct {
count int
total int
min int
max int
name []byte
}
type result struct {
entries map[string]entry
buffer []byte
}
var results chan result
func main() {
f, err := os.Open(`../1brc/data/full.txt`)
// f, err := os.Open(`../1brc/data/test.txt`)
if err != nil {
panic(err)
}
c := runtime.NumCPU() / 2
run(c, 64*1024*1024, f)
}
func run(c, size int, f io.Reader) {
var r result
var ok bool
var fe entry
results = make(chan result, c)
for i := 0; i < c; i++ {
results <- result{
map[string]entry{},
make([]byte, size),
}
}
read(f)
final := map[string]entry{}
for i := 0; i < c; i++ {
select {
case r = <-results:
for name, e := range r.entries {
fe, ok = final[name]
if !ok {
fe.name = []byte(name)
fe.max = -1e6
fe.min = 1e6
}
fe.total += e.total
fe.count += e.count
if e.max > fe.max {
fe.max = e.max
}
if e.min < fe.min {
fe.min = e.min
}
final[name] = fe
}
}
}
var entries []entry
for _, e := range final {
entries = append(entries, e)
}
slices.SortFunc(entries, func(a, b entry) int {
return bytes.Compare(a.name, b.name)
})
fmt.Print(`{`)
for i, e := range entries {
if bytes.HasPrefix(e.name, []byte(`\n`)) {
continue
}
fmt.Printf(`%s=%.1f/%.1f/%.1f`, e.name, float64(e.min)/10, float64(e.total)/float64(e.count)/10, float64(e.max)/10)
if i < len(entries)-1 {
fmt.Print(`, `)
}
}
fmt.Print(`}`)
}
func read(f io.Reader) {
var r result
var n int
var off int
var rem = make([]byte, 100)
var err error
for {
select {
case r = <-results:
copy(r.buffer[:off], rem[:off])
n, err = f.Read(r.buffer[off:cap(r.buffer)])
n += off
off = n - bytes.LastIndexByte(r.buffer[:n], '\n')
copy(rem[:off], r.buffer[n-off:n])
r.buffer = r.buffer[:n-off]
go doPart(r)
if err == io.EOF {
return
}
}
}
}
func doPart(r result) {
var i int
var j int
var k int
var temp int
var name string
var e entry
var ok bool
for i < len(r.buffer) {
j = bytes.IndexByte(r.buffer[i:], ';') + i
name = string(r.buffer[i:j])
e, ok = r.entries[name]
if !ok {
e.max = -1e6
e.min = 1e6
}
k = bytes.IndexByte(r.buffer[i:], '\n') + i
if j > k {
k = len(r.buffer)
}
temp = customStringToIntParser(r.buffer[j+1 : k])
e.count++
e.total += temp
if temp < e.min {
e.min = temp
}
if temp > e.max {
e.max = temp
}
r.entries[name] = e
i = k + 1
}
results <- r
return
}
// https://github.com/shraddhaag/1brc/blob/17d575fd0f143aed18d285713d030a5b52b478df/main.go#L231
func customStringToIntParser(input []byte) (output int) {
var isNegativeNumber bool
if input[0] == '-' {
isNegativeNumber = true
input = input[1:]
}
switch len(input) {
case 3:
output = int(input[0])*10 + int(input[2]) - int('0')*11
case 4:
output = int(input[0])*100 + int(input[1])*10 + int(input[3]) - (int('0') * 111)
}
if isNegativeNumber {
return -output
}
return
}