From 92057e66b6c3c0fe0afd7ecf4db136e70fe0061d Mon Sep 17 00:00:00 2001
From: crumblingstatue <radiantstatue@gmail.com>
Date: Tue, 5 Nov 2024 20:20:52 +0100
Subject: [PATCH] Make stream buffer size configurable, and have a larger
 default

---
 src/app.rs          | 9 ++++++++-
 src/args.rs         | 6 +++++-
 src/gui/file_ops.rs | 1 +
 3 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/src/app.rs b/src/app.rs
index 171d408..42452a0 100644
--- a/src/app.rs
+++ b/src/app.rs
@@ -73,8 +73,12 @@ pub struct App {
     /// A quit was requested
     pub quit_requested: bool,
     pub plugins: Vec<PluginContainer>,
+    /// Size of the buffer used for streaming reads
+    pub stream_buffer_size: usize,
 }
 
+const DEFAULT_STREAM_BUFFER_SIZE: usize = 65_536;
+
 impl App {
     pub(crate) fn new(
         mut args: Args,
@@ -107,6 +111,7 @@ impl App {
             backend_cmd: Default::default(),
             quit_requested: false,
             plugins: Vec::new(),
+            stream_buffer_size: args.src.stream_buffer_size.unwrap_or(DEFAULT_STREAM_BUFFER_SIZE),
         };
         for path in args.load_plugin {
             this.plugins.push(unsafe { PluginContainer::new(path)? });
@@ -378,6 +383,7 @@ impl App {
                 take: None,
                 read_only,
                 stream: false,
+                stream_buffer_size: None,
             },
             None,
             msg,
@@ -475,8 +481,8 @@ impl App {
                 let (tx, rx) = std::sync::mpsc::channel();
                 let mut src_clone = src.provider.clone();
                 self.stream_read_recv = Some(rx);
+                let buffer_size = self.stream_buffer_size;
                 thread::spawn(move || {
-                    let buffer_size = 1024;
                     let mut buf = vec![0; buffer_size];
                     let result: anyhow::Result<()> = try {
                         let amount = src_clone.read(&mut buf)?;
@@ -1013,6 +1019,7 @@ fn load_proc_memory_linux(
             take: Some(size),
             read_only: !is_write,
             stream: false,
+            stream_buffer_size: None,
         },
         None,
         msg,
diff --git a/src/args.rs b/src/args.rs
index 96aeb2c..91732dc 100644
--- a/src/args.rs
+++ b/src/args.rs
@@ -66,8 +66,12 @@ pub struct SourceArgs {
     /// Open file as read-only, without writing privileges
     #[arg(long)]
     pub read_only: bool,
-    #[arg(long)]
     /// Specify source as a streaming source (for example, standard streams).
     /// Sets read-only attribute.
+    #[arg(long)]
     pub stream: bool,
+    /// The buffer size in bytes to use for reading when streaming
+    #[arg(long)]
+    #[serde(default)]
+    pub stream_buffer_size: Option<usize>,
 }
diff --git a/src/gui/file_ops.rs b/src/gui/file_ops.rs
index 6341909..69067e0 100644
--- a/src/gui/file_ops.rs
+++ b/src/gui/file_ops.rs
@@ -250,6 +250,7 @@ impl FileOps {
                             take: None,
                             read_only: false,
                             stream: false,
+                            stream_buffer_size: None,
                         });
                     };
                     msg_if_fail(result, "Failed to save as", msg);