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

kevent() requires a Vec for its event list, when instead it should probably take a slice #1043

Open
yorickpeterse opened this issue Apr 5, 2024 · 2 comments

Comments

@yorickpeterse
Copy link

The wrapper for kevent() is defined as follows:

pub unsafe fn kevent(
    kqueue: impl AsFd,
    changelist: &[Event],
    eventlist: &mut Vec<Event>,
    timeout: Option<Duration>,
) -> io::Result<usize>

Here the eventlist argument is defined as &mut Vec<Event>, requiring a heap allocating of a Vec on every call. For a system where file descriptors are frequently (un)registered (e.g. a busy socket server), it's not unlikely for the Vec allocations to become a bit of a problem.

Would it be possible to change the signature such that eventlist is a &mut [Event] instead? This way one can simply stack allocate the slice, and still use a Vec if deemed necessary (e.g. one is expecting many events).

Some of this was discussed in #578, but it seems nothing was done in response.

@yorickpeterse
Copy link
Author

For extra context: I'm porting the network poller setup for https://github.com/inko-lang/inko from the "polling" crate back to direct usage of epoll/kqueue/etc (inko-lang/inko#344 (comment) for more details as to why). Back in ye olde days I used the "nix" crate to interact with kqueue (https://github.com/inko-lang/inko/blob/87b36927d7bd3194e9da90e406f5206d8c713636/vm/src/network_poller/kqueue.rs). Given that I'm already using the rustix crate and long since stopped using the "nix" crate, I'd like to be able to use rustix for this as well, but using a stack allocated array as the old network polling code did.

yorickpeterse added a commit to yorickpeterse/rustix that referenced this issue Apr 5, 2024
This removes the need for dynamically allocating a Vec when the kevent()
function is called. The documentation is also rephrased slightly as
slices don't really have a "capacity", but only a fixed-size length.

This fixes bytecodealliance#1043.
yorickpeterse added a commit to yorickpeterse/rustix that referenced this issue Apr 5, 2024
This removes the need for dynamically allocating a Vec when the kevent()
function is called. The documentation is also rephrased slightly as
slices don't really have a "capacity", but only a fixed-size length.

This fixes bytecodealliance#1043.
@oxalica
Copy link
Contributor

oxalica commented Jul 22, 2024

I suggest to follow the API of read_uninit and define fn kevent_uninit(&mut [MaybeUninit<Event>], ..) -> Result<(&mut [Event], &mut [MaybeUninit<Event>])>. So we can keep the ability to use uninitialized memory without requiring Vec.

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

No branches or pull requests

2 participants