Skip to content
gatewaytofredomPublic

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pcsv

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.

Install

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/pcsv

Use -tags purego to disable amd64/arm64 assembly and force the pure-Go SWAR scanner (e.g. for restricted sandboxes or auditability).

Quick Start

Zero-allocation streaming (Scan)

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
}

Callback streaming (ForEach)

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
})

Drop-in encoding/csv compatibility (Read / ReadAll)

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
}

Go 1.23+ range-over-func iterator (Records)

for rec, err := range pcsv.Records(src, pcsv.WithComma(',')) {
	if err != nil {
		return err
	}
	_ = rec.Field(0)
}

Benchmarks

Measured on 12th Gen Intel Core i5-12400 (linux/amd64, Go 1.23.6) with -benchtime=500ms:

End-to-End CSV Parsing (100 KB inputs)

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

SIMD Scanner Microbenchmarks (16 KB buffer)

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

Apple M4 End-to-End (darwin/arm64, NEON, 100 KB inputs)

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

Tests & Fuzzing

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 .

License

MIT

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages