-
-
Notifications
You must be signed in to change notification settings - Fork 7k
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
Use InputStreamReader for serial UTF8 decoder #10389
Draft
cmaglie
wants to merge
3
commits into
arduino:master
Choose a base branch
from
cmaglie:improve_serial_utf8_decoder
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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.
Do we even need to limit to 128 chars here? Sems lie decoderInRaw has no fixed buffer size, so maybe you can just push all of buf in there? If this has performance issues (e.g. buf is a lot bigger than 128 so memory usages increases or something), then maybe the incoming
buf
should be limited?It should be fairly easy to just read from the serial port in 128-byte blocks, moving the loop here:
Arduino/arduino-core/src/processing/app/Serial.java
Lines 178 to 179 in 2162966
(the argument to
getBytes()
is the number of bytes to read.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 PipedInputStream has a fixed circular buffer size, if exceeded the
decoderInRaw.write(...)
call will block forever (unless we spawn a thread that reads concurrently fromdecoderOutputUTF8
)... but I would like to avoid complicating it again.We can increase the buffer size to something bigger like 16Kb to reduce the function call pressure, I'll push a change for this.
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.
Ah, I see. I can't find the default size anywhere, though, so hardcoding 128 might not be ideal. Passing an explicit buffer size would be better (probably through a
BUFFER_SIZE
constant or something). The buffer size should probably be a few bytes bigger than the max write size, in case some bytes are left in the buffer. A unittest for this would be to write a partial utf-8 character and then a full buffer worth of bytes.Wouldn't moving the loop only slightly simplify things? It could result in more function calls and
chars
allocations maybe, but the latter could easily be an instance variable if this is an issue.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.
yep look my last commit.
I don't know, adding a thread would already increase the number of LOC, also you need to figure out how to synchronize it. I'm not going into that path... :-) if you want to give it a try I may wait for it before merging this one.
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 main problem is that JSSC doesn't implement the
InputStream
interface, otherwise we could have chained it to the InputStreamReader directly, without the need of this buffering. Unfortunately it provides this weirdgetBytes()
method, so we cannot avoid this boilerplate...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 wasn't suggesting adding a thread, just let
serialEvent
loop and read BUFFER_SIZE bytes at a time (untilserialEvent.getEventValue()
have been read) and callingprocessSerialEvent
for each buffer just as now. This would be very similar, except that it ensures that thebuf
variable is also limited toBUFFER_SIZE
.I thought about this again, and I think this is not actually needed. The
InputStreamReader
does the decoding and the buffer is in thePipedInputStream
. Looking at theInputStream
interface, there is no way to peek at bytes or re-insert them after reading them, so the only way forInputStreamReader
to decide there are no additional characters is to read all of them, so I suppose there is a tiny buffer inInputStreamReader
for partial characters?Docs for
InputStreamReader
also suggest that there is some internal buffering:Loooking at docs for
Reader.read(char[])
, so also InputStreamReader, I see:I wonder what this means exactly. Two risks:
processSerialEvent
was called with a partial utf-8 character, or 16kbyte + a partial utf-8 character), this might block and deadlock?InputStream
(untilchars
is full)? If it could read less, leaving some bytes in the pipe, you'd also get a deadlock.Problem 1 does indeed seem to occur. This program deadlocks for me:
The docs suggest that ready()` can be used to check this:
But looking at the
StreamDecoder.inReady()
source, which is used byInputStreamReader.ready()
, this actually returns true when theInputStream
has any bytes available, rather than actually pre-reading bytes to see if there is anything actually available. And indeed,ready()
returns true, and then reading blocks...InputStreamReader.ready()
does specify what it does correctly (return true when bytes are available), but then still (incorrectly) promises it won't block.So I wonder if
InputStreamReader
is useful at all if we cannot guarantee it never blocks (and stop calling read()` when there's < 4 bytes in the buffer would prevent blocking, but also prevent the last few bytes before a pause from being read...).Yeah, indeed. I wonder: Wouldn't a simple wrapper be possible here to allow removing the pipe stuff and simplifying the chain? I think this could remove one buffer from the chain. With this PR, we have:
SerialPort.readBytes
allocates a buffer, which we store inbuf
.InputStreamReader
, into its internal bufferchars
buffer.With a wrapper, you could do:
SerialPort.readBytes
allocates a buffer, which we store in an internal buffer in the wrapper.read()
method is called byInputStreamReader
(passing, ISR's internal buffer array), data is copied to ISR's buffer.chars
buffer.Note that the current code also uses 4 buffers (
buf
, aByteBuffer
aCharBuffer
andchars
, but the latter could be removed, see below).I had a bit of a look at how this would look, but given the deadlock problem above must be solved first, I stopped doing so :-)
Seems we also can't fix this by making a non-blocking
InputStream
to wrapInputStreamReader
, since that triggers an exception: http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/sun/nio/cs/StreamDecoder.java#l288Looking at the core implementation of
InputStreamReader
(which defers toStreamDecoder.implRead()
, it seems they essentially do the same trick as our original code (Write to aByteBuffer
, feed that into aCharsetDecoder
, which puts the result in aCharBuffer
).Looking at this PR now, it is indeed simpler code, but adds one buffer in the chain, adds several layers of extra objects (pipes, InputStreamReader, StreamDecoder) and on top of that, seems to deadlock.
So, maybe the original approach should be kept, maybe it can be a bit more simplified by reading smaller chunks from the SerialPort? Also, looking at the code again, I think the
chars
array buffer could be removed as well, by just usingCharBuffer.array()
to access that internal buffer directly.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.
...and this is a problem indeed, if there is no easy solution I guess we must drop this PR.
Yes, but besides that, my concern was the error in #8903 that I've got also on my Windows VM after upgrading to the JDK 8u242. Using an
InputStreamReader
seemed like the best approach, but I guess we need to find another way.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.
Hm, interesting, especially since the
InputStreamReader
seems to useByteBuffer
internally too. Any idea what changed about thisflip()
to cause this? I can't find anything relevant in the docs, e.g. these all look the same: