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

IO.copy_stream fixes #6557

Merged
merged 3 commits into from Feb 12, 2021
Merged

IO.copy_stream fixes #6557

merged 3 commits into from Feb 12, 2021

Conversation

headius
Copy link
Member

@headius headius commented Feb 12, 2021

  • Avoid copying the source bytes if the source ByteBuffer has an accessible array.
  • Update the source ByteBuffer position with the number of bytes the write call reports as written.
  • Only loop on writes when the target is a real IO.

The last item fixes #6555.

* Don't copy the source bytes when we can access an underlying
  array.
* Advance the incoming buffer the number of bytes that were
  written.
The CRuby logic here will use binwrite when the target has a real
fd associated with it, which loops until everything is written,
and does a single dynamic "write" call when the target is a
pseudo-IO. This patch simulates this behavior by bailing out after
as single write when the target channel is wrapping a pseudo-IO.

Fixes jruby#6555
@headius headius added this to the JRuby 9.2.15.0 milestone Feb 12, 2021
@headius headius changed the base branch from master to jruby-9.2 February 12, 2021 04:52
When the incoming bytes are on the heap, we can use them in-place
for the string we pass to the Ruby write method. However, since
the target IO-like may want to modify the incoming string, we must
make sure it is marked as shared so our original src bytes will
not be modified.

This issue originally manifested in jruby#4903 and was fixed by always
copying the incoming buffer, but marking the string shared has the
same effecit if the target attempts to make any modifications.
@headius headius merged commit 2c3c16f into jruby:jruby-9.2 Feb 12, 2021
@headius headius deleted the copy_stream_fixes branch February 12, 2021 05:31
headius added a commit to headius/jruby that referenced this pull request Aug 5, 2021
The "optimizations" here were overzealous:

* The incoming ByteBuffer and its contents should not be
  referenced past the end of the call, as was done here by
  returning a String that directly wraps the buffer's underlying
  array.
* The copy of bytes out of the native buffer is essentially the
  same as calling #duplicate, so this is just unnecessary double-
  copying.

Fixes jruby#6767.
headius added a commit that referenced this pull request Aug 5, 2021
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.

IO.copy_stream double-writes to InternetMessageIO
1 participant