Skip to content

Commit

Permalink
feat(bin/client): use BufWriter when writing to file (#1756)
Browse files Browse the repository at this point in the history
* feat: Use `std::io::BufWriter` with a 64KB buffer for dumping data

Let's see if this makes things faster.

* Flush `BufWriter` on FIN

* Update neqo-bin/src/bin/client/http3.rs

Co-authored-by: Max Inden <mail@max-inden.de>
Signed-off-by: Lars Eggert <lars@eggert.org>

* Fix

* Also for h09

* Move BufWriter construction into get_output_file

---------

Signed-off-by: Lars Eggert <lars@eggert.org>
Co-authored-by: Lars Eggert <lars@eggert.org>
  • Loading branch information
mxinden and larseggert authored Mar 17, 2024
1 parent ef5caeb commit 29fdbc0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
13 changes: 7 additions & 6 deletions neqo-bin/src/bin/client/http09.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::{
cell::RefCell,
collections::{HashMap, VecDeque},
fs::File,
io::Write,
io::{BufWriter, Write},
net::SocketAddr,
path::PathBuf,
rc::Rc,
Expand All @@ -29,7 +29,7 @@ use super::{get_output_file, Args, KeyUpdateState, Res};
use crate::qlog_new;

pub struct Handler<'a> {
streams: HashMap<StreamId, Option<File>>,
streams: HashMap<StreamId, Option<BufWriter<File>>>,
url_queue: VecDeque<Url>,
all_paths: Vec<PathBuf>,
args: &'a Args,
Expand Down Expand Up @@ -219,7 +219,7 @@ impl<'b> Handler<'b> {
client: &mut Connection,
stream_id: StreamId,
output_read_data: bool,
maybe_out_file: &mut Option<File>,
maybe_out_file: &mut Option<BufWriter<File>>,
) -> Res<bool> {
let mut data = vec![0; 4096];
loop {
Expand All @@ -246,8 +246,7 @@ impl<'b> Handler<'b> {
}

fn read(&mut self, client: &mut Connection, stream_id: StreamId) -> Res<()> {
let mut maybe_maybe_out_file = self.streams.get_mut(&stream_id);
match &mut maybe_maybe_out_file {
match self.streams.get_mut(&stream_id) {
None => {
println!("Data on unexpected stream: {stream_id}");
return Ok(());
Expand All @@ -261,7 +260,9 @@ impl<'b> Handler<'b> {
)?;

if fin_recvd {
if maybe_out_file.is_none() {
if let Some(mut out_file) = maybe_out_file.take() {
out_file.flush()?;
} else {
println!("<FIN[{stream_id}]>");
}
self.streams.remove(&stream_id);
Expand Down
12 changes: 8 additions & 4 deletions neqo-bin/src/bin/client/http3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::{
collections::{HashMap, VecDeque},
fmt::Display,
fs::File,
io::Write,
io::{BufWriter, Write},
net::SocketAddr,
path::PathBuf,
rc::Rc,
Expand Down Expand Up @@ -269,7 +269,7 @@ impl StreamHandlerType {
}

struct DownloadStreamHandler {
out_file: Option<File>,
out_file: Option<BufWriter<File>>,
}

impl StreamHandler for DownloadStreamHandler {
Expand Down Expand Up @@ -300,8 +300,12 @@ impl StreamHandler for DownloadStreamHandler {
println!("READ[{}]: 0x{}", stream_id, hex(&data));
}

if fin && self.out_file.is_none() {
println!("<FIN[{stream_id}]>");
if fin {
if let Some(mut out_file) = self.out_file.take() {
out_file.flush()?;
} else {
println!("<FIN[{stream_id}]>");
}
}

Ok(true)
Expand Down
8 changes: 5 additions & 3 deletions neqo-bin/src/bin/client/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
collections::{HashMap, VecDeque},
fmt::{self, Display},
fs::{create_dir_all, File, OpenOptions},
io,
io::{self, BufWriter},
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, ToSocketAddrs},
path::PathBuf,
pin::Pin,
Expand Down Expand Up @@ -36,6 +36,8 @@ use url::{Origin, Url};
mod http09;
mod http3;

const BUFWRITER_BUFFER_SIZE: usize = 64 * 1024;

#[derive(Debug)]
pub enum ClientError {
ArgumentError(&'static str),
Expand Down Expand Up @@ -252,7 +254,7 @@ fn get_output_file(
url: &Url,
output_dir: &Option<PathBuf>,
all_paths: &mut Vec<PathBuf>,
) -> Option<File> {
) -> Option<BufWriter<File>> {
if let Some(ref dir) = output_dir {
let mut out_path = dir.clone();

Expand Down Expand Up @@ -284,7 +286,7 @@ fn get_output_file(
.ok()?;

all_paths.push(out_path);
Some(f)
Some(BufWriter::with_capacity(BUFWRITER_BUFFER_SIZE, f))
} else {
None
}
Expand Down

0 comments on commit 29fdbc0

Please sign in to comment.