-
Notifications
You must be signed in to change notification settings - Fork 469
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
Remove ArrayVec dependency #414
Merged
Vtec234
merged 6 commits into
crossbeam-rs:master
from
Shnatsel:remove-arrayvec-dependency
Sep 17, 2019
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
80cfac2
Rewrite Bag without using ArrayVec
Shnatsel c596c15
Drop ArrayVec dependency from Cargo.toml, etc.
Shnatsel 86c6bff
Implement Debug for Bag manually
Shnatsel 373f37f
More concise Debug implementation
Shnatsel 4ec94c8
Mark ugly array initialization as TODO
Shnatsel 1349724
Fix 1.28.0 build
Vtec234 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,7 +42,6 @@ use core::ptr; | |
use core::sync::atomic; | ||
use core::sync::atomic::Ordering; | ||
|
||
use arrayvec::ArrayVec; | ||
use crossbeam_utils::CachePadded; | ||
|
||
use atomic::{Shared, Owned}; | ||
|
@@ -60,10 +59,11 @@ const MAX_OBJECTS: usize = 64; | |
const MAX_OBJECTS: usize = 4; | ||
|
||
/// A bag of deferred functions. | ||
#[derive(Default, Debug)] | ||
// can't #[derive(Debug)] because Debug is not implemented for arrays 64 items long | ||
pub struct Bag { | ||
/// Stashed objects. | ||
deferreds: ArrayVec<[Deferred; MAX_OBJECTS]>, | ||
deferreds: [Deferred; MAX_OBJECTS], | ||
len: usize | ||
} | ||
|
||
/// `Bag::try_push()` requires that it is safe for another thread to execute the given functions. | ||
|
@@ -77,7 +77,7 @@ impl Bag { | |
|
||
/// Returns `true` if the bag is empty. | ||
pub fn is_empty(&self) -> bool { | ||
self.deferreds.is_empty() | ||
self.len == 0 | ||
} | ||
|
||
/// Attempts to insert a deferred function into the bag. | ||
|
@@ -89,7 +89,13 @@ impl Bag { | |
/// | ||
/// It should be safe for another thread to execute the given function. | ||
pub unsafe fn try_push(&mut self, deferred: Deferred) -> Result<(), Deferred> { | ||
self.deferreds.try_push(deferred).map_err(|e| e.element()) | ||
if self.len < MAX_OBJECTS { | ||
self.deferreds[self.len] = deferred; | ||
self.len += 1; | ||
Ok(()) | ||
} else { | ||
Err(deferred) | ||
} | ||
} | ||
|
||
/// Seals the bag with the given epoch. | ||
|
@@ -98,17 +104,48 @@ impl Bag { | |
} | ||
} | ||
|
||
impl Default for Bag { | ||
fn default() -> Self { | ||
// [no_op; MAX_OBJECTS] blocked by https://github.com/rust-lang/rust/issues/49147 | ||
#[cfg(not(feature = "sanitize"))] | ||
return Bag { len: 0, deferreds: | ||
[Deferred::new(no_op_func), Deferred::new(no_op_func), Deferred::new(no_op_func), Deferred::new(no_op_func), | ||
Deferred::new(no_op_func), Deferred::new(no_op_func), Deferred::new(no_op_func), Deferred::new(no_op_func), | ||
Deferred::new(no_op_func), Deferred::new(no_op_func), Deferred::new(no_op_func), Deferred::new(no_op_func), | ||
Deferred::new(no_op_func), Deferred::new(no_op_func), Deferred::new(no_op_func), Deferred::new(no_op_func), | ||
Deferred::new(no_op_func), Deferred::new(no_op_func), Deferred::new(no_op_func), Deferred::new(no_op_func), | ||
Deferred::new(no_op_func), Deferred::new(no_op_func), Deferred::new(no_op_func), Deferred::new(no_op_func), | ||
Deferred::new(no_op_func), Deferred::new(no_op_func), Deferred::new(no_op_func), Deferred::new(no_op_func), | ||
Deferred::new(no_op_func), Deferred::new(no_op_func), Deferred::new(no_op_func), Deferred::new(no_op_func), | ||
Deferred::new(no_op_func), Deferred::new(no_op_func), Deferred::new(no_op_func), Deferred::new(no_op_func), | ||
Deferred::new(no_op_func), Deferred::new(no_op_func), Deferred::new(no_op_func), Deferred::new(no_op_func), | ||
Deferred::new(no_op_func), Deferred::new(no_op_func), Deferred::new(no_op_func), Deferred::new(no_op_func), | ||
Deferred::new(no_op_func), Deferred::new(no_op_func), Deferred::new(no_op_func), Deferred::new(no_op_func), | ||
Deferred::new(no_op_func), Deferred::new(no_op_func), Deferred::new(no_op_func), Deferred::new(no_op_func), | ||
Deferred::new(no_op_func), Deferred::new(no_op_func), Deferred::new(no_op_func), Deferred::new(no_op_func), | ||
Deferred::new(no_op_func), Deferred::new(no_op_func), Deferred::new(no_op_func), Deferred::new(no_op_func), | ||
Deferred::new(no_op_func), Deferred::new(no_op_func), Deferred::new(no_op_func), Deferred::new(no_op_func)] | ||
}; | ||
#[cfg(feature = "sanitize")] | ||
return Bag { len: 0, deferreds: [Deferred::new(no_op_func), Deferred::new(no_op_func), Deferred::new(no_op_func), Deferred::new(no_op_func)] }; | ||
} | ||
} | ||
|
||
impl Drop for Bag { | ||
fn drop(&mut self) { | ||
// Call all deferred functions. | ||
for deferred in self.deferreds.drain(..) { | ||
for i in 0..self.len { | ||
let no_op = Deferred::new(no_op_func); | ||
let deferred = mem::replace(&mut self.deferreds[i], no_op); | ||
deferred.call(); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be an iteration over |
||
} | ||
} | ||
|
||
fn no_op_func() {} | ||
|
||
/// A pair of an epoch and a bag. | ||
#[derive(Default, Debug)] | ||
#[derive(Default)] | ||
struct SealedBag { | ||
epoch: Epoch, | ||
bag: Bag, | ||
|
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.
I'm curious on two things over why are this many particular Deferred objects created here and is there a static way to populate the list than to hardcode each new obj construction (to make it DRY'er)?
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.
Because
Deferred
isn't copy, there is only the unsafe option to start withmem::uninitialized
and then fill the array withptr::write
, but that would defeat the point of this PR. I don't believemacro_rules
macros can pattern match on natural numbers (so that we could insert the same expressionn
times with a macro), and a compiler plugin would be overkill, so it seems we have to tolerate this until the mentioned rust-lang issue is resolved.@Shnatsel could you mark this as TODO?
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.
You can match on a number but you're just matching on the token form of a number, so if you match on 3 you know that you're in the 3 branch but can't automatically use the 3 in any way. You'd need to write a macro that expanded each case by hand anyway.
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.
You could definitely do this with a macro, maybe just not in the most natural way. For example you could hardcode 0-10 fold repetition and write 64-fold repetition as repeating by 6 * 10 + 4 🙂