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

parser: two-phase parsing #120

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open

Conversation

ashtum
Copy link
Collaborator

@ashtum ashtum commented Jan 12, 2025

With the new changes, the parser returns immediately after the header is parsed and does not begin parsing the body until the next call to parse(). In the case of bodiless messages and head responses, it directly transitions to the complete_in_place state after the header is parsed, making a call to parse() unnecessary (but still valid).

This two-phase parsing brings a few benefits with almost no complications on the usage side of the API:

  • It introduces an optimization opportunity for users who want to attach a body. If they do so immediately after the header is parsed (which seems to be the case most of the time), there's no need for cb1_ for elastic bodies and a small cb1_ for sink bodies (as it will be used temporarily). This means all the extra space can be utilized for cb0_.
  • Because parsing the body might complete with an error, returning after the header is parsed allows users to access the header and on the next call to parse encounter the error.
  • Setting the body limit in the middle of parsing the body or after it doesn't make much sense, so returning right after the header is parsed provides a window for setting such limits.
  • If users want to attach a body, they will almost always do so immediately after the header is parsed. By not continuing the parsing of the body, we avoid the need for an extra buffer copy operation (in case the user wants to attach a buffer).

@cppalliance-bot
Copy link

An automated preview of the documentation is available at https://120.http-proto.prtest.cppalliance.org/index.html

1 similar comment
@cppalliance-bot
Copy link

An automated preview of the documentation is available at https://120.http-proto.prtest.cppalliance.org/index.html

With the new changes, the parser returns immediately after the header is parsed
and does not begin parsing the body until the next call to `parse()`. In the
case of bodiless messages and head responses, it directly transitions to the
`complete_in_place` state after the header is parsed, making a call to `parse()`
unnecessary (but still valid).

This two-phase parsing brings a few benefits with almost no complications on the
usage side of the API:

- It introduces an optimization opportunity for users who want to attach a body.
  If they do so immediately after the header is parsed (which seems to be the
  case most of the time), there's no need for `cb1_` for elastic bodies and a
  small `cb1_` for sink bodies (as it will be used temporarily). This means all
  the extra space can be utilized for `cb0_`.
- Because parsing the body might complete with an error, returning after the
  header is parsed allows users to access the header and on the next call to
  parse encounter the error.
- Setting the body limit in the middle of parsing the body or after it doesn't
  make much sense, so returning right after the header is parsed provides a
  window for setting such limits.
- If users want to attach a body, they will almost always do so immediately
  after the header is parsed. By not continuing the parsing of the body, we
  avoid the need for an extra buffer copy operation (in case the user wants to
  attach a buffer).
@cppalliance-bot
Copy link

An automated preview of the documentation is available at https://120.http-proto.prtest.cppalliance.org/index.html

Copy link

codecov bot commented Jan 12, 2025

Codecov Report

Attention: Patch coverage is 87.27273% with 14 lines in your changes missing coverage. Please review.

Project coverage is 90.96%. Comparing base (cfc59eb) to head (9990825).

Files with missing lines Patch % Lines
src/parser.cpp 87.27% 14 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #120      +/-   ##
===========================================
- Coverage    91.11%   90.96%   -0.15%     
===========================================
  Files           81       81              
  Lines         4806     4826      +20     
===========================================
+ Hits          4379     4390      +11     
- Misses         427      436       +9     
Files with missing lines Coverage Δ
include/boost/http_proto/parser.hpp 100.00% <ø> (ø)
src/parser.cpp 88.17% <87.27%> (-0.93%) ⬇️

Continue to review full report in Codecov by Sentry.

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

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.

2 participants