Skip to content

Commit e000658

Browse files
committed
refactor: use cached values in writer
1 parent 553b753 commit e000658

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

simd_scanner.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,14 @@ var useAVX512 bool
2323

2424
// Cached broadcast values for fixed characters (initialized in init()).
2525
var (
26+
// AVX-512 (64-byte) cached values
2627
cachedQuoteCmp archsimd.Int8x64
2728
cachedCrCmp archsimd.Int8x64
2829
cachedNlCmp archsimd.Int8x64
30+
// AVX2 (32-byte) cached values
31+
cachedQuoteCmp32 archsimd.Int8x32
32+
cachedCrCmp32 archsimd.Int8x32
33+
cachedNlCmp32 archsimd.Int8x32
2934
)
3035

3136
// SIMD processing constants.
@@ -44,6 +49,10 @@ func init() {
4449
cachedQuoteCmp = archsimd.BroadcastInt8x64('"')
4550
cachedCrCmp = archsimd.BroadcastInt8x64('\r')
4651
cachedNlCmp = archsimd.BroadcastInt8x64('\n')
52+
// Pre-broadcast fixed characters for AVX2 (32-byte) operations
53+
cachedQuoteCmp32 = archsimd.BroadcastInt8x32('"')
54+
cachedCrCmp32 = archsimd.BroadcastInt8x32('\r')
55+
cachedNlCmp32 = archsimd.BroadcastInt8x32('\n')
4756
}
4857
}
4958

writer.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,16 @@ func (w *Writer) fieldNeedsQuotesScalar(field string) bool {
123123
}
124124

125125
// fieldNeedsQuotesSIMD uses SIMD to detect special characters requiring quoting.
126+
// Uses cached broadcast values for fixed characters (quote, CR, NL) to avoid
127+
// repeated BroadcastInt8x32 calls.
126128
func (w *Writer) fieldNeedsQuotesSIMD(field string) bool {
127129
data := unsafe.Slice(unsafe.StringData(field), len(field))
128130
int8Data := bytesToInt8Slice(data)
129131

130132
commaCmp := archsimd.BroadcastInt8x32(int8(w.Comma))
131-
newlineCmp := archsimd.BroadcastInt8x32('\n')
132-
carriageReturnCmp := archsimd.BroadcastInt8x32('\r')
133-
quoteCmp := archsimd.BroadcastInt8x32('"')
133+
newlineCmp := cachedNlCmp32
134+
carriageReturnCmp := cachedCrCmp32
135+
quoteCmp := cachedQuoteCmp32
134136

135137
// Process 32-byte chunks
136138
offset := 0
@@ -186,10 +188,11 @@ func (w *Writer) writeQuotedFieldScalar(field string) error {
186188
}
187189

188190
// writeQuotedFieldSIMD escapes quotes using SIMD to find quote positions.
191+
// Uses cached broadcast value for quote character to avoid repeated BroadcastInt8x32 calls.
189192
func (w *Writer) writeQuotedFieldSIMD(field string) error {
190193
data := unsafe.Slice(unsafe.StringData(field), len(field))
191194
int8Data := bytesToInt8Slice(data)
192-
quoteCmp := archsimd.BroadcastInt8x32('"')
195+
quoteCmp := cachedQuoteCmp32
193196

194197
offset := 0
195198
lastWritten := 0

0 commit comments

Comments
 (0)