From 32b656adf4070aecf1185fd85c2e59aa6ebb4d65 Mon Sep 17 00:00:00 2001 From: Narfinger Date: Mon, 29 Sep 2025 11:51:17 +0200 Subject: [PATCH] More changes to HashMap/HashSet to use fxhash (#39244) Moved more functions to fxhash. And provide comments about the choices when necessary. Testing: Hash functions shouldn't change functionality. Signed-off-by: Narfinger --- components/compositing/compositor.rs | 5 ++--- components/net/protocols/mod.rs | 5 +++-- components/script/dom/html/htmlimageelement.rs | 5 +++-- components/shared/compositing/lib.rs | 4 ++-- components/shared/net/pub_domains.rs | 10 ++++++---- components/webgl/webxr.rs | 5 ++--- 6 files changed, 18 insertions(+), 16 deletions(-) diff --git a/components/compositing/compositor.rs b/components/compositing/compositor.rs index 1a381334c15..352f5a9ae2c 100644 --- a/components/compositing/compositor.rs +++ b/components/compositing/compositor.rs @@ -3,7 +3,6 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ use std::cell::{Cell, Ref, RefCell}; -use std::collections::HashMap; use std::collections::hash_map::Entry; use std::env; use std::fs::create_dir_all; @@ -1693,9 +1692,9 @@ struct FrameDelayer { /// The latest [`Epoch`] of canvas images that have been sent to WebRender. Note /// that this only records the `Epoch`s for canvases and only ones that are involved /// in "update the rendering". - image_epochs: HashMap, + image_epochs: FxHashMap, /// A map of all pending canvas images - pending_canvas_images: HashMap, + pending_canvas_images: FxHashMap, /// Whether or not we have a pending frame. pending_frame: bool, /// A list of pipelines that should be notified when we are no longer waiting for diff --git a/components/net/protocols/mod.rs b/components/net/protocols/mod.rs index a4d420bf2a5..47f2df22fa4 100644 --- a/components/net/protocols/mod.rs +++ b/components/net/protocols/mod.rs @@ -2,7 +2,6 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -use std::collections::HashMap; use std::collections::hash_map::Entry; use std::future::Future; use std::ops::Bound; @@ -14,6 +13,7 @@ use log::error; use net_traits::filemanager_thread::RelativePos; use net_traits::request::Request; use net_traits::response::Response; +use rustc_hash::FxHashMap; use servo_url::ServoUrl; use crate::fetch::methods::{DoneChannel, FetchContext, RangeRequestBounds}; @@ -68,7 +68,7 @@ pub trait ProtocolHandler: Send + Sync { #[derive(Default)] pub struct ProtocolRegistry { - pub(crate) handlers: HashMap>, // Maps scheme -> handler + pub(crate) handlers: FxHashMap>, // Maps scheme -> handler } #[derive(Clone, Copy, Debug)] @@ -94,6 +94,7 @@ impl ProtocolRegistry { registry } + /// Do not allow users to enter an arbitrary protocol as this can lead to slowdowns. pub fn register( &mut self, scheme: &str, diff --git a/components/script/dom/html/htmlimageelement.rs b/components/script/dom/html/htmlimageelement.rs index 1502e9753bf..e395ce33bd0 100644 --- a/components/script/dom/html/htmlimageelement.rs +++ b/components/script/dom/html/htmlimageelement.rs @@ -3,7 +3,6 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ use std::cell::Cell; -use std::collections::HashSet; use std::default::Default; use std::rc::Rc; use std::sync::{Arc, LazyLock}; @@ -32,6 +31,7 @@ use pixels::{ CorsStatus, ImageMetadata, PixelFormat, Snapshot, SnapshotAlphaMode, SnapshotPixelFormat, }; use regex::Regex; +use rustc_hash::FxHashSet; use servo_url::ServoUrl; use servo_url::origin::MutableOrigin; use style::attr::{AttrValue, LengthOrPercentageOrAuto, parse_unsigned_integer}; @@ -840,7 +840,8 @@ impl HTMLImageElement { let source_set = self.source_set.borrow(); let len = source_set.image_sources.len(); - let mut repeat_indices = HashSet::new(); + // Using FxHash is ok here as the indices are just 0..len + let mut repeat_indices = FxHashSet::default(); for outer_index in 0..len { if repeat_indices.contains(&outer_index) { continue; diff --git a/components/shared/compositing/lib.rs b/components/shared/compositing/lib.rs index b6e383bbf11..ebde61789fd 100644 --- a/components/shared/compositing/lib.rs +++ b/components/shared/compositing/lib.rs @@ -12,6 +12,7 @@ use crossbeam_channel::Sender; use embedder_traits::{AnimationState, EventLoopWaker, TouchEventResult}; use log::warn; use malloc_size_of_derive::MallocSizeOf; +use rustc_hash::FxHashMap; use smallvec::SmallVec; use strum_macros::IntoStaticStr; use webrender_api::{DocumentId, FontVariation}; @@ -20,7 +21,6 @@ pub mod display_list; pub mod rendering_context; pub mod viewport_description; -use std::collections::HashMap; use std::sync::{Arc, Mutex}; use base::generic_channel::{self, GenericCallback, GenericSender}; @@ -426,7 +426,7 @@ pub enum WebrenderImageHandlerType { #[derive(Default)] pub struct WebrenderExternalImageRegistry { /// Map of all generated external images. - external_images: HashMap, + external_images: FxHashMap, /// Id generator for the next external image identifier. next_image_id: u64, } diff --git a/components/shared/net/pub_domains.rs b/components/shared/net/pub_domains.rs index 6e6f883cd2f..86d1774c17a 100644 --- a/components/shared/net/pub_domains.rs +++ b/components/shared/net/pub_domains.rs @@ -14,20 +14,22 @@ //! we don't need to make the code more complex for it. The `mach` update command makes sure that //! those cases are not present. -use std::collections::HashSet; use std::iter::FromIterator; use std::sync::LazyLock; use embedder_traits::resources::{self, Resource}; use malloc_size_of::{MallocSizeOf, MallocSizeOfOps}; use malloc_size_of_derive::MallocSizeOf; +use rustc_hash::FxHashSet; use servo_url::{Host, ImmutableOrigin, ServoUrl}; +// We can use FxHash here. +// The list is given by publicsuffix.org so an attack is highly unlikely #[derive(Clone, Debug, Default, MallocSizeOf)] pub struct PubDomainRules { - rules: HashSet, - wildcards: HashSet, - exceptions: HashSet, + rules: FxHashSet, + wildcards: FxHashSet, + exceptions: FxHashSet, } static PUB_DOMAINS: LazyLock = LazyLock::new(load_pub_domains); diff --git a/components/webgl/webxr.rs b/components/webgl/webxr.rs index 7d71a5f2495..078a1130070 100644 --- a/components/webgl/webxr.rs +++ b/components/webgl/webxr.rs @@ -2,7 +2,6 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -use std::collections::HashMap; use std::num::NonZeroU32; use canvas_traits::webgl::{ @@ -25,7 +24,7 @@ use crate::webgl_thread::{GLContextData, WebGLThread}; /// Bridge between WebGL and WebXR pub(crate) struct WebXRBridge { factory_receiver: crossbeam_channel::Receiver>, - managers: HashMap>>, + managers: FxHashMap>>, next_manager_id: NonZeroU32, } @@ -34,7 +33,7 @@ impl WebXRBridge { let WebXRBridgeInit { factory_receiver, .. } = init; - let managers = HashMap::new(); + let managers = FxHashMap::default(); let next_manager_id = NonZeroU32::MIN; WebXRBridge { factory_receiver,