Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Use FxHasher in places where we don't need DDoS resistance #2342

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ log = { version = "0.4", default-features = false }
qlog = { version = "0.13", default-features = false }
quinn-udp = { version = "0.5.10", default-features = false, features = ["direct-log", "fast-apple-datapath"] }
regex = { version = "1.9", default-features = false }
rustc-hash = { version = "1.1", default-features = false, features = [ "std" ]}
static_assertions = { version = "1.1", default-features = false }
strum = { version = "0.26", default-features = false, features = ["derive"] }
url = { version = "2.5", default-features = false, features = ["std"] }
Expand Down
1 change: 1 addition & 0 deletions neqo-bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ neqo-udp = { path = "./../neqo-udp" }
qlog = { workspace = true }
quinn-udp = { workspace = true }
regex = { workspace = true, features = ["unicode-perl"] }
rustc-hash = { workspace = true }
tokio = { version = "1", default-features = false, features = ["net", "time", "macros", "rt", "rt-multi-thread"] }
url = { workspace = true }

Expand Down
5 changes: 3 additions & 2 deletions neqo-bin/src/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},
collections::VecDeque,
fs::File,
io::{BufWriter, Write as _},
net::SocketAddr,
Expand All @@ -25,6 +25,7 @@ use neqo_transport::{
CloseReason, Connection, ConnectionEvent, ConnectionIdGenerator, EmptyConnectionIdGenerator,
Error, Output, RandomConnectionIdGenerator, State, StreamId, StreamType,
};
use rustc_hash::FxHashMap as HashMap;
use url::Url;

use super::{get_output_file, qlog_new, Args, CloseState, Res};
Expand Down Expand Up @@ -226,7 +227,7 @@ impl super::Client for Connection {
impl<'b> Handler<'b> {
pub fn new(url_queue: VecDeque<Url>, args: &'b Args) -> Self {
Self {
streams: HashMap::new(),
streams: HashMap::default(),
url_queue,
handled_urls: Vec::new(),
all_paths: Vec::new(),
Expand Down
5 changes: 3 additions & 2 deletions neqo-bin/src/client/http3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

use std::{
cell::RefCell,
collections::{HashMap, VecDeque},
collections::VecDeque,
fmt::Display,
fs::File,
io::{BufWriter, Write as _},
Expand All @@ -27,6 +27,7 @@ use neqo_transport::{
AppError, CloseReason, Connection, EmptyConnectionIdGenerator, Error as TransportError, Output,
RandomConnectionIdGenerator, StreamId,
};
use rustc_hash::FxHashMap as HashMap;
use url::Url;

use super::{get_output_file, qlog_new, Args, CloseState, Res};
Expand All @@ -45,7 +46,7 @@ impl<'a> Handler<'a> {
let url_handler = UrlHandler {
url_queue,
handled_urls: Vec::new(),
stream_handlers: HashMap::new(),
stream_handlers: HashMap::default(),
all_paths: Vec::new(),
args,
};
Expand Down
14 changes: 9 additions & 5 deletions neqo-bin/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#![expect(clippy::unwrap_used, reason = "This is example code.")]

use std::{
collections::{HashMap, VecDeque},
collections::VecDeque,
fmt::{self, Display},
fs::{create_dir_all, File, OpenOptions},
io::{self, BufWriter},
Expand All @@ -31,6 +31,7 @@ use neqo_crypto::{
use neqo_http3::Output;
use neqo_transport::{AppError, CloseReason, ConnectionId, Version};
use neqo_udp::RecvBuf;
use rustc_hash::FxHashMap as HashMap;
use tokio::time::Sleep;
use url::{Host, Origin, Url};

Expand Down Expand Up @@ -528,10 +529,13 @@ const fn local_addr_for(remote_addr: &SocketAddr, local_port: u16) -> SocketAddr

fn urls_by_origin(urls: &[Url]) -> impl Iterator<Item = ((Host, u16), VecDeque<Url>)> {
urls.iter()
.fold(HashMap::<Origin, VecDeque<Url>>::new(), |mut urls, url| {
urls.entry(url.origin()).or_default().push_back(url.clone());
urls
})
.fold(
HashMap::<Origin, VecDeque<Url>>::default(),
|mut urls, url| {
urls.entry(url.origin()).or_default().push_back(url.clone());
urls
},
)
.into_iter()
.filter_map(|(origin, urls)| match origin {
Origin::Tuple(_scheme, h, p) => Some(((h, p), urls)),
Expand Down
7 changes: 4 additions & 3 deletions neqo-bin/src/server/http09.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#![expect(clippy::unwrap_used, reason = "This is example code.")]

use std::{borrow::Cow, cell::RefCell, collections::HashMap, fmt::Display, rc::Rc, time::Instant};
use std::{borrow::Cow, cell::RefCell, fmt::Display, rc::Rc, time::Instant};

use neqo_common::{event::Provider as _, hex, qdebug, qerror, qinfo, qwarn, Datagram};
use neqo_crypto::{generate_ech_keys, random, AllowZeroRtt, AntiReplay};
Expand All @@ -16,6 +16,7 @@ use neqo_transport::{
ConnectionEvent, ConnectionIdGenerator, Output, State, StreamId,
};
use regex::Regex;
use rustc_hash::FxHashMap as HashMap;

use super::{qns_read_response, Args};
use crate::{send_data::SendData, STREAM_IO_BUFFER_SIZE};
Expand Down Expand Up @@ -68,8 +69,8 @@ impl HttpServer {
let is_qns_test = args.shared.qns_test.is_some();
Ok(Self {
server,
write_state: HashMap::new(),
read_state: HashMap::new(),
write_state: HashMap::default(),
read_state: HashMap::default(),
is_qns_test,
regex: if is_qns_test {
Regex::new(r"GET +/(\S+)(?:\r)?\n").map_err(|_| Error::Internal)?
Expand Down
6 changes: 3 additions & 3 deletions neqo-bin/src/server/http3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

use std::{
cell::RefCell,
collections::HashMap,
fmt::{self, Display},
rc::Rc,
time::Instant,
Expand All @@ -20,6 +19,7 @@ use neqo_http3::{
Http3OrWebTransportStream, Http3Parameters, Http3Server, Http3ServerEvent, StreamId,
};
use neqo_transport::{server::ValidateAddress, ConnectionIdGenerator};
use rustc_hash::FxHashMap as HashMap;

use super::{qns_read_response, Args};
use crate::send_data::SendData;
Expand Down Expand Up @@ -68,8 +68,8 @@ impl HttpServer {
}
Self {
server,
remaining_data: HashMap::new(),
posts: HashMap::new(),
remaining_data: HashMap::default(),
posts: HashMap::default(),
is_qns_test: args.shared.qns_test.is_some(),
}
}
Expand Down
1 change: 1 addition & 0 deletions neqo-http3/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ neqo-crypto = { path = "./../neqo-crypto" }
neqo-qpack = { path = "./../neqo-qpack" }
neqo-transport = { path = "./../neqo-transport" }
qlog = { workspace = true }
rustc-hash = { workspace = true}
sfv = { version = "0.9", default-features = false }
url = { workspace = true }

Expand Down
17 changes: 6 additions & 11 deletions neqo-http3/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,15 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::{
cell::RefCell,
collections::{BTreeSet, HashMap},
fmt::Debug,
mem,
rc::Rc,
};
use std::{cell::RefCell, fmt::Debug, mem, rc::Rc};

use neqo_common::{qdebug, qerror, qinfo, qtrace, qwarn, Decoder, Header, MessageType, Role};
use neqo_qpack::{decoder::QPackDecoder, encoder::QPackEncoder};
use neqo_transport::{
streams::SendOrder, AppError, CloseReason, Connection, DatagramTracking, State, StreamId,
StreamType, ZeroRttState,
};
use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet};

use crate::{
client_events::Http3ClientEvents,
Expand Down Expand Up @@ -302,7 +297,7 @@ pub struct Http3Connection {
pub qpack_encoder: Rc<RefCell<QPackEncoder>>,
pub qpack_decoder: Rc<RefCell<QPackDecoder>>,
settings_state: Http3RemoteSettingsState,
streams_with_pending_data: BTreeSet<StreamId>,
streams_with_pending_data: HashSet<StreamId>,
pub send_streams: HashMap<StreamId, Box<dyn SendStream>>,
pub recv_streams: HashMap<StreamId, Box<dyn RecvStream>>,
webtransport: ExtendedConnectFeature,
Expand Down Expand Up @@ -333,9 +328,9 @@ impl Http3Connection {
),
local_params: conn_params,
settings_state: Http3RemoteSettingsState::NotReceived,
streams_with_pending_data: BTreeSet::new(),
send_streams: HashMap::new(),
recv_streams: HashMap::new(),
streams_with_pending_data: HashSet::default(),
send_streams: HashMap::default(),
recv_streams: HashMap::default(),
role,
}
}
Expand Down
3 changes: 2 additions & 1 deletion neqo-http3/src/control_stream_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::collections::{HashMap, VecDeque};
use std::collections::VecDeque;

use neqo_common::{qtrace, Encoder};
use neqo_transport::{Connection, StreamId, StreamType};
use rustc_hash::FxHashMap as HashMap;

use crate::{frames::HFrame, BufferedStream, Error, Http3StreamType, RecvStream, Res};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::{cell::RefCell, collections::BTreeSet, mem, rc::Rc};
use std::{cell::RefCell, mem, rc::Rc};

use neqo_common::{qtrace, Encoder, Header, MessageType, Role};
use neqo_qpack::{QPackDecoder, QPackEncoder};
use neqo_transport::{Connection, DatagramTracking, StreamId};
use rustc_hash::FxHashSet as HashSet;

use super::{ExtendedConnectEvents, ExtendedConnectType, SessionCloseReason};
use crate::{
Expand Down Expand Up @@ -43,8 +44,8 @@ pub struct WebTransportSession {
state: SessionState,
frame_reader: FrameReader,
events: Box<dyn ExtendedConnectEvents>,
send_streams: BTreeSet<StreamId>,
recv_streams: BTreeSet<StreamId>,
send_streams: HashSet<StreamId>,
recv_streams: HashSet<StreamId>,
role: Role,
}

Expand Down Expand Up @@ -89,8 +90,8 @@ impl WebTransportSession {
state: SessionState::Negotiating,
frame_reader: FrameReader::new(),
events,
send_streams: BTreeSet::new(),
recv_streams: BTreeSet::new(),
send_streams: HashSet::default(),
recv_streams: HashSet::default(),
role,
}
}
Expand Down Expand Up @@ -123,8 +124,8 @@ impl WebTransportSession {
state: SessionState::Active,
frame_reader: FrameReader::new(),
events,
send_streams: BTreeSet::new(),
recv_streams: BTreeSet::new(),
send_streams: HashSet::default(),
recv_streams: HashSet::default(),
role,
})
}
Expand Down Expand Up @@ -324,7 +325,7 @@ impl WebTransportSession {
matches!(self.state, SessionState::Active)
}

pub fn take_sub_streams(&mut self) -> (BTreeSet<StreamId>, BTreeSet<StreamId>) {
pub fn take_sub_streams(&mut self) -> (HashSet<StreamId>, HashSet<StreamId>) {
(
mem::take(&mut self.recv_streams),
mem::take(&mut self.send_streams),
Expand Down
4 changes: 2 additions & 2 deletions neqo-http3/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use std::{
cell::{RefCell, RefMut},
collections::HashMap,
path::PathBuf,
rc::Rc,
time::Instant,
Expand All @@ -18,6 +17,7 @@ use neqo_transport::{
server::{ConnectionRef, Server, ValidateAddress},
ConnectionIdGenerator, Output,
};
use rustc_hash::FxHashMap as HashMap;

use crate::{
connection::Http3State,
Expand Down Expand Up @@ -73,7 +73,7 @@ impl Http3Server {
http3_parameters.get_connection_parameters().clone(),
)?,
http3_parameters,
http3_handlers: HashMap::new(),
http3_handlers: HashMap::default(),
events: Http3ServerEvents::default(),
})
}
Expand Down
1 change: 1 addition & 0 deletions neqo-qpack/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ log = { workspace = true }
neqo-common = { path = "./../neqo-common" }
neqo-transport = { path = "./../neqo-transport" }
qlog = { workspace = true }
rustc-hash = { workspace = true }
static_assertions = { workspace = true }

[dev-dependencies]
Expand Down
7 changes: 4 additions & 3 deletions neqo-qpack/src/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::collections::{HashMap, HashSet, VecDeque};
use std::collections::VecDeque;

use neqo_common::{qdebug, qerror, qlog::NeqoQlog, qtrace, Header};
use neqo_transport::{Connection, Error as TransportError, StreamId};
use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet};

use crate::{
decoder_instructions::{DecoderInstruction, DecoderInstructionReader},
Expand Down Expand Up @@ -68,7 +69,7 @@ impl QPackEncoder {
instruction_reader: DecoderInstructionReader::new(),
local_stream: LocalStreamState::NoStream,
max_blocked_streams: 0,
unacked_header_blocks: HashMap::new(),
unacked_header_blocks: HashMap::default(),
blocked_stream_cnt: 0,
use_huffman,
next_capacity: None,
Expand Down Expand Up @@ -385,7 +386,7 @@ impl QPackEncoder {
let stream_is_blocker = self.is_stream_blocker(stream_id);
let can_block = self.blocked_stream_cnt < self.max_blocked_streams || stream_is_blocker;

let mut ref_entries = HashSet::new();
let mut ref_entries = HashSet::default();

for iter in h {
let name = iter.name().as_bytes().to_vec();
Expand Down
1 change: 1 addition & 0 deletions neqo-transport/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ neqo-common = { path = "../neqo-common" }
neqo-crypto = { path = "../neqo-crypto" }
mtu = { version = "0.2", default-features = false } # neqo is only user currently, can bump freely
qlog = { workspace = true }
rustc-hash = { workspace = true }
smallvec = { version = "1.13", default-features = false }
static_assertions = { workspace = true }
strum = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion neqo-transport/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use std::{
cell::RefCell,
cmp::min,
collections::HashSet,
ops::{Deref, DerefMut},
path::PathBuf,
rc::Rc,
Expand All @@ -24,6 +23,7 @@ use neqo_crypto::{
encode_ech_config, AntiReplay, Cipher, PrivateKey, PublicKey, ZeroRttCheckResult,
ZeroRttChecker,
};
use rustc_hash::FxHashSet as HashSet;

pub use crate::addr_valid::ValidateAddress;
use crate::{
Expand Down
Loading