-
-
Notifications
You must be signed in to change notification settings - Fork 2k
sort: add complete no-thread WASI fallback #13806
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
Open
DePasqualeOrg
wants to merge
7
commits into
uutils:main
Choose a base branch
from
DePasqualeOrg:codex/wasi-sort-no-threads
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4f7c3ac
sort: add complete no-thread WASI fallback
DePasqualeOrg 245caf6
sort: detect threaded WASI target
DePasqualeOrg d4f660f
sort: propagate temporary write errors
DePasqualeOrg 930b6bb
sort: propagate final merge flush errors
DePasqualeOrg 6b7b5db
sort: truncate output for empty input
DePasqualeOrg 1855076
sort: revert empty-output truncation
DePasqualeOrg b9f815c
sort: restore carry-over allocation comment
DePasqualeOrg 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
There are no files selected for viewing
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,12 @@ | ||
| fn main() { | ||
| // Set a short alias for the WASI-without-threads configuration so that | ||
| // source files can use `#[cfg(wasi_no_threads)]`. | ||
| println!("cargo::rustc-check-cfg=cfg(wasi_no_threads)"); | ||
|
|
||
| let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap_or_default(); | ||
| let target = std::env::var("TARGET").unwrap_or_default(); | ||
|
|
||
| if target_os == "wasi" && target != "wasm32-wasip1-threads" { | ||
| println!("cargo::rustc-cfg=wasi_no_threads"); | ||
| } | ||
| } | ||
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,44 @@ | ||
| // This file is part of the uutils coreutils package. | ||
| // | ||
| // For the full copyright and license information, please view the LICENSE | ||
| // file that was distributed with this source code. | ||
|
|
||
| //! Check if a file is ordered. | ||
| //! | ||
| //! On most platforms this uses a multi-threaded reader. On WASI without | ||
| //! atomics, a synchronous variant is used instead. The two implementations | ||
| //! live in sibling modules and are selected via cfg at the module boundary. | ||
|
|
||
| use std::cmp::Ordering; | ||
| use std::ffi::OsStr; | ||
|
|
||
| use uucore::error::UResult; | ||
|
|
||
| use crate::{GlobalSettings, open}; | ||
|
|
||
| #[cfg(not(wasi_no_threads))] | ||
| mod threaded; | ||
| #[cfg(not(wasi_no_threads))] | ||
| use threaded as runner; | ||
|
|
||
| #[cfg(wasi_no_threads)] | ||
| mod sync; | ||
| #[cfg(wasi_no_threads)] | ||
| use sync as runner; | ||
|
|
||
| /// Check if the file at `path` is ordered. | ||
| pub fn check(path: &OsStr, settings: &GlobalSettings) -> UResult<()> { | ||
| let max_allowed_cmp = if settings.unique { | ||
| Ordering::Less | ||
| } else { | ||
| Ordering::Equal | ||
| }; | ||
| let file = open(path)?; | ||
| let chunk_size = if settings.buffer_size < 100 * 1024 { | ||
| settings.buffer_size | ||
| } else { | ||
| 100 * 1024 | ||
| }; | ||
|
|
||
| runner::check(path, settings, max_allowed_cmp, file, chunk_size) | ||
| } |
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,98 @@ | ||
| // This file is part of the uutils coreutils package. | ||
| // | ||
| // For the full copyright and license information, please view the LICENSE | ||
| // file that was distributed with this source code. | ||
|
|
||
| //! Synchronous ordered-file check for targets without thread support. | ||
|
|
||
| use std::cmp::Ordering; | ||
| use std::ffi::OsStr; | ||
| use std::io::Read; | ||
| use std::iter; | ||
|
|
||
| use itertools::Itertools; | ||
| use uucore::error::UResult; | ||
|
|
||
| use crate::{ | ||
| GlobalSettings, SortError, | ||
| chunks::{self, Chunk, RecycledChunk}, | ||
| compare_by, | ||
| }; | ||
|
|
||
| pub(super) fn check( | ||
| path: &OsStr, | ||
| settings: &GlobalSettings, | ||
| max_allowed_cmp: Ordering, | ||
| mut file: Box<dyn Read + Send>, | ||
| chunk_size: usize, | ||
| ) -> UResult<()> { | ||
| let separator = settings.line_ending.into(); | ||
| let mut carry_over = vec![]; | ||
| let mut prev_chunk: Option<Chunk> = None; | ||
| let mut spare_recycled: Option<RecycledChunk> = None; | ||
| let mut line_idx = 0; | ||
|
|
||
| loop { | ||
| let recycled = spare_recycled | ||
| .take() | ||
| .unwrap_or_else(|| RecycledChunk::new(chunk_size)); | ||
|
|
||
| let (chunk, should_continue) = chunks::read_to_chunk( | ||
| recycled, | ||
| None, | ||
| &mut carry_over, | ||
| &mut file, | ||
| &mut iter::empty(), | ||
| separator, | ||
| settings, | ||
| )?; | ||
|
|
||
| let Some(chunk) = chunk else { | ||
| break; | ||
| }; | ||
|
|
||
| line_idx += 1; | ||
| if let Some(prev) = prev_chunk.take() { | ||
| let prev_last = prev.lines().last().unwrap(); | ||
| let new_first = chunk.lines().first().unwrap(); | ||
|
|
||
| if compare_by( | ||
| prev_last, | ||
| new_first, | ||
| settings, | ||
| prev.line_data(), | ||
| chunk.line_data(), | ||
| ) > max_allowed_cmp | ||
| { | ||
| return Err(SortError::Disorder { | ||
| file: path.to_owned(), | ||
| line_number: line_idx, | ||
| line: String::from_utf8_lossy(new_first.line).into_owned(), | ||
| silent: settings.check_silent, | ||
| } | ||
| .into()); | ||
| } | ||
| spare_recycled = Some(prev.recycle()); | ||
| } | ||
|
|
||
| for (a, b) in chunk.lines().iter().tuple_windows() { | ||
| line_idx += 1; | ||
| if compare_by(a, b, settings, chunk.line_data(), chunk.line_data()) > max_allowed_cmp { | ||
| return Err(SortError::Disorder { | ||
| file: path.to_owned(), | ||
| line_number: line_idx, | ||
| line: String::from_utf8_lossy(b.line).into_owned(), | ||
| silent: settings.check_silent, | ||
| } | ||
| .into()); | ||
| } | ||
| } | ||
|
|
||
| prev_chunk = Some(chunk); | ||
|
|
||
| if !should_continue { | ||
| break; | ||
| } | ||
| } | ||
| Ok(()) | ||
| } |
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.