Zero-dependency, pure-Go (zero CGO), Go 1.18+ RFC 4180 & encoding/csv-compliant zero-allocation SIMD CSV parser.
pcsv provides both a zero-allocation streaming API (Scan / ForEach / Go 1.23 Records) returning borrowed Record views and a drop-in encoding/csv-compatible API (Read / ReadAll / FieldPos / InputOffset). On amd64 (AVX2/SSE2) and arm64 (NEON), structural byte scanning ({Comma, '"', '\n', '\r'}) and quoted-field scanning ({'"', '\r'}) use Plan 9 SIMD assembly, with a portable 8-byte SWAR (uint64) fallback on other architectures and under -tags purego.
Requires Go 1.18+ (Records iterator requires Go 1.23+; Reader, NewBytesReader, Scan, ForEach, Read, and ReadAll work on Go 1.18+).
go get github.com/gatewaytofredom/pcsvUse -tags purego to disable amd64/arm64 assembly and force the pure-Go SWAR scanner (e.g. for restricted sandboxes or auditability).
rec.Field(i) returns a borrowed []byte valid until the next Scan, Read, Reset, or ResetBytes call (0 B/op, 0 allocs/op after warmup):
r := pcsv.NewReader(src) // or pcsv.NewBytesReader(buf) for zero-copy []byte input
for r.Scan() {
rec := r.Record()
id := rec.Field(0)
name := rec.Field(1)
_, _ = id, name
}
if err := r.Err(); err != nil {
return err
}r := pcsv.NewBytesReader(data)
err := r.ForEach(func(rec pcsv.Record) error {
// Borrowed []byte slices valid for this callback invocation.
fmt.Printf("%s: %s\n", rec.Field(0), rec.Field(1))
return nil
})r := pcsv.NewReader(src,
pcsv.WithComma(';'),
pcsv.WithComment('#'),
pcsv.WithTrimLeadingSpace(true),
)
for {
rec, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
return err // returns *pcsv.ParseError (Line, Column, StartLine, Err)
}
_ = rec
}for rec, err := range pcsv.Records(src, pcsv.WithComma(',')) {
if err != nil {
return err
}
_ = rec.Field(0)
}Measured on 12th Gen Intel Core i5-12400 (linux/amd64, Go 1.23.6) with -benchtime=500ms:
| Benchmark | Unquoted CSV | Mixed Quoted/Escaped CSV | Allocs / 100 KB |
|---|---|---|---|
encoding/csv (Read) |
389.65 MB/s | 567.51 MB/s | 2,372 – 3,584 allocs |
encoding/csv (Read, ReuseRecord) |
476.26 MB/s | 759.41 MB/s | 1,194 – 1,800 allocs |
pcsv.NewBytesReader (Scan) |
1201.31 MB/s (3.1×) | 736.35 MB/s | 0 B/op, 0 allocs/op |
pcsv.NewReader (Scan) |
1166.83 MB/s (3.0×) | 710.54 MB/s | 0 allocs/op |
pcsv.NewBytesReader (ForEach) |
1161.62 MB/s (3.0×) | 701.14 MB/s | 0 B/op, 0 allocs/op |
pcsv.NewReader (Read, ReuseRecord) |
468.64 MB/s | 446.49 MB/s | 1,180 – 1,786 allocs |
| Kernel | Scalar (SWAR) | SSE2 (amd64) |
AVX2 (amd64) |
NEON (Apple M4) |
|---|---|---|---|---|
IndexStructural |
2,693.75 MB/s | 11,588.82 MB/s | 15,344.72 MB/s | 11,334.77 MB/s |
ScanQuoteCR |
5,449.53 MB/s | 30,884.54 MB/s | 71,963.90 MB/s | 54,822.40 MB/s |
| Benchmark | Unquoted CSV | Mixed Quoted/Escaped CSV | Allocs / 100 KB |
|---|---|---|---|
encoding/csv (Read) |
449.39 MB/s | 612.50 MB/s | 2,372 – 3,584 allocs |
encoding/csv (Read, ReuseRecord) |
530.90 MB/s | 730.15 MB/s | 1,194 – 1,800 allocs |
pcsv.NewBytesReader (Scan) |
1,507.29 MB/s (3.4×) | 944.35 MB/s | 0 B/op, 0 allocs/op |
pcsv.NewReader (Scan) |
1,476.43 MB/s (3.3×) | 934.33 MB/s | 0 allocs/op |
go vet ./...
go vet -tags purego ./...
go test -race ./...
go test -tags purego ./...
go test -fuzz=FuzzDifferentialEncodingCSV -fuzztime=10s .
go test -bench='Benchmark(CSV|IndexStructural|ScanQuoteCR)' -benchmem -benchtime=500ms .