| CARVIEW |
flatparse: High-performance parsing from strict bytestrings
Flatparse is a high-performance parsing library for strict bytestring input. See the README for more information:
https://github.com/AndrasKovacs/flatparse.
[Skip to Readme]
Flags
Manual Flags
| Name | Description | Default |
|---|---|---|
| dump | dump core, stg and cmm to files | Disabled |
| llvm | use llvm for building | Disabled |
Use -f <flag> to enable a flag, or -f -<flag> to disable that flag. More info
Downloads
- flatparse-0.5.3.1.tar.gz [browse] (Cabal source package)
- Package description (as included in the package)
Maintainer's Corner
For package maintainers and hackage trustees
Candidates
| Versions [RSS] | 0.1.0.0, 0.1.0.1, 0.1.0.2, 0.1.1.1, 0.1.1.2, 0.2.0.0, 0.2.1.0, 0.2.2.0, 0.3.0.0, 0.3.0.1, 0.3.0.2, 0.3.0.3, 0.3.1.0, 0.3.2.0, 0.3.3.0, 0.3.4.0, 0.3.5.0, 0.3.5.1, 0.4.0.0, 0.4.0.1, 0.4.0.2, 0.4.1.0, 0.5.0.0, 0.5.0.1, 0.5.0.2, 0.5.1.0, 0.5.1.1, 0.5.2.0, 0.5.2.1, 0.5.3.0, 0.5.3.1 |
|---|---|
| Dependencies | base (>=4.7 && <5), bytestring, containers, integer-gmp, template-haskell, utf8-string (>=1.0.2 && <1.1) [details] |
| Tested with | ghc ==8.6.5, ghc ==8.8.4, ghc ==8.10.7, ghc ==9.0.2, ghc ==9.2.8, ghc ==9.4.8, ghc ==9.6.7, ghc ==9.8.4 |
| License | MIT |
| Copyright | 2021 András Kovács |
| Author | András Kovács |
| Maintainer | puttamalac@gmail.com |
| Uploaded | by AndrasKovacs at 2025-10-07T15:25:27Z |
| Category | Parsing |
| Home page | https://github.com/AndrasKovacs/flatparse#readme |
| Bug tracker | https://github.com/AndrasKovacs/flatparse/issues |
| Source repo | head: git clone https://github.com/AndrasKovacs/flatparse |
| Distributions | LTSHaskell:0.5.3.1, NixOS:0.5.3.1, Stackage:0.5.3.1 |
| Reverse Dependencies | 11 direct, 30 indirect [details] |
| Downloads | 5304 total (139 in the last 30 days) |
| Rating | (no votes yet) [estimated by Bayesian average] |
| Your Rating |
|
| Status | Docs available [build log] Last success reported on 2025-10-07 [all 1 reports] |
Readme for flatparse-0.5.3.1
[back to package description]flatparse
flatparse is a high-performance parsing library, supporting parsing for programming languages, human-readable data and machine-readable data. The "flat" in the name refers to the ByteString parsing input, which has pinned contiguous data, and also to the library internals, which avoids indirections and heap allocations whenever possible. flatparse is generally lower-level than parsec-style libraries, but it is possible to build higher-level features (such as source spans, hints, indentation parsing) on top of it, without making any compromises in performance.
LLVM
It is advised to build with -fllvm
option
when using this package, since that can result in significant speedups (20-40%
from what I've seen). Additionally, you can enable -fllvm for flatparse
specifically by enabling the llvm package flag. However, this has minor
impact, since almost all parser code will be typically inlined into modules
outside flatparse, and compiled there.
Features and non-features
- Excellent performance. On microbenchmarks,
flatparseis 2-10 times faster thanattoparsecormegaparsec. On larger examples with heavier use of source positions and spans and/or indentation parsing, the performance difference may be greater, becauseflatparse-s handling of source positions is heavily optimized. Compile times and executable sizes are also significantly better withflatparsethan withmegaparsecorattoparsec.flatparseinternals make liberal use of unboxed tuples and GHC primops. As a result, pure validators (parsers returning()) inflatparseare not difficult to implement with zero heap allocation. - No incremental parsing, and only strict
ByteStringis supported as input. However, it can be still useful to convert fromText,Stringor other types toByteString, and then useflatparsefor parsing, sinceflatparseperformance usually more than makes up for the conversion costs. - Only little-endian systems are currently supported as the host machine. This may change in the future. However,
flatparsedoes include primitive integer parsers with specific endianness. - Support for fast source location handling, indentation parsing and informative error messages.
flatparseprovides a low-level interface to these. Batteries are not included, but it should be possible for users to build custom solutions, which are more sophisticated, but still as fast as possible. In my experience, the included batteries in other libraries often come with major unavoidable overheads, and often we still have to extend existing machinery in order to scale to production features. - The backtracking model of
flatparseis different to parsec libraries, and is more close to the nom library in Rust. The idea is that parser failure is distinguished from parsing error. The former is used for control flow, and we can backtrack from it. The latter is used for unrecoverable errors, and by default it's propagated to the top.flatparsedoes not track whether parsers have consumed inputs. In my experience, what we really care about is the failure/error distinction, and inparsecormegaparsecthe consumed/non-consumed separation is often muddled and discarded in larger parser implementations. By default, basicflatparseparsers can fail but can not throw errors, with the exception of the specifically error-throwing operations. Hence,flatparseusers have to be mindful about grammar, and explicitly insert errors where it is known that the input can't be valid.
flatparse comes in two flavors: FlatParse.Basic and FlatParse.Stateful. Both support a custom error type. Also, both come in three modes, where we can respectively run IO actions, ST actions, or no side effects. The modes are selected by a state token type parameter on the parser types.
FlatParse.Basiconly supports the above features. If you don't need indentation parsing, this is sufficient.FlatParse.Statefuladditionally supports a built-inIntworth of internal state and an additional custom reader environment. This can support a wide range of indentation parsing features. There is a moderate overhead in performance and code size compared toBasic. In microbenchmarks and small parsers, the performance difference betweenBasicandStatefulis more up to the whims of GHC and LLVM, and is a bit more "random".
Tutorial
Informative tutorials are work in progress. See src/FlatParse/Examples
for a lexer/parser example with acceptably good error messages.
Contribution
Pull requests are welcome. I'm fairly quick to add PR authors as collaborators.
Some benchmarks
Execution times below. See source code in bench. Compiled with GHC 9.10.2 -O2 -fllvm with
flatparse-0.5.3.1. Executed on AMD 9800X3D CPU. Uses lts-24.7 Stackage snapshot for the involved
packages.
| benchmark | runtime |
|---|---|
| sexp/fpbasic | 1.80 ms |
| sexp/fpstateful | 1.25 ms |
| sexp/attoparsec | 10.2 ms |
| sexp/megaparsec | 6.92 ms |
| sexp/parsec | 39.9 ms |
| long keyword/fpbasic | 0.054 ms |
| long keyword/fpstateful | 0.062 ms |
| long keyword/attoparsec | 0.308 ms |
| long keyword/megaparsec | 0.687 ms |
| long keyword/parsec | 3.50 ms |
| numeral csv/fpbasic | 0.540 ms |
| numeral csv/fpstateful | 0.504 ms |
| numeral csv/attoparsec | 3.17 ms |
| numeral csv/megaparsec | 1.09 ms |
| numeral csv/parsec | 13.8 ms |
| lambda term/fpbasic | 1.52 ms |
| lambda term/fpstateful | 1.56 ms |
| lambda term/attoparsec | 4.94 ms |
| lambda term/megaparsec | 5.35 ms |
| lambda term/parsec | 17.7 ms |
Object file sizes for each module containing the s-exp, long keyword and numeral csv benchmarks.
| library | object file size (bytes) |
|---|---|
| fpbasic | 71088 |
| fpstateful | 73576 |
| attoparsec | 242816 |
| megaparsec | 402984 |
| parsec | 329008 |