Skip to content
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

introduce hasKnownResult to optimize several parsers. #348

Merged
merged 7 commits into from
Jan 14, 2022

Conversation

johnynek
Copy link
Collaborator

@johnynek johnynek commented Jan 9, 2022

Often, we can statically see what the result value will be if a parser succeeds.

If we know this, can optimize map, select and flatMap.

In particular, we can avoid reallocating a parser inside each flatMap.

Most users should avoid using flatMap anyway, but in a library function, you might need flatMap but want to get the optimization when the user passes you a constant parser.

@johnynek johnynek requested a review from regadas January 9, 2022 20:27
@codecov-commenter
Copy link

codecov-commenter commented Jan 9, 2022

Codecov Report

Merging #348 (faa8f8f) into main (3acc967) will increase coverage by 0.34%.
The diff coverage is 98.75%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #348      +/-   ##
==========================================
+ Coverage   96.56%   96.91%   +0.34%     
==========================================
  Files           9        9              
  Lines        1049     1103      +54     
  Branches       94       98       +4     
==========================================
+ Hits         1013     1069      +56     
+ Misses         36       34       -2     
Impacted Files Coverage Δ
core/shared/src/main/scala/cats/parse/Parser.scala 96.73% <98.75%> (+0.31%) ⬆️
...shared/src/main/scala/cats/parse/Accumulator.scala 100.00% <0.00%> (+2.43%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3acc967...faa8f8f. Read the comment docs.

Copy link
Collaborator

@regadas regadas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be interesting to see new benchmark numbers.

@regadas
Copy link
Collaborator

regadas commented Jan 12, 2022

@johnynek could you rebase this?

@johnynek
Copy link
Collaborator Author

Yes! I will do. Thanks for the review.

Note, this optimization will mostly apply to flatMap, and most parser don't need flatMap (e.g. our json benchmarks don't use flatMap). But still, I thought it was worth trying to limit the worst case.

@johnynek
Copy link
Collaborator Author

@regadas let me know if this looks good.

Copy link
Collaborator

@regadas regadas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

@regadas regadas merged commit 7b524da into main Jan 14, 2022
@regadas regadas deleted the oscar/flat_map_opt branch January 14, 2022 00:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants