-
-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bring JSON parser on par with circe-fs2 #491
Merged
Merged
+700
−178
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This abstraction allows to create chunks of various types out of an input stream. One possible accumulator is the `Token` accumulator which generates the token stream. Leveraging this abstraction, we can then implement a pipe that builds directly AST values instead of having intermediate `Token` representation.
Inspired by the `Facade` abstraction from jawn, we can build the AST directly, without emitting intermediate tokens, which makes it faster.
@ybasket this PR is ready to be reviewed! |
ybasket
approved these changes
Jul 16, 2023
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work and sorry for the time it took me to review it!
json/src/main/scala/fs2/data/json/internal/JsonTokenParser.scala
Outdated
Show resolved
Hide resolved
The fact that it now returs `Unit` makes it clearer it is side effectful.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The goal of this PR is to have performances on par with circe-fs2, so that users can switch fearlessly to fs2-data (see circe/circe-fs2#425).
The existing two steps approach has a major drawback, that prevents this from happening: it builds intermediate
Token
, which are immediately consumed to build the AST using an instance ofBuilder
.This can be avoided for people coming from circe-fs2, by adding a new pipe
ast.parse
that directly transforms the input byte/char/string stream into the AST.To achieve this, this PR takes inspiration from the
Facade
approach in jawn to model theChunkAccumulator
in the JSON parser. One such simple accumulator simply wraps the existingVectorBuilder[Token]
and makes it possible to build the token stream. Another implementation (BuilderChunkAccumulator[Json]
) directly builds the AST from the parser without instantiating anyToken
. In the future, it can even be extended to implement filtering at the source, without constructing theToken
s that are not selected. The abstraction is kept internal for now, so that we have more latitude to change the API and stabilize it.ChunkAccumulator
TokenChunkAccumulator
BuilderChunkAccumulator
ast.parse
.through(tokens).through(ast.values)
to.through(ast.parse)
ast.parse
documentationUsing the new abstraction, here are the results of the benchmark.