-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwriter.go
More file actions
300 lines (260 loc) · 8.35 KB
/
writer.go
File metadata and controls
300 lines (260 loc) · 8.35 KB
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
//go:build goexperiment.simd && amd64
package simdcsv
import (
"bufio"
"io"
"math/bits"
"strings"
"unsafe"
"simd/archsimd"
)
// Writer writes records using CSV encoding.
//
// Records are terminated by a newline and use ',' as the field delimiter by default.
// The exported fields can be changed before the first call to Write or WriteAll.
//
// Writes are buffered; call Flush to ensure data reaches the underlying io.Writer.
// Check Error for any errors that occurred during Write or Flush.
type Writer struct {
Comma rune // Field delimiter (set to ',' by NewWriter)
UseCRLF bool // Use \r\n as line terminator instead of \n
w *bufio.Writer
err error
}
// NewWriter returns a new Writer that writes to w.
func NewWriter(w io.Writer) *Writer {
return &Writer{
Comma: ',',
w: bufio.NewWriter(w),
}
}
// Write writes a single CSV record with necessary quoting.
// Writes are buffered; call Flush to ensure output reaches the underlying Writer.
func (w *Writer) Write(record []string) error {
if w.err != nil {
return w.err
}
for i, field := range record {
if i > 0 {
if _, w.err = w.w.WriteRune(w.Comma); w.err != nil {
return w.err
}
}
if w.err = w.writeField(field); w.err != nil {
return w.err
}
}
return w.writeLineEnding()
}
// writeField writes a single field, quoting if necessary.
func (w *Writer) writeField(field string) error {
if w.fieldNeedsQuotes(field) {
return w.writeQuotedField(field)
}
_, err := w.w.WriteString(field)
return err
}
// writeLineEnding writes \r\n or \n based on UseCRLF setting.
func (w *Writer) writeLineEnding() error {
if w.UseCRLF {
_, w.err = w.w.WriteString("\r\n")
} else {
w.err = w.w.WriteByte('\n')
}
return w.err
}
// WriteAll writes multiple records and calls Flush.
func (w *Writer) WriteAll(records [][]string) error {
for _, record := range records {
if err := w.Write(record); err != nil {
return err
}
}
return w.Flush()
}
// Flush writes buffered data to the underlying io.Writer.
func (w *Writer) Flush() error {
w.err = w.w.Flush()
return w.err
}
// Error returns any error from a previous Write or Flush.
func (w *Writer) Error() error {
return w.err
}
// writerSIMDMinSize is the minimum field size for SIMD benefit in writeQuotedField.
const writerSIMDMinSize = 16
// writerSIMDCheckThreshold is the minimum size for SIMD benefit in fieldNeedsQuotes.
// Higher than writerSIMDMinSize because checking has more overhead than writing.
const writerSIMDCheckThreshold = 64
// fieldNeedsQuotes reports whether field requires quoting.
// Dispatches to SIMD or scalar based on CPU support and field size.
func (w *Writer) fieldNeedsQuotes(field string) bool {
if len(field) == 0 {
return false
}
// Leading whitespace always requires quoting
if field[0] == ' ' || field[0] == '\t' {
return true
}
// Use SIMD only for larger fields where the overhead is justified
if useAVX512 && len(field) >= writerSIMDCheckThreshold && w.Comma >= 0 && w.Comma < 128 {
return w.fieldNeedsQuotesSIMD(field)
}
return w.fieldNeedsQuotesScalar(field)
}
// fieldNeedsQuotesScalar checks for special characters using direct byte iteration.
// This is faster than strings.ContainsAny for short strings due to charset building overhead.
func (w *Writer) fieldNeedsQuotesScalar(field string) bool {
// For ASCII comma (common case), use direct byte comparison
if w.Comma < 128 {
comma := byte(w.Comma)
for i := 0; i < len(field); i++ {
c := field[i]
if c == comma || c == '\n' || c == '\r' || c == '"' {
return true
}
}
return false
}
// For non-ASCII comma, fall back to rune iteration
for _, c := range field {
if c == w.Comma || c == '\n' || c == '\r' || c == '"' {
return true
}
}
return false
}
// fieldNeedsQuotesSIMD uses AVX-512 SIMD to detect special characters requiring quoting.
// Handles any field size >= writerSIMDMinSize using padded operations for partial chunks.
func (w *Writer) fieldNeedsQuotesSIMD(field string) bool {
data := unsafe.Slice(unsafe.StringData(field), len(field))
int8Data := bytesToInt8Slice(data)
commaCmp := cachedSepCmp[w.Comma]
// Process 64-byte chunks using AVX-512
offset := 0
for offset+simdChunkSize <= len(data) {
chunk := archsimd.LoadInt8x64Slice(int8Data[offset : offset+simdChunkSize])
commaMask := chunk.Equal(commaCmp).ToBits()
newlineMask := chunk.Equal(cachedNlCmp).ToBits()
crMask := chunk.Equal(cachedCrCmp).ToBits()
quoteMask := chunk.Equal(cachedQuoteCmp).ToBits()
if commaMask|newlineMask|crMask|quoteMask != 0 {
return true
}
offset += simdChunkSize
}
// Process remaining bytes using SIMD with partial load
if offset < len(data) {
remaining := data[offset:]
chunk := archsimd.LoadInt8x64SlicePart(bytesToInt8Slice(remaining))
commaMask := chunk.Equal(commaCmp).ToBits()
newlineMask := chunk.Equal(cachedNlCmp).ToBits()
crMask := chunk.Equal(cachedCrCmp).ToBits()
quoteMask := chunk.Equal(cachedQuoteCmp).ToBits()
// Mask out bits beyond valid data
validBits := len(remaining)
mask := (uint64(1) << validBits) - 1
if (commaMask|newlineMask|crMask|quoteMask)&mask != 0 {
return true
}
}
return false
}
// writeQuotedField writes a field surrounded by quotes, escaping internal quotes.
func (w *Writer) writeQuotedField(field string) error {
if err := w.w.WriteByte('"'); err != nil {
return err
}
// Use SIMD for fields that benefit from parallel quote detection
if useAVX512 && len(field) >= writerSIMDMinSize {
return w.writeQuotedFieldSIMD(field)
}
return w.writeQuotedFieldScalar(field)
}
// writeQuotedFieldScalar escapes quotes using optimized batch writing.
// Instead of writing character by character, it finds quotes using IndexByte
// and writes chunks between quotes in single WriteString calls.
func (w *Writer) writeQuotedFieldScalar(field string) error {
lastWritten := 0
for i := 0; i < len(field); {
// Find next quote position from current offset
idx := strings.IndexByte(field[i:], '"')
if idx == -1 {
break // No more quotes in remaining string
}
quotePos := i + idx
// Write content up to and including the quote, then add escape quote
if _, err := w.w.WriteString(field[lastWritten : quotePos+1]); err != nil {
return err
}
if err := w.w.WriteByte('"'); err != nil {
return err
}
lastWritten = quotePos + 1
i = lastWritten
}
// Write remaining content after last quote (or entire field if no quotes)
if lastWritten < len(field) {
if _, err := w.w.WriteString(field[lastWritten:]); err != nil {
return err
}
}
return w.w.WriteByte('"')
}
// writeQuotedFieldSIMD escapes quotes using AVX-512 SIMD to find quote positions.
// Handles any field size >= writerSIMDMinSize using padded operations for partial chunks.
func (w *Writer) writeQuotedFieldSIMD(field string) error {
data := unsafe.Slice(unsafe.StringData(field), len(field))
int8Data := bytesToInt8Slice(data)
offset := 0
lastWritten := 0
// Process 64-byte chunks using AVX-512
for offset+simdChunkSize <= len(data) {
chunk := archsimd.LoadInt8x64Slice(int8Data[offset : offset+simdChunkSize])
mask := chunk.Equal(cachedQuoteCmp).ToBits()
for mask != 0 {
pos := bits.TrailingZeros64(mask)
quotePos := offset + pos
// Write content up to and including the quote, then add escape quote
if _, err := w.w.WriteString(field[lastWritten : quotePos+1]); err != nil {
return err
}
if err := w.w.WriteByte('"'); err != nil {
return err
}
lastWritten = quotePos + 1
mask &= ^(uint64(1) << pos)
}
offset += simdChunkSize
}
// Process remaining bytes using SIMD with partial load
if offset < len(data) {
remaining := data[offset:]
chunk := archsimd.LoadInt8x64SlicePart(bytesToInt8Slice(remaining))
mask := chunk.Equal(cachedQuoteCmp).ToBits()
// Mask out bits beyond valid data
validBits := len(remaining)
validMask := (uint64(1) << validBits) - 1
mask &= validMask
for mask != 0 {
pos := bits.TrailingZeros64(mask)
quotePos := offset + pos
// Write content up to and including the quote, then add escape quote
if _, err := w.w.WriteString(field[lastWritten : quotePos+1]); err != nil {
return err
}
if err := w.w.WriteByte('"'); err != nil {
return err
}
lastWritten = quotePos + 1
mask &= ^(uint64(1) << pos)
}
}
// Write remaining content and closing quote
if lastWritten < len(field) {
if _, err := w.w.WriteString(field[lastWritten:]); err != nil {
return err
}
}
return w.w.WriteByte('"')
}