-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Performance reading from serial ports suffers due to excessive read() calls #5324
Comments
I said this in discord, but this cannot be fixed for our current This optimization relies on the fact that a partial read or write to a socket in a *nix OS which is a continuous byte stream (e.g TCP but not UDP) implies that the descriptor no longer has readiness for the IO direction in question as it could not fill the buffer. Unfortunately, we don't know if the file descriptor we are wrapping is a continuous stream. It could very well be something like a seqpacket or datagram socket, in which case partial reads and writes are to be expected in cases where there is still data in the buffer to be read. Furthermore, without specialization, I'm not sure that this is even possible given that our APIs are generic over the return type (it need only be I think the only way to do this would maybe be to add a public type which acts like the internal |
Hmm, lets imagine that we're writing a wrapper around
Would this work? (I understand that this would need to happen in the external library, rather than in Tokio.) |
That would work. |
My suggestion of another type was for if we want to add something which does this to make things easier on users. This feels like something that could potentially work fine in tokio-util. |
Perhaps we could add that. I think we should also ensure to document how to properly implement the optimization in the documentation for AsyncFd. |
I can go ahead and add this then, both to tokio as docs and to tokio-util as a wrapper type. |
Working on this now. There are two approaches we can take. The first approach would be to do this in @Darksonn what are your thoughts here? I personally really don't have much of a preference between the two options, although I do slightly lean towards tokio-util because we can add a few different types (including one that isn't AsyncRead/Write for non-streaming IO) and not be bound by the tokio stability guarantees. |
StreamedFd is a higher-level wrapper around an AsyncFd which provides the PollEvented optimization to users. Fixes #5324.
Version
Platform
Description
When reading from a serial port using AsyncFd in O_NONBLOCK mode, an extra read() call is performed:
One can assume that if the buffer didn't fill up, the following read won't succeed, so it should proceed directly to epoll_wait instead of attempting the read and having it fail. Note that on some serial devices, they will never return more than a smaller buffer (say 64) so I would encourage this optimization to only take place when a single character is read.
This was fixed for non-AsyncFd reads here:
tokio-rs PR-4970
tokio-rs PR-4840
The text was updated successfully, but these errors were encountered: