-
Notifications
You must be signed in to change notification settings - Fork 999
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
feat(core): Log protocol upgrades on trace #3128
Merged
mergify
merged 9 commits into
libp2p:master
from
Actyx:rku/improve-upgrade-apply-logging
Dec 14, 2022
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fe1dabb
core: improve logging of libp2p_core::upgrade::apply
rkuhn 18002ff
Apply suggestions from code review
rkuhn 4ccd17c
more PR review
rkuhn 1fb088a
replace test with doctest
rkuhn e38213c
Update core/src/upgrade.rs
rkuhn 579d86e
test all the bytes
rkuhn 90ab1ed
move DisplayProtocolName to apply.rs and make pub(crate)
rkuhn 249cb42
Merge branch 'master' into rku/improve-upgrade-apply-logging
mxinden 26a39d2
Merge branch 'master' into rku/improve-upgrade-apply-logging
mergify[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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.
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.
A stream upgrade involves various I/O operations on many layers. I don't think a heap allocated 32 byte vector have a significant performance impact.
Thus, unless the performance improvement is proven through a benchmark, I suggest allocating on the heap here.
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.
Given that all protocol names I’m aware of are quite small, I see no reason to unconditionally allocate on the heap — why should that be the default that needs no reason? I could reduce the inline capacity to keep
size_of
equal to that of aVec<u8>
, if you want.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.
I am with @rkuhn here. I think it is good to avoid allocations for the majority of the cases. I don't see the use of a different type as too much complexity.
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.
Reason for me to not use
SmallVec
by default is that it adds noise, additional compile time and increases the attack vector in our dependency supply chain. Though the last one might be a lost fight in the first place.I wrote a benchmark to try to replicate a performance benefit using
SmallVec
here. I have not been able thus far. Please take a look at Actyx#95. Maybe I can nerd-snipe either of you into proving me wrong @rkuhn and @thomaseizinger.I am sorry in case this comes across as nit-picky. I am not trying to start an argument. I am genuinely interested in performance optimizations in rust-libp2p. I am curious in which cases we could optimize through stack allocation. I don't think this is one of those cases.
On a more general note, again out of curiosity, is either of you aware of a case where heap allocation is faster than stack allocation? I can not come up with a real world case where the heap would be faster than the stack.
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.
To me this is foremost a question of using the most appropriate data structure for the job. SmallVec was already in the dependencies, and it is better suited than Vec due to usage characteristics, so I used it.
Regarding the benchmark: memory management cannot be benchmarked in this fashion. In general, microbenchmarks like yours are very rarely meaningful and even then they require careful interpretation of the result. The reason is that most of the time you are looking at a huge machine doing a tiny task, with all sorts of optimisations kicking in. And when you run a relevant task instead, different optimisations will completely change the picture. It is quite obvious that a stack allocation takes less than one CPU cycle and that a heap allocation can in general not be this fast. If your microbenchmark finds them to be equally fast, then your heap usage allows the
malloc
implementation to optimise down to a single pointer subtraction — which is a very likely thread-local optimisation to make formalloc()
.But all of this theory is irrelevant in my opinion because we’re not optimising for the last percent of performance (as far as I’m aware we’re nowhere close to getting there at this point). So I just propose usage of the most appropriate data structure at hand.
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.
I think it is reasonable to trust in CS knowledge like "stack allocations are faster than heap", even if you can't produce a micro benchmark that proves it.
Soon, this will be replaced by
Cow
so I am happy either way. If @mxinden feels strongly, let's go withVec
.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.
BTW: this discussion uses “heap” and “stack”, but “stack” doesn’t actually enter the picture in the code in question since we’re talking about a memory slot in a generator, which will end up spawned onto a thread pool, so allocated on the heap in any case.
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.
Right, what would be the right terminology then (out of curiosity?). "local variable"?
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.
That sounds appropriate to me — it’ll end up being stored in a compiler-generated
enum
variant if it lives across an.await
point.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.
Thanks for expanding here. I think the points above are valid. That said, I haven't yet formed a good guiding principle for rust-libp2p.
As said above, the concrete instance is not relevant. Let's get this merged. Looking forward to the easier debugging of protocol upgrades on streams.