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

Add select::FdSet::fds() method #1207

Merged
merged 8 commits into from Apr 19, 2020
Merged

Add select::FdSet::fds() method #1207

merged 8 commits into from Apr 19, 2020

Conversation

zombiezen
Copy link
Contributor

To be more consistent with most Rust APIs and enable cloning of the iterator, I made FdSet::contains operate on an immutable borrow instead of a mutable one by copying the set. If this is not desirable, I can roll that back from this PR and focus purely on the fds() method.

Copy link
Member

@asomers asomers left a comment

Choose a reason for hiding this comment

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

This looks like a good feature. Let's wait to see if rust-lang/libc#1725 is approved before we decide how to implement contains

pub fn contains(&mut self, fd: RawFd) -> bool {
unsafe { libc::FD_ISSET(fd, &mut self.0) }
pub fn contains(&self, fd: RawFd) -> bool {
let mut copy = self.0;
Copy link
Member

Choose a reason for hiding this comment

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

libc::FD_ISSET should be taking a *const pointer rather than *mut. I just opened a PR to fix that.
rust-lang/libc#1725

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As an aside, if we're worried about the cost of this copy, then we might want to consider not deriving Copy for FdSet.

Copy link
Member

Choose a reason for hiding this comment

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

You know, that's an excellent point. On my system, FdSet is 128 bytes big. Users probably shouldn't copy such a time unless they really need to. However removing Copy doesn't necessarily prevent the value from ever being memcpy'd, and the consensus is that it's best to impl Copy regardless of size. See https://www.reddit.com/r/rust/comments/2xxjda/when_should_my_type_be_copy/ and https://internals.rust-lang.org/t/pre-rfc-copy-like-trait-for-large-types/11852/14 for some discussion.

Copy link
Member

Choose a reason for hiding this comment

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

I think that for now we should proceed without waiting for rust-lang/libc#1725 . But copying the entire structure on each loop of the iterator is woefully inefficient. I think you should retain the &mut reference instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. This sadly means that the iterator cannot implement Clone, but I don't see a good way around that.

src/sys/select.rs Outdated Show resolved Hide resolved
/// assert_eq!(fds, vec![4, 9]);
/// ```
#[inline]
pub fn fds(&self) -> Fds {
Copy link
Member

Choose a reason for hiding this comment

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

Typically a C programmer will know what the maximum file descriptor he registered was, and when inspecting the fd_set he won't need to iterate all the way to FD_SETSIZE, which can be up to 64k on some platforms. But as written your iterator will always iterate that high. That's not good for performance. What if you add an max: Option<RawFd> argument tofds? If supplied, the iterator won't check any higher than that. Such an argument could also be useful to highest.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added to fds(). I didn't add to highest() in the interest of not introducing unnecessary breakage. If someone wants to get that performance gain, they can use set.fds(Some(my_limit)).next_back(), which isn't terribly hard to write and is pretty clear.

pub fn contains(&mut self, fd: RawFd) -> bool {
unsafe { libc::FD_ISSET(fd, &mut self.0) }
pub fn contains(&self, fd: RawFd) -> bool {
let mut copy = self.0;
Copy link
Member

Choose a reason for hiding this comment

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

I think that for now we should proceed without waiting for rust-lang/libc#1725 . But copying the entire structure on each loop of the iterator is woefully inefficient. I think you should retain the &mut reference instead.

Copy link
Member

@asomers asomers left a comment

Choose a reason for hiding this comment

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

This is looking better. Please add some tests, too.

CHANGELOG.md Show resolved Hide resolved
src/sys/select.rs Outdated Show resolved Hide resolved
@asomers
Copy link
Member

asomers commented Apr 19, 2020

The CI failure was an intermittent network hiccup.

Copy link
Member

@asomers asomers left a comment

Choose a reason for hiding this comment

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

bors r+

@bors
Copy link
Contributor

bors bot commented Apr 19, 2020

Build succeeded:

@bors bors bot merged commit f94e882 into nix-rust:master Apr 19, 2020
@zombiezen zombiezen deleted the select-fd-iterator branch April 19, 2020 21:52
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.

None yet

2 participants