From 634d4b6d47006af5fd3080987c1f251b559a3d4e Mon Sep 17 00:00:00 2001 From: Alessandro Decina Date: Thu, 11 Apr 2024 12:25:35 +1000 Subject: [PATCH] quic: use smallvec to aggregate chunks, save 1 alloc per packet (#735) quic: use smallvec, save one allocation per packet Use smallvec to hold chunks. Streams are packet-sized so we don't expect them to have many chunks. This saves us an allocation for each packet. (cherry picked from commit 55ab7fadbce16111541247db795902ebebcb6bb2) # Conflicts: # programs/sbf/Cargo.lock # streamer/Cargo.toml # streamer/src/nonblocking/quic.rs --- Cargo.lock | 5 +++-- Cargo.toml | 2 +- programs/sbf/Cargo.lock | 9 +++++++-- streamer/Cargo.toml | 5 +++++ streamer/src/nonblocking/quic.rs | 17 ++++++++++++++++- 5 files changed, 32 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a1556bfb858ff4..1a06894ff8c373 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5372,9 +5372,9 @@ checksum = "9def91fd1e018fe007022791f865d0ccc9b3a0d5001e01aabb8b40e46000afb5" [[package]] name = "smallvec" -version = "1.13.1" +version = "1.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6ecd384b10a64542d77071bd64bd7b231f4ed5940fba55e98c3de13824cf3d7" +checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" [[package]] name = "smpl_jwt" @@ -7392,6 +7392,7 @@ dependencies = [ "rand 0.8.5", "rcgen", "rustls", + "smallvec", "solana-logger", "solana-metrics", "solana-perf", diff --git a/Cargo.toml b/Cargo.toml index 9b5e08f3dd1937..75eac661051b4a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -303,7 +303,7 @@ sha2 = "0.10.8" sha3 = "0.10.4" signal-hook = "0.3.17" siphasher = "0.3.11" -smallvec = "1.13.1" +smallvec = "1.13.2" smpl_jwt = "0.7.1" socket2 = "0.5.5" soketto = "0.7" diff --git a/programs/sbf/Cargo.lock b/programs/sbf/Cargo.lock index aea038aac30468..6c22df17e39fcd 100644 --- a/programs/sbf/Cargo.lock +++ b/programs/sbf/Cargo.lock @@ -4649,9 +4649,9 @@ checksum = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" [[package]] name = "smallvec" -version = "1.13.1" +version = "1.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6ecd384b10a64542d77071bd64bd7b231f4ed5940fba55e98c3de13824cf3d7" +checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" [[package]] name = "smpl_jwt" @@ -6436,6 +6436,11 @@ dependencies = [ "rand 0.8.5", "rcgen", "rustls", +<<<<<<< HEAD +======= + "smallvec", + "solana-measure", +>>>>>>> 55ab7fadbc (quic: use smallvec to aggregate chunks, save 1 alloc per packet (#735)) "solana-metrics", "solana-perf", "solana-sdk", diff --git a/streamer/Cargo.toml b/streamer/Cargo.toml index 21ae96d11fd9a4..b24a544d755d60 100644 --- a/streamer/Cargo.toml +++ b/streamer/Cargo.toml @@ -28,6 +28,11 @@ quinn-proto = { workspace = true } rand = { workspace = true } rcgen = { workspace = true } rustls = { workspace = true, features = ["dangerous_configuration"] } +<<<<<<< HEAD +======= +smallvec = { workspace = true } +solana-measure = { workspace = true } +>>>>>>> 55ab7fadbc (quic: use smallvec to aggregate chunks, save 1 alloc per packet (#735)) solana-metrics = { workspace = true } solana-perf = { workspace = true } solana-sdk = { workspace = true } diff --git a/streamer/src/nonblocking/quic.rs b/streamer/src/nonblocking/quic.rs index f668a08edd38c3..a2017be0468765 100644 --- a/streamer/src/nonblocking/quic.rs +++ b/streamer/src/nonblocking/quic.rs @@ -17,6 +17,11 @@ use { quinn::{Connecting, Connection, Endpoint, EndpointConfig, TokioRuntime, VarInt}, quinn_proto::VarIntBoundsExceeded, rand::{thread_rng, Rng}, +<<<<<<< HEAD +======= + smallvec::SmallVec, + solana_measure::measure::Measure, +>>>>>>> 55ab7fadbc (quic: use smallvec to aggregate chunks, save 1 alloc per packet (#735)) solana_perf::packet::{PacketBatch, PACKETS_PER_BATCH}, solana_sdk::{ packet::{Meta, PACKET_DATA_SIZE}, @@ -93,7 +98,12 @@ struct PacketChunk { // the Packet and then when copying the Packet into a PacketBatch) struct PacketAccumulator { pub meta: Meta, +<<<<<<< HEAD pub chunks: Vec, +======= + pub chunks: SmallVec<[PacketChunk; 2]>, + pub start_time: Instant, +>>>>>>> 55ab7fadbc (quic: use smallvec to aggregate chunks, save 1 alloc per packet (#735)) } #[derive(Copy, Clone, Debug)] @@ -872,7 +882,12 @@ async fn handle_chunk( meta.set_socket_addr(remote_addr); *packet_accum = Some(PacketAccumulator { meta, +<<<<<<< HEAD chunks: Vec::new(), +======= + chunks: SmallVec::new(), + start_time: Instant::now(), +>>>>>>> 55ab7fadbc (quic: use smallvec to aggregate chunks, save 1 alloc per packet (#735)) }); } @@ -1471,7 +1486,7 @@ pub mod test { meta.size = size; let packet_accum = PacketAccumulator { meta, - chunks: vec![PacketChunk { + chunks: smallvec::smallvec![PacketChunk { bytes, offset, end_of_chunk: size,