Skip to content

Refactor IOBuffer code #57570

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

Merged
merged 52 commits into from
Mar 28, 2025
Merged

Refactor IOBuffer code #57570

merged 52 commits into from
Mar 28, 2025

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

  • 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
  • Current behaviour for reset and position did not work for PipeBuffer. Fix that

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

# 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
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.

@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.

@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%

Co-authored-by: Jameson Nash <[email protected]>
@jakobnissen jakobnissen added merge me PR is reviewed. Merge when all tests are passing and removed status: waiting for author labels Mar 28, 2025
@jakobnissen
Copy link
Member Author

Test failures seem unrelated

@vtjnash vtjnash merged commit 5419713 into JuliaLang:master Mar 28, 2025
8 of 10 checks passed
@vtjnash vtjnash added the backport 1.12 Change should be backported to release-1.12 label Mar 28, 2025
KristofferC pushed a commit that referenced this pull request Mar 31, 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
* Do not corrupt data on `copyline` on a non-appending buffer
* Respect `maxsize` even after `take!` (fix #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
* Current behaviour for `reset` and `position` did not work for
`PipeBuffer`. Fix that

### 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
Co-authored-by: Jameson Nash <[email protected]>

(cherry picked from commit 5419713)
@KristofferC KristofferC mentioned this pull request Mar 31, 2025
36 tasks
KristofferC pushed a commit that referenced this pull request Mar 31, 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
* Do not corrupt data on `copyline` on a non-appending buffer
* Respect `maxsize` even after `take!` (fix #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
* Current behaviour for `reset` and `position` did not work for
`PipeBuffer`. Fix that

### 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
Co-authored-by: Jameson Nash <[email protected]>

(cherry picked from commit 5419713)
@KristofferC KristofferC mentioned this pull request Apr 4, 2025
51 tasks
@KristofferC KristofferC removed the backport 1.12 Change should be backported to release-1.12 label Apr 9, 2025
oscardssmith pushed a commit that referenced this pull request Apr 15, 2025
Fixes #57962

This reverts the changes to `skip` added in #57570

The code before #57570 was written in a very specific way to avoid
overflow errors, so I'm not sure why this was changed.
KristofferC pushed a commit that referenced this pull request Apr 16, 2025
Fixes #57962

This reverts the changes to `skip` added in #57570

The code before #57570 was written in a very specific way to avoid
overflow errors, so I'm not sure why this was changed.

(cherry picked from commit b29c808)
KristofferC pushed a commit that referenced this pull request Apr 16, 2025
Fixes #57962

This reverts the changes to `skip` added in #57570

The code before #57570 was written in a very specific way to avoid
overflow errors, so I'm not sure why this was changed.

(cherry picked from commit b29c808)
@DilumAluthge DilumAluthge removed the merge me PR is reviewed. Merge when all tests are passing label Apr 29, 2025
@jakobnissen jakobnissen deleted the iobuffer branch April 30, 2025 08:06
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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