From 0e36c43c64d2089e3af50d78b37e0ff12f083caa Mon Sep 17 00:00:00 2001 From: Devyn Cairns Date: Fri, 5 Apr 2024 20:52:27 -0700 Subject: [PATCH] Add BufWriter to ChildStdin on the plugin interface (#12419) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Description This speeds up writing messages to the plugin, because otherwise every individual piece of the messages (not even the entire message) is written with one syscall, leading to a lot of back and forth with the kernel. I learned this by running `strace` to debug something and saw a ton of `write()` calls. ```nushell # Before 1..10 | each { timeit { example seq 1 10000 | example sum } } | math avg 269ms 779µs 149ns # After > 1..10 | each { timeit { example seq 1 10000 | example sum } } | math avg 39ms 636µs 643ns ``` # User-Facing Changes - Performance improvement # Tests + Formatting - :green_circle: `toolkit fmt` - :green_circle: `toolkit clippy` - :green_circle: `toolkit test` - :green_circle: `toolkit test stdlib` --- crates/nu-plugin/src/plugin/mod.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/nu-plugin/src/plugin/mod.rs b/crates/nu-plugin/src/plugin/mod.rs index 90253ab7a3..d314f3c54a 100644 --- a/crates/nu-plugin/src/plugin/mod.rs +++ b/crates/nu-plugin/src/plugin/mod.rs @@ -10,7 +10,7 @@ use std::{ env, ffi::OsStr, fmt::Write, - io::{BufReader, Read, Write as WriteTrait}, + io::{BufReader, BufWriter, Read, Write as WriteTrait}, ops::Deref, path::Path, process::{Child, ChildStdout, Command as CommandSys, Stdio}, @@ -178,8 +178,9 @@ fn make_plugin_interface( let encoder = get_plugin_encoding(&mut stdout)?; let reader = BufReader::with_capacity(OUTPUT_BUFFER_SIZE, stdout); + let writer = BufWriter::with_capacity(OUTPUT_BUFFER_SIZE, stdin); - let mut manager = PluginInterfaceManager::new(source.clone(), (Mutex::new(stdin), encoder)); + let mut manager = PluginInterfaceManager::new(source.clone(), (Mutex::new(writer), encoder)); manager.set_garbage_collector(gc); let interface = manager.get_interface();