Skip to content

Commit

Permalink
More FxHasher
Browse files Browse the repository at this point in the history
  • Loading branch information
larseggert committed Jan 10, 2025
1 parent 0bda917 commit 6762a02
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 37 deletions.
1 change: 1 addition & 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 neqo-bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ neqo-udp = { path = "./../neqo-udp" }
qlog = { workspace = true }
quinn-udp = { workspace = true }
regex = { workspace = true }
rustc-hash = { workspace = true }
tokio = { version = "1", default-features = false, features = ["net", "time", "macros", "rt", "rt-multi-thread"] }
url = { workspace = true }

Expand Down
7 changes: 4 additions & 3 deletions neqo-bin/src/client/http09.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use std::{
cell::RefCell,
collections::{HashMap, VecDeque},
collections::VecDeque,
fs::File,
io::{BufWriter, Write as _},
net::SocketAddr,
Expand All @@ -23,13 +23,14 @@ use neqo_transport::{
CloseReason, Connection, ConnectionEvent, ConnectionIdGenerator, EmptyConnectionIdGenerator,
Error, Output, RandomConnectionIdGenerator, State, StreamId, StreamType,
};
use rustc_hash::FxHashMap;
use url::Url;

use super::{get_output_file, qlog_new, Args, CloseState, Res};
use crate::STREAM_IO_BUFFER_SIZE;

pub struct Handler<'a> {
streams: HashMap<StreamId, Option<BufWriter<File>>>,
streams: FxHashMap<StreamId, Option<BufWriter<File>>>,
url_queue: VecDeque<Url>,
handled_urls: Vec<Url>,
all_paths: Vec<PathBuf>,
Expand Down Expand Up @@ -220,7 +221,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: FxHashMap::default(),
url_queue,
handled_urls: Vec::new(),
all_paths: Vec::new(),
Expand Down
7 changes: 4 additions & 3 deletions neqo-bin/src/client/http3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use std::{
cell::RefCell,
collections::{HashMap, VecDeque},
collections::VecDeque,
fmt::Display,
fs::File,
io::{BufWriter, Write as _},
Expand All @@ -25,6 +25,7 @@ use neqo_transport::{
AppError, CloseReason, Connection, EmptyConnectionIdGenerator, Error as TransportError, Output,
RandomConnectionIdGenerator, StreamId,
};
use rustc_hash::FxHashMap;
use url::Url;

use super::{get_output_file, qlog_new, Args, CloseState, Res};
Expand All @@ -43,7 +44,7 @@ impl<'a> Handler<'a> {
let url_handler = UrlHandler {
url_queue,
handled_urls: Vec::new(),
stream_handlers: HashMap::new(),
stream_handlers: FxHashMap::default(),
all_paths: Vec::new(),
args,
};
Expand Down Expand Up @@ -362,7 +363,7 @@ impl StreamHandler for UploadStreamHandler {
struct UrlHandler<'a> {
url_queue: VecDeque<Url>,
handled_urls: Vec<Url>,
stream_handlers: HashMap<StreamId, Box<dyn StreamHandler>>,
stream_handlers: FxHashMap<StreamId, Box<dyn StreamHandler>>,
all_paths: Vec<PathBuf>,
args: &'a 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 @@
#![allow(clippy::future_not_send)]

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;
use tokio::time::Sleep;
use url::{Host, Origin, Url};

Expand Down Expand Up @@ -522,10 +523,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(
FxHashMap::<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
11 changes: 6 additions & 5 deletions neqo-bin/src/server/http09.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

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 @@ -14,6 +14,7 @@ use neqo_transport::{
ConnectionEvent, ConnectionIdGenerator, Output, State, StreamId,
};
use regex::Regex;
use rustc_hash::FxHashMap;

use super::{qns_read_response, Args};
use crate::{send_data::SendData, STREAM_IO_BUFFER_SIZE};
Expand All @@ -26,8 +27,8 @@ struct HttpStreamState {

pub struct HttpServer {
server: Server,
write_state: HashMap<StreamId, HttpStreamState>,
read_state: HashMap<StreamId, Vec<u8>>,
write_state: FxHashMap<StreamId, HttpStreamState>,
read_state: FxHashMap<StreamId, Vec<u8>>,
is_qns_test: bool,
regex: Regex,
read_buffer: Vec<u8>,
Expand Down Expand Up @@ -66,8 +67,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: FxHashMap::default(),
read_state: FxHashMap::default(),
is_qns_test,
regex: if is_qns_test {
Regex::new(r"GET +/(\S+)(?:\r)?\n").unwrap()
Expand Down
10 changes: 5 additions & 5 deletions neqo-bin/src/server/http3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use std::{
cell::RefCell,
collections::HashMap,
fmt::{self, Display},
rc::Rc,
time::Instant,
Expand All @@ -18,15 +17,16 @@ use neqo_http3::{
Http3OrWebTransportStream, Http3Parameters, Http3Server, Http3ServerEvent, StreamId,
};
use neqo_transport::{server::ValidateAddress, ConnectionIdGenerator};
use rustc_hash::FxHashMap;

use super::{qns_read_response, Args};
use crate::send_data::SendData;

pub struct HttpServer {
server: Http3Server,
/// Progress writing to each stream.
remaining_data: HashMap<StreamId, SendData>,
posts: HashMap<Http3OrWebTransportStream, usize>,
remaining_data: FxHashMap<StreamId, SendData>,
posts: FxHashMap<Http3OrWebTransportStream, usize>,
is_qns_test: bool,
}

Expand Down Expand Up @@ -66,8 +66,8 @@ impl HttpServer {
}
Self {
server,
remaining_data: HashMap::new(),
posts: HashMap::new(),
remaining_data: FxHashMap::default(),
posts: FxHashMap::default(),
is_qns_test: args.shared.qns_test.is_some(),
}
}
Expand Down
14 changes: 6 additions & 8 deletions neqo-http3/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

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

use crate::{
connection::Http3State,
Expand All @@ -39,7 +39,7 @@ const MAX_EVENT_DATA_SIZE: usize = 1024;
pub struct Http3Server {
server: Server,
http3_parameters: Http3Parameters,
http3_handlers: HashMap<ConnectionRef, HandlerRef>,
http3_handlers: FxHashMap<ConnectionRef, HandlerRef>,
events: Http3ServerEvents,
}

Expand Down Expand Up @@ -75,7 +75,7 @@ impl Http3Server {
http3_parameters.get_connection_parameters().clone(),
)?,
http3_parameters,
http3_handlers: HashMap::new(),
http3_handlers: FxHashMap::default(),
events: Http3ServerEvents::default(),
})
}
Expand Down Expand Up @@ -318,17 +318,15 @@ fn prepare_data(

#[cfg(test)]
mod tests {
use std::{
collections::HashMap,
ops::{Deref, DerefMut},
};
use std::ops::{Deref, DerefMut};

use neqo_common::{event::Provider as _, Encoder};
use neqo_crypto::{AuthenticationStatus, ZeroRttCheckResult, ZeroRttChecker};
use neqo_qpack::{encoder::QPackEncoder, QpackSettings};
use neqo_transport::{
CloseReason, Connection, ConnectionEvent, State, StreamId, StreamType, ZeroRttState,
};
use rustc_hash::FxHashMap;
use test_fixture::{
anti_replay, default_client, fixture_init, now, CountingConnectionIdGenerator,
DEFAULT_ALPN, DEFAULT_KEYS,
Expand Down Expand Up @@ -1267,7 +1265,7 @@ mod tests {
let out = peer_conn.process_output(now());
hconn.process(out.dgram(), now());

let mut requests = HashMap::new();
let mut requests = FxHashMap::default();
while let Some(event) = hconn.next_event() {
match event {
Http3ServerEvent::Headers { stream, .. } => {
Expand Down
5 changes: 3 additions & 2 deletions neqo-transport/src/connection/tests/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::{cmp::max, collections::HashMap};
use std::cmp::max;

use neqo_common::{event::Provider as _, qdebug};
use rustc_hash::FxHashMap;
use test_fixture::now;

use super::{
Expand Down Expand Up @@ -164,7 +165,7 @@ fn sendorder_test(order_of_sendorder: &[Option<SendOrder>]) {
})
.enumerate()
.map(|(a, b)| (b, a))
.collect::<HashMap<_, _>>();
.collect::<FxHashMap<_, _>>();

// streams should arrive in priority order, not order of creation, if sendorder prioritization
// is working correctly
Expand Down
4 changes: 2 additions & 2 deletions 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;

pub use crate::addr_valid::ValidateAddress;
use crate::{
Expand Down Expand Up @@ -490,7 +490,7 @@ impl Server {
// `ActiveConnectionRef` `Hash` implementation doesn’t access any of the interior mutable types.
#[allow(clippy::mutable_key_type)]
#[must_use]
pub fn active_connections(&self) -> HashSet<ConnectionRef> {
pub fn active_connections(&self) -> FxHashSet<ConnectionRef> {
self.connections
.iter()
.filter(|c| c.borrow().has_events())
Expand Down
7 changes: 3 additions & 4 deletions neqo-transport/src/tracking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -660,9 +660,8 @@ impl Default for AckTracker {

#[cfg(test)]
mod tests {
use std::collections::HashSet;

use neqo_common::Encoder;
use rustc_hash::FxHashSet;
use test_fixture::now;

use super::{
Expand All @@ -679,7 +678,7 @@ mod tests {

fn test_ack_range(pns: &[PacketNumber], nranges: usize) {
let mut rp = RecvdPackets::new(PacketNumberSpace::Initial); // Any space will do.
let mut packets = HashSet::new();
let mut packets = FxHashSet::default();

for pn in pns {
rp.set_received(now(), *pn, true);
Expand All @@ -702,7 +701,7 @@ mod tests {
}

// Check that the ranges include the right values.
let mut in_ranges = HashSet::new();
let mut in_ranges = FxHashSet::default();
for range in &rp.ranges {
for included in range.smallest..=range.largest {
in_ranges.insert(included);
Expand Down

0 comments on commit 6762a02

Please sign in to comment.