Skip to content

[1/3] MessageDeframer: batch discard operations - #1595

Merged
ctz merged 7 commits into
rustls:mainfrom
japaric:batch-discard-operations
Dec 6, 2023
Merged

ctz merged 7 commits into
rustls:mainfrom
japaric:batch-discard-operations

Conversation

@japaric

@japaric japaric commented Nov 17, 2023

Copy link
Copy Markdown
Contributor

at @djc 's suggestion I have taken the MessageDeframer commits in #1583 into their own PR

I have also sneaked in an extra commit that batches discard operations (which may produce slight perf improvements in the Connection API) that makes the MessageDeframer operate on slices and it's part of the ongoing work on making decoding / decryption non-allocating / "in-place".

@codecov

codecov Bot commented Nov 17, 2023 •

Copy link
Copy Markdown

Codecov Report

Attention: 6 lines in your changes are missing coverage. Please review.

Comparison is base (e051f5c) 95.90% compared to head (131f4df) 95.89%.
Report is 9 commits behind head on main.

❗ Current head 131f4df differs from pull request most recent head a288b4c. Consider uploading reports for the commit a288b4c to get more accurate results

Files Patch % Lines
rustls/src/msgs/deframer.rs 97.16% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1595      +/-   ##
==========================================
- Coverage   95.90%   95.89%   -0.01%     
==========================================
  Files          78       78              
  Lines       16083    16188     +105     
==========================================
+ Hits        15424    15524     +100     
- Misses        659      664       +5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@cpu cpu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for breaking this out 👍

Comment thread rustls/src/msgs/deframer.rs Resolved
Comment thread rustls/src/msgs/deframer.rs Resolved
Comment thread fuzz/fuzzers/deframer.rs Resolved
Comment thread rustls/src/msgs/deframer.rs Resolved
Comment thread rustls/src/msgs/deframer.rs Resolved
Comment thread rustls/src/msgs/deframer.rs Resolved
Comment thread rustls/src/msgs/deframer.rs Resolved
Comment thread rustls/src/msgs/deframer.rs Resolved
@japaric
japaric force-pushed the batch-discard-operations branch 2 times, most recently from 0a42bec to 816eecc Compare November 20, 2023 13:10
@japaric

japaric commented Nov 20, 2023

Copy link
Copy Markdown
Contributor Author

@djc I've addressed your comments in new commits. I'll squash when you are all happy with the final state.

@japaric

japaric commented Nov 21, 2023

Copy link
Copy Markdown
Contributor Author

@djc anything else you would like to see addressed here or I should I go ahead and squash the commits?

@djc

djc commented Nov 21, 2023

Copy link
Copy Markdown
Member

I prefer reviewing the squashed changes anyway. I'm pretty busy this week and focusing my rustls time on things that are blocking the 0.22 release so might take me a bit to get to this.

@japaric
japaric force-pushed the batch-discard-operations branch from 816eecc to 3166291 Compare November 22, 2023 09:49
@japaric

japaric commented Nov 22, 2023

Copy link
Copy Markdown
Contributor Author

squashed

@japaric
japaric force-pushed the batch-discard-operations branch from 3166291 to 70eb6e2 Compare November 24, 2023 15:04
@japaric

japaric commented Nov 24, 2023

Copy link
Copy Markdown
Contributor Author

updated to not use the <_>::default() syntax as per PR #1620

@japaric
japaric force-pushed the batch-discard-operations branch from 70eb6e2 to e671245 Compare November 24, 2023 15:06
Comment thread rustls/src/msgs/deframer.rs Resolved
Comment thread rustls/src/msgs/deframer.rs Resolved
Comment thread rustls/src/msgs/deframer.rs Resolved
Comment thread rustls/src/msgs/deframer.rs Resolved
Comment thread rustls/src/msgs/deframer.rs Resolved
Comment thread rustls/src/msgs/deframer.rs Resolved
Comment thread rustls/src/conn.rs Outdated
.deframe(&mut self.deframer_buffer)?
.map(Message::try_from)
{
let mut to_discard = 0;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It looks like this would be simpler if to_discard (potentially with a different name) lived within the MessageDeframer and was reset by a call to a discarded(&mut self) -> usize like method.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

that's not a good place to track that because then one could use the same deframer with two different buffers and the same discard cursor would be used for both and that would be wrong (e.g. trigger a runtime panic). discard is associated to the buffer, not the deframer itself.

I have turned the discard field of DeframerSliceBuffer into a value (it was a reference) to remove the need for this caller-site stack / variable allocation.

@japaric
japaric force-pushed the batch-discard-operations branch from e671245 to ac7bbec Compare November 28, 2023 14:27
@japaric

japaric commented Nov 28, 2023

Copy link
Copy Markdown
Contributor Author

addressed the review comments

@ctz ctz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this looks reasonable to me -- my only remaining question is whether we want to land this prior to cutting 0.22? looks like it gives a small but positive performance improvement -- but is the risk/reward there?

@japaric japaric changed the title MessageDeframer: batch discard operations [1/3] MessageDeframer: batch discard operations Dec 1, 2023
@ctz

ctz commented Dec 2, 2023

Copy link
Copy Markdown
Member

my only remaining question is whether we want to land this prior to cutting 0.22? looks like it gives a small but positive performance improvement -- but is the risk/reward there?

0.22 is out, so this concern is gone. apologies for how long that took; i hope we can make quick progress on these outstanding PRs now

@ctz
ctz force-pushed the batch-discard-operations branch from ac7bbec to b8dbbc6 Compare December 4, 2023 09:33
@ctz

ctz commented Dec 4, 2023 •

Copy link
Copy Markdown
Member

Both complaints from cargo-semver-checks are somewhat irrelevant; they are changes to types only available under rustls::internals.

Comment thread rustls/src/msgs/deframer.rs Resolved
Comment thread rustls/src/msgs/deframer.rs Resolved
Comment thread rustls/src/msgs/deframer.rs Resolved
Comment thread rustls/src/msgs/deframer.rs Resolved
@pvdrz
pvdrz force-pushed the batch-discard-operations branch from b8dbbc6 to 5f760fa Compare December 4, 2023 15:39
@pvdrz
pvdrz requested a review from djc December 4, 2023 15:40
@pvdrz
pvdrz force-pushed the batch-discard-operations branch 3 times, most recently from 56b5764 to 131f4df Compare December 4, 2023 20:33

@djc djc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would be good IMO to squash the final commit into the commit that originally introduced the DeframerBuffer trait.

Comment thread rustls/src/conn.rs
let discard = deframer_buffer.pending_discard();
self.deframer_buffer.discard(discard);

match res?.map(Message::try_from) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is it correct to only yield the error after discarding here? I would find it more intuitive to have the ? directly on the deframe() call, but I don't have the context paged in for what kinds of errors the deframer will yield and whether we need to fuse it somehow?

(If we think this is the right way to do it, would be good to add a comment with some reasoning.)

@pvdrz pvdrz Dec 6, 2023 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't have a proper answer for this.

The logic before this PR shows that deframe calls pop and doesn't discard anything itself. pop only discards on its happy path. So I don't see any evident reason to not do deframe()? instead.

However, I suspect that @japaric had a reason to do this whole let res = deframe() and then do res? as it is more complicated than just doing deframe()?.

So I think this is your call, we can leave as it is or we can change it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think the best motivation for this sort of code are tests that demonstrate how things go wrong if one were to (for example) only do the discarding in the happy path. At a guess, the error fusing inside the deframer will make the distinction not observable at the public API level.

Tomorrow I think I'll write some extra tests for this; on a purely selfish level that is the way I find most effective to understand code -- reading doesn't do a lot for me.

) -> Result<Option<Deframed>, Error> {
self.inner
.pop(record_layer, negotiated_version, &mut self.buffer)
let mut deframer_buffer = self.buffer.borrow();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Same here with the discard vs error returns.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

same answer as the previous one

Comment thread rustls/src/msgs/deframer.rs Resolved
Comment thread rustls/src/msgs/deframer.rs Resolved
@pvdrz
pvdrz force-pushed the batch-discard-operations branch from 131f4df to a288b4c Compare December 6, 2023 16:24
@pvdrz

pvdrz commented Dec 6, 2023

Copy link
Copy Markdown
Contributor

Would be good IMO to squash the final commit into the commit that originally introduced the DeframerBuffer trait.

done

@pvdrz
pvdrz force-pushed the batch-discard-operations branch from a288b4c to f55b5eb Compare December 6, 2023 16:27

@cpu cpu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks great, thanks for your attention to the commit history. The DeframerBuffer const generic approach was a cool idea 👍

@ctz
ctz added this pull request to the merge queue Dec 6, 2023
Merged via the queue into rustls:main with commit 17e938c Dec 6, 2023
@tshepang
tshepang deleted the batch-discard-operations branch December 7, 2023 11:34
@cpu cpu mentioned this pull request Dec 7, 2023
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.

5 participants