FastCSV: a high-performance CSV parser and writer library for Swift

Hello everyone.

A while ago, I was looking for a CSV parser in Swift. While there are a few popular out there for Swift that are pretty good, I found them to be a little bit slow, so decided to try to write my own. Then I fell into a rabbit hole of trying to squeeze as much performance as I could out of it. I was quickly able to outperform SwiftCSV which appears to be the most popular library for swift, but surprisingly it took me a while to beat the Python CSV parser.

I was pretty happy with the results and thought it might be worthwhile to spend some effort to make it usable for others. The result is FastCSV. I've tried to make it as swifty as I could. Here are some of the features.

Reading

  • Decodable support โ€” decode CSV rows directly into Swift structs, only materializing the columns you need

  • Column mapping โ€” map CSV headers to struct properties at the call site, no CodingKeys required

  • High-performance parsing with zero-copy techniques

  • Low memory footprint through chunked streaming โ€” constant memory regardless of file size

  • Three API tiers โ€” typed structs via Decodable, dictionary access by column name, or raw array iteration

  • Configurable delimiters supporting standard CSV, TSV, and custom formats

  • Quote handling with optional optimization for quote-free data

  • Error recovery allowing processing to continue despite malformed rows

  • UTF-8 BOM detection and automatic removal

Writing

  • Encodable support โ€” write Swift structs directly to CSV with automatic header derivation

  • RFC 4180 quoting โ€” fields containing delimiters, quotes, or newlines are quoted automatically

  • Multiple output targets โ€” write to file path, URL, or string

  • Row-by-row or batch โ€” streaming writes via CSVWriter or one-shot via static methods

  • Round-trip fidelity โ€” read, transform, and write back with full type preservation

A few things I'm interested in adding to it in the future are support for compressed CSV files and exploring SIMD to try to improve performance further.

Hope this will be of use to somebody. The repo is at GitHub - wombat2k/FastCSV: FastCSV is a high-performance CSV (Comma-Separated Values) parser written in Swift ยท GitHub.

11 Likes

I haven't looked closely if you use any internationalization features of Foundation, but on non-Apple platforms it is hugely beneficial to use FoundationEssentials to not pay the size cost of icu.

Would it be possible to replace all uses if import Foundation with:

#if canImport(FoundationEssentials)
import FoundationEssentials
#else
import Foundation
#endif
3 Likes

I use DateFormatter as a decoding convenience, so would need to change to something like ParseableFormatStyle which apparently is the more modern way of doing this. Will give this a a try.

2 Likes

OK, this turned out to be trickier than I thought. It did result in a small breaking change, so changed the version to 1.1. It also allowed me to find a bug when decoding dates. I'm also now testing locally on Linux. Thanks for the suggestion, I think it really improved the overall library.

1 Like