streamly-flatparse: Run flatparse parsers on Streamly streams of byte arrays

[ bsd3, library, parsing, streamly ] [ Propose Tags ] [ Report a vulnerability ]

flatparse parsers are fast, but need their whole input in one piece. This library runs them on a stream of byte arrays, as they come from a socket or a file: it gathers the arrays until the parser succeeds, or until a delimiter, within a size, and gives back the stream that follows what was parsed.


[Skip to Readme]

Modules

  • Streamly
    • External
      • Streamly.External.FlatParse

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1.0.0
Change log CHANGELOG.md
Dependencies base (>=4.19 && <4.22), bytestring (>=0.12 && <0.13), flatparse (>=0.5 && <0.6), streamly-bytestring (>=0.2.3 && <0.3), streamly-core (>=0.3.3 && <0.4) [details]
Tested with ghc ==9.8.4 || ==9.10.3 || ==9.12.4
License BSD-3-Clause
Author Xavier Dectot
Maintainer Xavier Dectot <xavier@wheredoibegin.fr>
Uploaded by GdeVolpiano at 2026-11-28T09:53:03Z
Category Parsing, Streamly
Home page https://github.com/GuillaumedeVolpiano/streamly-flatparse
Source repo head: git clone https://github.com/GuillaumedeVolpiano/streamly-flatparse.git
Distributions
Reverse Dependencies 2 direct, 3 indirect [details]
Downloads 0 total (0 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs not available [build log]
All reported builds failed as of 2026-11-28 [all 1 reports]

Readme for streamly-flatparse-0.1.0.0

[back to package description]

streamly-flatparse

Run flatparse parsers on Streamly streams of byte arrays.

flatparse parsers are fast, but they read one piece of bytes and cannot stop to wait for more. Bytes that come from a socket or a file come in arrays cut anywhere. This library stands between the two, in place of the parsers of Streamly: it gathers the arrays of a stream until a parser has what it needs, within a size, and gives the stream that follows what was parsed.

{-# LANGUAGE OverloadedStrings #-}

import Data.Word (Word8)
import FlatParse.Basic (Parser, anyAsciiDecimalInt, byteString)
import Streamly.Data.Array (Array)
import Streamly.Data.Stream (Stream)
import Streamly.External.FlatParse (ParseError, parseMany)

-- A decimal number on a line of its own.
line :: Parser () Int
line = anyAsciiDecimalInt <* byteString "\n"

numbers :: Monad m => Stream m (Array Word8) -> Stream m (Either (ParseError ()) Int)
numbers = parseMany 100 line

What it gives

  • parseBreak: a parser on the start of a stream, tried again as arrays come, with the stream that follows what it read. parse gives the result alone.
  • parseBreakOn: the same after waiting for a delimiter, the parser run once on the bytes up to it.
  • parseMany: a parser again and again, as a stream of what it finds.
  • parseArray: a parser on one array that holds the whole input.

Each takes the largest number of bytes the parser may be given.

What the parsers must be like

  • For parseBreak and parseMany, a failure is taken to mean that the input is not all there yet. The parser must therefore fail on every proper prefix of what it accepts: it must end on something that tells its end, such as a delimiter. A parser that succeeds early, like "some digits", would stop at the end of an array with more digits to come. An error (cut, err) stops at once: use one where no further byte can help.
  • For parseBreakOn, the delimiter must not occur inside what is parsed.

Speed

On the head of an HTTP response of 241 bytes (cabal bench):

flatparse alone, on the bytes 0.58 μs
parseBreak, the head in one array 0.60 μs
parseBreak, the head cut in three arrays 1.17 μs
parseBreakOn the empty line, in one array 0.93 μs

On five thousand short lines, 28 kB, each parsed by parseMany:

flatparse alone, one parser for all the lines 81 μs
parseMany, the lines in one array 237 μs
parseMany, in arrays of 4096 bytes 226 μs

With parseBreak the input is parsed again from its start at each array: cheap for what fits in a few arrays, wasteful for long inputs, which parseBreakOn parses once.