-
Notifications
You must be signed in to change notification settings - Fork 5k
Streams: one PipeReader loop with owned chunks, hold-not-adopt buffer pins, right-sized native pulls #38886
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
Streams: one PipeReader loop with owned chunks, hold-not-adopt buffer pins, right-sized native pulls #38886
Changes from 6 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
720b4b3
PipeReader: keep the outer read loop's scratch claim when a nested re…
robobun b34f8d0
PipeReader: make the per-loop read scratch a claim on its owner
Jarred-Sumner 281ac60
PipeReadScratch: thin Option<Box<[_; N]>> instead of a boxed slice
Jarred-Sumner 944b574
PipeReadScratch: guard holds raw pointers, zeroed buffer
Jarred-Sumner 3433c6a
readFile: cap the pre-stat read at max_size
Jarred-Sumner a8fb11f
PipeReadScratch: interior-mutable state so a refused nested claim nev…
Jarred-Sumner 76845dd
PipeReadScratch: guard is a shared borrow of the owner
Jarred-Sumner 48c2d44
html-rewriter test: pace the locked-reader test by its reads, not set…
Jarred-Sumner 8587b96
VirtualMachine: project the pipe-read scratch through a raw RareData …
Jarred-Sumner 3ba5cdd
PipeReader: one read loop that tells consumers who owns each chunk
Jarred-Sumner 465f179
PipeReader: keep the uv buffer range check type-checked in release
Jarred-Sumner e424871
PipeReader: close before delivering the final chunk so a nested pull …
Jarred-Sumner ed8135e
shell-pipe-read-fault: the read loop now delivers the bytes read befo…
Jarred-Sumner 46a521f
PipeReader: read_into reports Progress on a hung-up pipe until the 0-…
Jarred-Sumner da7fe6d
restore bun.lock
Jarred-Sumner a3a8bae
[autofix.ci] apply automated fixes
autofix-ci[bot] 626627b
Hold, don't adopt, a bufferless typed array pinned for a threadpool o…
Jarred-Sumner f7b9e5b
native ReadableStream pull: size the slab to what the source delivers
Jarred-Sumner 9bc9e86
pin: report what was pinned instead of tracking held views in a table
Jarred-Sumner ec3c707
[autofix.ci] apply automated fixes
autofix-ci[bot] 4d732b6
tests: pin contract is per storage kind now; pull chunks are right-si…
Jarred-Sumner a095452
FileReader (Windows): stop the completion-driven reader at the highwa…
Jarred-Sumner 6d64efb
html-rewriter test: allow the one in-flight completion on Windows in …
Jarred-Sumner 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| use core::cell::{Cell, UnsafeCell}; | ||
| use core::ops::{Deref, DerefMut}; | ||
| use core::ptr::NonNull; | ||
|
|
||
| pub const PIPE_READ_BUFFER_SIZE: usize = 256 * 1024; | ||
| type PipeReadBuffer = [u8; PIPE_READ_BUFFER_SIZE]; | ||
|
|
||
| #[derive(Clone, Copy, PartialEq, Eq)] | ||
| enum State { | ||
| Available, | ||
| Used, | ||
| } | ||
|
|
||
| /// Per-loop scratch for blocking pipe/file reads. Chunks are delivered straight out of it, and a consumer may run user code that starts a nested read while still parsing the chunk, so only one borrower on the thread may hold it at a time. Interior-mutable so a refused nested `claim` never forms a `&mut` over the outer guard's pointers. | ||
| pub struct PipeReadScratch { | ||
| state: Cell<State>, | ||
| buffer: UnsafeCell<Option<Box<PipeReadBuffer>>>, | ||
| } | ||
|
|
||
| impl PipeReadScratch { | ||
| pub const fn new() -> Self { | ||
| Self { | ||
| state: Cell::new(State::Available), | ||
| buffer: UnsafeCell::new(None), | ||
| } | ||
| } | ||
|
|
||
| /// `None` while a borrower further up the stack still holds the guard. | ||
| pub fn claim(&self) -> Option<PipeReadScratchGuard> { | ||
| if self.state.replace(State::Used) == State::Used { | ||
| return None; | ||
| } | ||
| // SAFETY: `state` was just moved to `Used`, so no guard (the only other accessor of `buffer`) exists; the borrow ends at `;`. | ||
| let buffer = unsafe { &mut *self.buffer.get() }.get_or_insert_with(bun_core::boxed_zeroed); | ||
| Some(PipeReadScratchGuard { | ||
| buffer: NonNull::from(&mut buffer[..]), | ||
| state: NonNull::from(&self.state), | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| impl Default for PipeReadScratch { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
|
|
||
| /// Exclusive claim on the scratch; released on drop. The owner (VM rare data / mini loop) outlives every guard. | ||
| pub struct PipeReadScratchGuard { | ||
| buffer: NonNull<[u8]>, | ||
| state: NonNull<Cell<State>>, | ||
| } | ||
|
|
||
| impl Deref for PipeReadScratchGuard { | ||
| type Target = [u8]; | ||
| #[inline] | ||
| fn deref(&self) -> &[u8] { | ||
| // SAFETY: the buffer is a separate heap allocation only reachable through this guard while `state` is `Used`. | ||
| unsafe { self.buffer.as_ref() } | ||
| } | ||
| } | ||
|
|
||
| impl DerefMut for PipeReadScratchGuard { | ||
| #[inline] | ||
| fn deref_mut(&mut self) -> &mut [u8] { | ||
| // SAFETY: as in `deref`. | ||
| unsafe { self.buffer.as_mut() } | ||
| } | ||
| } | ||
|
|
||
| impl Drop for PipeReadScratchGuard { | ||
| fn drop(&mut self) { | ||
| // SAFETY: the owner outlives the guard; `Cell` needs only a shared ref. | ||
| unsafe { self.state.as_ref() }.set(State::Available); | ||
| } | ||
| } | ||
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.