Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/wasi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ jobs:
# caret diagnostics; the host test crate does have them, hence the
# not(wasi_runner) in the cfg guarding every `mod diagnostics`.
# TODO: add integration tests for these tools as WASI support is extended:
# csplit ls rm seq sort
# csplit ls rm seq
# nproc is not part of the default feature set, so it needs to be
# requested explicitly for the host test crate to compile its tests.
UUTESTS_BINARY_PATH="$(pwd)/target/${{ matrix.job.target }}/debug/coreutils.wasm" \
Expand All @@ -92,7 +92,7 @@ jobs:
test_paste:: test_pathchk:: test_pr:: test_printenv:: test_printf:: test_ptx:: \
test_pwd:: test_readlink:: test_realpath:: test_rmdir:: test_sha1sum:: test_sha224sum:: test_sha256sum:: \
test_sha384sum:: test_sha512sum:: test_shred:: test_shuf:: test_sleep:: \
test_sort::sort_multiple test_sort::test_check test_split:: test_sum:: test_tail:: test_tee:: \
test_sort:: test_split:: test_sum:: test_tail:: test_tee:: \
test_test:: test_touch:: test_tr:: test_true:: test_truncate:: test_tsort:: test_uname:: \
test_uniq:: test_unexpand:: test_unlink:: test_vdir:: test_wc:: test_yes::

Expand Down
19 changes: 10 additions & 9 deletions src/uu/sort/src/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,22 +163,23 @@ pub fn check(path: &OsStr, settings: &GlobalSettings) -> UResult<()> {

let mut file = open(path)?;
let mut carry_over = vec![];
let (sender, receiver) = std::sync::mpsc::sync_channel(1);
let mut prev_chunk: Option<Chunk> = None;
let mut line_idx = 0;
// Reuse the previous chunk's buffer for the next read instead of allocating a
// fresh one each time, once it is no longer needed for the disorder comparison.
let mut recycled_chunk = None;
loop {
let recycled = RecycledChunk::new(buffer_size(settings));
let should_continue = chunks::read(
&sender,
recycled,
None,
let chunk_buffer = recycled_chunk
.take()
.unwrap_or_else(|| RecycledChunk::new(buffer_size(settings)));
let (chunk, should_continue) = chunks::read_once(
chunk_buffer,
&mut carry_over,
&mut file,
&mut std::iter::empty(),
settings.line_ending.into(),
settings,
)?;
while let Ok(chunk) = receiver.try_recv() {
if let Some(chunk) = chunk {
check_chunk(
path,
settings,
Expand All @@ -187,7 +188,7 @@ pub fn check(path: &OsStr, settings: &GlobalSettings) -> UResult<()> {
&chunk,
&mut line_idx,
)?;
prev_chunk = Some(chunk);
recycled_chunk = prev_chunk.replace(chunk).map(Chunk::recycle);
}
if !should_continue {
break;
Expand Down
27 changes: 27 additions & 0 deletions src/uu/sort/src/chunks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,33 @@ pub fn read<T: Read>(
Ok(should_continue)
}

/// Read a single chunk synchronously, without a background reader thread.
///
/// WASI has no threads, so callers that would otherwise hand `recycled_chunk` to a reader
/// thread call this instead to read the next chunk directly on the current thread. `read`
/// sends at most one chunk per call, so a throwaway channel is enough to receive it.
#[cfg(target_os = "wasi")]
pub fn read_once<T: Read>(
recycled_chunk: RecycledChunk,
carry_over: &mut Vec<u8>,
file: &mut T,
separator: u8,
settings: &GlobalSettings,
) -> UResult<(Option<Chunk>, bool)> {
let (sender, receiver) = std::sync::mpsc::sync_channel(1);
let should_continue = read(
&sender,
recycled_chunk,
None,
carry_over,
file,
&mut std::iter::empty(),
separator,
settings,
)?;
Ok((receiver.try_recv().ok(), should_continue))
}

/// Split `read` into `Line`s, and add them to `lines`.
fn parse_lines<'a>(
read: &'a [u8],
Expand Down
Loading
Loading