AndrasKovacs / flatparse

Fast parsing from bytestrings
MIT License
144 stars 11 forks source link

Levity polymorphism #5

Open AndrasKovacs opened 3 years ago

AndrasKovacs commented 3 years ago

Currently we can only return LiftedRep types from parsers. Sometimes this performs poorly, when we want to parse Int, Double, or other directly unboxable types. While sometimes it's possible to avoid boxing by using tail-recursive functions with unboxed accumulators, it's generally more convenient (and possibly more efficient) if we can use non-tail recursion and basic combinators with unboxed return values.

Unfortunately the ergonomics of levity polymorphism is awful. Example: https://hackage.haskell.org/package/bytesmith-0.3.7.0/docs/Data-Bytes-Parser-Rebindable.html I think something like this would be acceptable, but for the sake of usability more functions should be included in levity-polymorphic classes. This would require boilerplate (or code generation) on the implementation side, but would be hopefully more convenient on user's side.

raehik commented 2 years ago

Related: For certain cases, where you want to parse some data and consume it inside the parser (eventually returning some lifted value), withX# :: (X# -> Parser e a) -> Parser e a is useful and I think (?) efficient. You still can't return unboxed types, but it can result in fewer allocations (I think). See example usage in an file table format (reading data at offsets) here.