mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 17:25:15 +02:00
Replace ExternalStream
with new ByteStream
type (#12774)
# Description This PR introduces a `ByteStream` type which is a `Read`-able stream of bytes. Internally, it has an enum over three different byte stream sources: ```rust pub enum ByteStreamSource { Read(Box<dyn Read + Send + 'static>), File(File), Child(ChildProcess), } ``` This is in comparison to the current `RawStream` type, which is an `Iterator<Item = Vec<u8>>` and has to allocate for each read chunk. Currently, `PipelineData::ExternalStream` serves a weird dual role where it is either external command output or a wrapper around `RawStream`. `ByteStream` makes this distinction more clear (via `ByteStreamSource`) and replaces `PipelineData::ExternalStream` in this PR: ```rust pub enum PipelineData { Empty, Value(Value, Option<PipelineMetadata>), ListStream(ListStream, Option<PipelineMetadata>), ByteStream(ByteStream, Option<PipelineMetadata>), } ``` The PR is relatively large, but a decent amount of it is just repetitive changes. This PR fixes #7017, fixes #10763, and fixes #12369. This PR also improves performance when piping external commands. Nushell should, in most cases, have competitive pipeline throughput compared to, e.g., bash. | Command | Before (MB/s) | After (MB/s) | Bash (MB/s) | | -------------------------------------------------- | -------------:| ------------:| -----------:| | `throughput \| rg 'x'` | 3059 | 3744 | 3739 | | `throughput \| nu --testbin relay o> /dev/null` | 3508 | 8087 | 8136 | # User-Facing Changes - This is a breaking change for the plugin communication protocol, because the `ExternalStreamInfo` was replaced with `ByteStreamInfo`. Plugins now only have to deal with a single input stream, as opposed to the previous three streams: stdout, stderr, and exit code. - The output of `describe` has been changed for external/byte streams. - Temporary breaking change: `bytes starts-with` no longer works with byte streams. This is to keep the PR smaller, and `bytes ends-with` already does not work on byte streams. - If a process core dumped, then instead of having a `Value::Error` in the `exit_code` column of the output returned from `complete`, it now is a `Value::Int` with the negation of the signal number. # After Submitting - Update docs and book as necessary - Release notes (e.g., plugin protocol changes) - Adapt/convert commands to work with byte streams (high priority is `str length`, `bytes starts-with`, and maybe `bytes ends-with`). - Refactor the `tee` code, Devyn has already done some work on this. --------- Co-authored-by: Devyn Cairns <devyn.cairns@gmail.com>
This commit is contained in:
29
src/main.rs
29
src/main.rs
@ -25,15 +25,13 @@ use nu_cmd_base::util::get_init_cwd;
|
||||
use nu_lsp::LanguageServer;
|
||||
use nu_path::canonicalize_with;
|
||||
use nu_protocol::{
|
||||
engine::EngineState, report_error_new, util::BufferedReader, PipelineData, RawStream,
|
||||
ShellError, Span, Value,
|
||||
engine::EngineState, report_error_new, ByteStream, PipelineData, ShellError, Span, Value,
|
||||
};
|
||||
use nu_std::load_standard_library;
|
||||
use nu_utils::utils::perf;
|
||||
use run::{run_commands, run_file, run_repl};
|
||||
use signals::ctrlc_protection;
|
||||
use std::{
|
||||
io::BufReader,
|
||||
path::PathBuf,
|
||||
str::FromStr,
|
||||
sync::{atomic::AtomicBool, Arc},
|
||||
@ -345,22 +343,7 @@ fn main() -> Result<()> {
|
||||
start_time = std::time::Instant::now();
|
||||
let input = if let Some(redirect_stdin) = &parsed_nu_cli_args.redirect_stdin {
|
||||
trace!("redirecting stdin");
|
||||
let stdin = std::io::stdin();
|
||||
let buf_reader = BufReader::new(stdin);
|
||||
|
||||
PipelineData::ExternalStream {
|
||||
stdout: Some(RawStream::new(
|
||||
Box::new(BufferedReader::new(buf_reader)),
|
||||
Some(ctrlc.clone()),
|
||||
redirect_stdin.span,
|
||||
None,
|
||||
)),
|
||||
stderr: None,
|
||||
exit_code: None,
|
||||
span: redirect_stdin.span,
|
||||
metadata: None,
|
||||
trim_end_newline: false,
|
||||
}
|
||||
PipelineData::ByteStream(ByteStream::stdin(redirect_stdin.span)?, None)
|
||||
} else {
|
||||
trace!("not redirecting stdin");
|
||||
PipelineData::empty()
|
||||
@ -450,7 +433,7 @@ fn main() -> Result<()> {
|
||||
);
|
||||
}
|
||||
|
||||
LanguageServer::initialize_stdio_connection()?.serve_requests(engine_state, ctrlc)
|
||||
LanguageServer::initialize_stdio_connection()?.serve_requests(engine_state, ctrlc)?
|
||||
} else if let Some(commands) = parsed_nu_cli_args.commands.clone() {
|
||||
run_commands(
|
||||
&mut engine_state,
|
||||
@ -460,7 +443,6 @@ fn main() -> Result<()> {
|
||||
input,
|
||||
entire_start_time,
|
||||
);
|
||||
Ok(())
|
||||
} else if !script_name.is_empty() {
|
||||
run_file(
|
||||
&mut engine_state,
|
||||
@ -470,8 +452,9 @@ fn main() -> Result<()> {
|
||||
args_to_script,
|
||||
input,
|
||||
);
|
||||
Ok(())
|
||||
} else {
|
||||
run_repl(&mut engine_state, parsed_nu_cli_args, entire_start_time)
|
||||
run_repl(&mut engine_state, parsed_nu_cli_args, entire_start_time)?
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
Reference in New Issue
Block a user