-
Notifications
You must be signed in to change notification settings - Fork 7
Code refactoring and performance boosts #3
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
base: main
Are you sure you want to change the base?
Conversation
|
Awesome! I'll review the change possibly by next week. In the meantime, could you please give me some performance numbers? Thanks a lot! |
|
Hey! Hell yeah! Give me some time (as it's exam season), and I'll get back to you ASAP. Thanks, |
|
Hey, sorry! No benches yet, will try today (on my decent computer). Thanks, |
|
Main branch results: |
|
My results: |
|
Turns out it's LTO. |
Will look back after and check if I can avoid the regression.
|
Have refactored the code a bit more. Please take a look at the updated code. I've reduced boilerplate with the 2 Atomic types by using the type-state pattern. |
|
Another approach would be to use traits, which could produce cleaner code, but I'm not sure I'm ready for the task yet. |
|
As the Epoch API has changed, I bumped the major version. |
|
Benchmarks (no LTO): |
|
Hi, thanks for the updates. I really didn't have a chance to look into this PR, but I'll take some time next week. |
|
Hey, can you please approve new runs? I've fixed the issue with the old ones. |
|
MSRV needs bump. Could you do it pre-merge? I can't figure out which version to set it to. |
wvwwvwwv
left a comment
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.
Hi, I only reviewed half of the change. I'll review the rest by the end of this week.
|
|
||
| [dev-dependencies] | ||
| criterion = "0.5" | ||
| criterion = "0.5.1" |
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 intentionally omitted patch versions in Cargo.toml - revert it to "0.5".
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.
Fair.
| [dependencies] | ||
| loom = { version = "0.7", optional = true } | ||
| [dependencies.loom] | ||
| version = "0.7.2" |
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.
"0.7"
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.
Fair. I also need to make it optional.
| pub(super) mod ownership { | ||
| use crate::ref_counted::RefCounted; | ||
|
|
||
| pub(super) trait Type { |
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.
(nit, optional) Can you add doc to each method/type?
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 see why it's important but honestly I'm wayyy too lazy to... could you take a shot at it?
| if let Some(f) = self.f.take() { | ||
| f(); | ||
| } | ||
| let Some(f) = self.f.take() else { |
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.
This bumps MSRV, and I don't see any readability improvements here. Can you revert it?
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.
We're bumping MSRV anyway (because of atomic add for it's performance), and I believe this is included as well.
| } | ||
| } | ||
| } else { | ||
| if (*collector_ptr).num_readers > 0 { |
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.
Any justification for this refactoring? Comparing against 0 is "usually" better than '>' in terms of performance.
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.
The compiler can most probably optimize it away, and it causes much cleaner code.
| let mut current = GLOBAL_ROOT.chain_head.load(Relaxed); | ||
| loop { | ||
|
|
||
| unsafe { |
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.
This repeats line 244-248. Can you revert it?
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.
It's either repeating or some ugly nesting, I thought this would be preferred.
| current_collector_ptr = (*collector_ptr).next_link.load(Relaxed); | ||
| continue; | ||
| } | ||
| let Ok(mut current_ptr) = Self::lock_chain() else { |
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.
This is debatable; your code is clearly "clearer", but this bumps MSRV. I'll need to think about the pros and cons here.
-> Since you bumped SDD to 4, we can make breaking changes, though keeping MSRV as low as possible is good for dependent libraries.
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.
See the previous comment about let-else.
You can test it with |
| Tag::Second => 2, | ||
| Tag::Both => 3, | ||
| } | ||
| fn not(self) -> usize { |
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.
Why is it needed? What is the semantics of this method? A Tag is intentionally a four-state type, but the output of ! is a usize that doesn't quite make sense to me...
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.
Can you explain further?
| 2 => Ok(Tag::Second), | ||
| 3 => Ok(Tag::Both), | ||
| _ => Err(val), | ||
| fn add(self, rhs: Self) -> Tag { |
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.
Tag is supposed to contain either of four possible values; this doesn't seem right.
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.
Please explain further as well: this is used for later code replacing a match statement with arithmetic (just like the previous comment!).
|
|
||
| /// [`Tag`] is a four-state `Enum` that can be embedded in a pointer as the two least | ||
| /// significant bits of the pointer value. | ||
| #[repr(usize)] |
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.
usize is a waste of memory for a four-state type.
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.
Improves performance as you don't need to convert an u8 to a usize, even though I believe that'll be optimized away by the compiler.
| pub(super) fn drop_ref(&self) -> bool { | ||
| // It does not have to be a load-acquire as everything's synchronized via the global | ||
| // epoch. | ||
| let mut current = self.ref_cnt().load(Relaxed); |
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!
- Could you revive the debug assertion?
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.
Would be hard without messing with the code a lot.
| } | ||
| }, | ||
| ) | ||
| .fetch_update(order, order, |r| (r & 1 == 1).then_some(r + 2)) |
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.
Nice!
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!
| pub fn get_guarded_ref<'g>(&self, _guard: &'g Guard) -> &'g T { | ||
| unsafe { std::mem::transmute::<&T, _>(&**self) } | ||
| pub fn get_guarded_ref<'g>(&self, _: &'g Guard) -> &'g T { | ||
| #[allow(clippy::missing_transmute_annotations)] |
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.
Would you not omit type annotations here?
| @@ -0,0 +1,97 @@ | |||
| #[cfg(feature = "loom")] | |||
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.
Please remove this file.
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.
Will do tomorrow.
| /// let prev_prev = prev.prev(); | ||
| /// assert!(prev_prev < prev); | ||
| /// ``` | ||
| #[allow(clippy::precedence)] |
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.
Could you just add a parenthesis instead of suppressing the error?
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.
Well thought of.
|
Hi, any chance to update this PR? |
|
intel i9-14900, no difference: |
|
and 3rd major update in one year =( |
Compile with target-cpu. |
Soon |
Howdy!
I've refactored most of your code (in a day! turns out it's exhausting xD), and here are most of the things I did:
Once I have some time, I ask you to review this code and, for consistency, try to change what I missed to the new code style (as long as you like it). Feel free to propose any changes!
Thanks,
Alex <3