Skip to content

Commit

Permalink
gossip: Write to contact-info.bin file with a BufWriter (#4115)
Browse files Browse the repository at this point in the history
Non-buffered write blocks the `save_contact_info` execution to around
200ms-1s. Buffered write minimizes the execution to 6-14 ms.
  • Loading branch information
vadorovsky authored Dec 17, 2024
1 parent 9e9c4b5 commit dd63bae
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions gossip/src/cluster_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ use {
collections::{HashMap, HashSet, VecDeque},
fmt::Debug,
fs::{self, File},
io::BufReader,
io::{BufReader, BufWriter, Write},
iter::repeat,
net::{IpAddr, Ipv4Addr, SocketAddr, TcpListener, UdpSocket},
num::NonZeroUsize,
Expand Down Expand Up @@ -349,15 +349,19 @@ impl ClusterInfo {
let tmp_filename = &filename.with_extension("tmp");

match File::create(tmp_filename) {
Ok(mut file) => {
if let Err(err) = bincode::serialize_into(&mut file, &nodes) {
Ok(file) => {
let mut writer = BufWriter::new(file);
if let Err(err) = bincode::serialize_into(&mut writer, &nodes) {
warn!(
"Failed to serialize contact info info {}: {}",
tmp_filename.display(),
err
);
return;
}
if let Err(err) = writer.flush() {
warn!("Failed to save contact info: {err}");
}
}
Err(err) => {
warn!("Failed to create {}: {}", tmp_filename.display(), err);
Expand Down

0 comments on commit dd63bae

Please sign in to comment.