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

Refactor IOBuffer code #57570

Open
wants to merge 34 commits into
base: master
Choose a base branch
from
Open

Conversation

jakobnissen
Copy link
Member

@jakobnissen jakobnissen commented Feb 28, 2025

I've been a little frustrated with the IOBuffer code. It contains a whole bunch of implicit invariants, and is poorly commented. It also has several bugs that ultimately stems from the code being unclear about its own assumptions.

This is a refactoring of IOBuffer. The primary goals are:

  • Comment the code more heavily
  • Test the code more thoroughly

The secondary goals are

  • Fix a few outstanding bugs
  • Add some minor performance improvements

This is a purely internal refactoring with be no change in behaviour of IOBuffer, except straight up bugfixes. However, note that previous code may have relied on buggy behaviour. Fixing bugs may therefore cause breakage.

Current changes

BEHAVIOUR CHANGES

  • The following code used to not throw an error, but now does: IOBuffer(b"abc"; maxsize=2). I consider this a bugfix. It should not be possible to construct an IOBuffer with a buffersize larger than maxsize.
  • It used to be possible to write to indices higher than maxindex, which could trigger a bug causing data loss. The bug has been fixed, but as a result, some IOBuffers may reach full capacity faster (really: reach it at the correct point), changing writing behaviour.

Bugfixes

  • Allow reading from an IOBuffer backed by non-dense memory.
  • Invalidate non-dense memory buffer after take!
  • Do not corrupt data on copyline on a non-appending buffer
  • Respect maxsize even after take! (fix IOBuffer does not respect maxsize after the first take! #57549)
  • Fix bug when copying from an appending iobuffer to itself
  • Fix bug where re-allocating the buffer may cause it to shrink, discarding data.
  • Fix bug where truncate may throw a wrong BoundsError
  • Fix a bug where truncating a buffer may not correctly removed mark at position that has been deleted
  • Fix a bug where initializing an IOBuffer without an explicit buffer and with truncate=false makes it contain the full buffer

Changes to brittle code

  • Removed some tests that explicitly tested internal code and internal behaviour. Some of that behaviour has changed.
  • Changed some internal PipeBuffer behaviour, which did not respect writable IOBuffer's ownership of their buffer and therefore failed spuriously

Performance improvements

  • Writing to dense IOBuffer now uses memmove and is up to 10x faster for long writes.
  • Minor optimisations (about ten percent) for writing to IOBuffers in general

Closes #57549

base/iobuffer.jl Outdated
@@ -329,6 +404,7 @@ function _resize!(io::GenericIOBuffer, sz::Int)
a = io.data
offset = io.offset
if applicable(resize!, a, sz)
# TODO: This is buggy: The buffer should never touch data before the offset.
Copy link
Member

Choose a reason for hiding this comment

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

This seems like you're claiming something as a bug which was never part of the contract. When using an IOBuffer with writing (especially if a resize is needed), it is allowed to destroy the underlying object to make more space

Copy link
Member Author

Choose a reason for hiding this comment

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

That is good to know - adding the comments is precisely to learn what is part of the contract! Okay, so the IOBuffer takes ownership of its underlying array - and, in the case of being passed a Vector, it then transitively takes control of the underlying Memory.

base/iobuffer.jl Outdated

# Data is read/written from/to ptr, except in situations where append is true, in which case
# data is still read from ptr, but written to size+1.
# This value is alwaus in offset+1 : size+1
Copy link
Member

Choose a reason for hiding this comment

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

This is actually a bug in the current implementation. The ptr is not supposed to be restricted to be inside the file size, and any calls to write that happen should then automatically zero-pad the file from size:ptr (https://pubs.opengroup.org/onlinepubs/009695399/functions/lseek.html)

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm how does that work? Would the user have to seek first to beyond size? That doesn't correspond to how seek normally works in Julia: Normally, you can't seek beyond the filesize. Should that be allowed? Then what happens if the user seeks beyond lastindex(buffer.data)?

Copy link
Member Author

@jakobnissen jakobnissen Feb 28, 2025

Choose a reason for hiding this comment

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

How is it supposed to work with IOStream? What file should the following script create?

f = open("/tmp/foo", "w+")
write(f, "abcd")
flush(f)
seek(f, 10)
write(f, 'a')
flush(f)
close(f)

@jakobnissen jakobnissen force-pushed the iobuffer branch 2 times, most recently from 21008ca to 7274c10 Compare February 28, 2025 20:25
@nsajko nsajko added the io Involving the I/O subsystem: libuv, read, write, etc. label Mar 2, 2025
@jakobnissen jakobnissen marked this pull request as ready for review March 3, 2025 08:41
@jakobnissen jakobnissen added the status: waiting for PR reviewer PR is complete and seems ready to merge. Has tests and news/compat if needed. CI failures unrelated. label Mar 3, 2025
@jakobnissen
Copy link
Member Author

jakobnissen commented Mar 3, 2025

Let's stop the PR here - tests pass, the most pressing issues has been addressed, and it's already a fairly large (too large?) PR. I'll make a followup PR with some more tests and a few more optimisations and bugfixes after this is merged and I get code coverage.

@jakobnissen jakobnissen changed the title WIP: Refactor IOBuffer code Refactor IOBuffer code Mar 3, 2025
@jakobnissen
Copy link
Member Author

Adding @nhz2 because you've been a thorough reviewer on previous IO related PRs. Feel free to unassign yourself.

base/iobuffer.jl Outdated
buf.size = length(data) + offset
buf.offset = offset
if !iszero(offset)
unsafe_copyto!(mem, 1, mem, offset+1, len)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it is safe for src and dst to alias here, but from what I can tell the alias behavior of unsafe_copyto! is not documented.

Copy link
Member Author

Choose a reason for hiding this comment

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

It's safe, yes. Not documented, but I think that may be beyond the scope of this PR. Except if you suggest handling it another way?

# Data is read/written from/to ptr, except in situations where append is true, in which case
# data is still read from ptr, but written to size+1.
# This value is always in 1 : size+1
ptr::Int
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we could make this a MemoryRef If possible, doing so might save a few cycles.

Copy link
Member Author

Choose a reason for hiding this comment

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

That can't be done, because a MemoryRef can't point to one past the end, which ptr needs to do to signal EOF

Copy link
Member

Choose a reason for hiding this comment

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

Oscar seems probably right that we could replace the Memory+offset fields with a single MemoryRef field, since that may express almost the same concept

Copy link
Member Author

Choose a reason for hiding this comment

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

That would require a new struct which is not generic over the array type. Otherwise I can try to give it a go.

@vtjnash
Copy link
Member

vtjnash commented Mar 3, 2025

The removal of offset is a breaking change and performance regression. That probably shouldn't be mixed in with a PR that claims to be just bug fixes and refactoring for clarity

@jakobnissen
Copy link
Member Author

jakobnissen commented Mar 4, 2025

I would argue against it being a performance regression, but surely, it's not a breaking change. How is it breaking to remove a non-documented field of a struct? One which was added only in the current release? Which documented behaviour does that break?

Edit: I see it now. The documentation says that the buffer only controls any input array if write is true.


# Position is zero-indexed, but ptr is one-indexed, hence the -1
# TODO: Document that position for an unseekable stream is invalid, or
# make it error
position(io::GenericIOBuffer) = io.ptr - io.offset - 1

Choose a reason for hiding this comment

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

My vote here is for an error

Copy link
Contributor

Choose a reason for hiding this comment

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

mark returns the "marked position" of the stream, so erroring will also make mark error.

@jakobnissen
Copy link
Member Author

Thanks for the review. All comments has now been addressed (except the one about endianness which I don't understand)

@jakobnissen
Copy link
Member Author

The build failure (segfault) on 32-bit linux looks unrelated.

@jakobnissen
Copy link
Member Author

Bump. This is done now - a few people have reviewed it, but none have approved. It would be nice to get this in early in 1.13's cycle, so any bugs are detected early. Once this PR has landed, and #57668 has been fixed, I can make a followup PR to get test coverage closer to 100%

This allows e.g. reading from an IOBuffer backed by a UnitRange or whatever.
* For non-memory backed buffers, do not use invalid inbounds.
* For memory-backed buffers, use unsafe_copyto!, which is much faster
Use the existing implementation of in directly with the buffer. This should
already be fast
This fixes a bug introduced somewhere earlier in this chain of commits. However,
the previous behaviour wasn't great either.
In the changed code, correctness relied on the buffer having very specific
resizing behaviour, i.e. a mercurial implementation detail. The current solution
is more robust. However:
1. This workaround copies the data one extra time, which is not great
2. It's possible that the bug surfaced here due to another bug in the resizing
   behaviour. However, if so, I haven't been able to find it.
* Outline slow path in ensureoom.
* Do not uses memcpy when writing short arrays
This is due to some sketchy behaviour by some other code which relies on IOBuffer
internals, but shouldn't.
Before Julia 1.11, observing uninit bitstypes was UB. Now, it's no longer UB,
so no need.
Also test the behaviour that depends on this field
Only copy the used bytes, not unreachable bytes.
* Remove unsafe trait objects, to avoid adding private methods to public
  functions
* Remove a superfluous style change
@jakobnissen jakobnissen added status: waiting for author Anybody home? and removed status: waiting for PR reviewer PR is complete and seems ready to merge. Has tests and news/compat if needed. CI failures unrelated. labels Mar 21, 2025
Since it needs to write all its content, this would cause an infinite loop,
exhausing all memory. Throw an error instead.
This makes the method write(::IOBuffer, ::IOBuffer) work like the generic
fallback.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
io Involving the I/O subsystem: libuv, read, write, etc. status: waiting for author Anybody home?
Projects
None yet
Development

Successfully merging this pull request may close these issues.

IOBuffer does not respect maxsize after the first take!
7 participants