Skip to content

introduce hasKnownResult to optimize several parsers.#348

Merged
regadas merged 7 commits into
mainfrom
oscar/flat_map_opt
Jan 14, 2022
Merged

introduce hasKnownResult to optimize several parsers.#348
regadas merged 7 commits into
mainfrom
oscar/flat_map_opt

Conversation

@johnynek
Copy link
Copy Markdown
Contributor

@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
Copy Markdown

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
Copy Markdown
Contributor

@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
Copy Markdown
Contributor

regadas commented Jan 12, 2022

@johnynek could you rebase this?

@johnynek
Copy link
Copy Markdown
Contributor 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
Copy Markdown
Contributor Author

@regadas let me know if this looks good.

Copy link
Copy Markdown
Contributor

@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