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 <Narfinger@users.noreply.github.com>
This commit is contained in:
Narfinger 2025-09-29 11:51:17 +02:00 committed by GitHub
parent 389f0d4cc2
commit 32b656adf4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 18 additions and 16 deletions

View file

@ -3,7 +3,6 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::cell::{Cell, Ref, RefCell}; use std::cell::{Cell, Ref, RefCell};
use std::collections::HashMap;
use std::collections::hash_map::Entry; use std::collections::hash_map::Entry;
use std::env; use std::env;
use std::fs::create_dir_all; 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 /// 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 /// that this only records the `Epoch`s for canvases and only ones that are involved
/// in "update the rendering". /// in "update the rendering".
image_epochs: HashMap<ImageKey, Epoch>, image_epochs: FxHashMap<ImageKey, Epoch>,
/// A map of all pending canvas images /// A map of all pending canvas images
pending_canvas_images: HashMap<ImageKey, Epoch>, pending_canvas_images: FxHashMap<ImageKey, Epoch>,
/// Whether or not we have a pending frame. /// Whether or not we have a pending frame.
pending_frame: bool, pending_frame: bool,
/// A list of pipelines that should be notified when we are no longer waiting for /// A list of pipelines that should be notified when we are no longer waiting for

View file

@ -2,7 +2,6 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * 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/. */ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::collections::HashMap;
use std::collections::hash_map::Entry; use std::collections::hash_map::Entry;
use std::future::Future; use std::future::Future;
use std::ops::Bound; use std::ops::Bound;
@ -14,6 +13,7 @@ use log::error;
use net_traits::filemanager_thread::RelativePos; use net_traits::filemanager_thread::RelativePos;
use net_traits::request::Request; use net_traits::request::Request;
use net_traits::response::Response; use net_traits::response::Response;
use rustc_hash::FxHashMap;
use servo_url::ServoUrl; use servo_url::ServoUrl;
use crate::fetch::methods::{DoneChannel, FetchContext, RangeRequestBounds}; use crate::fetch::methods::{DoneChannel, FetchContext, RangeRequestBounds};
@ -68,7 +68,7 @@ pub trait ProtocolHandler: Send + Sync {
#[derive(Default)] #[derive(Default)]
pub struct ProtocolRegistry { pub struct ProtocolRegistry {
pub(crate) handlers: HashMap<String, Box<dyn ProtocolHandler>>, // Maps scheme -> handler pub(crate) handlers: FxHashMap<String, Box<dyn ProtocolHandler>>, // Maps scheme -> handler
} }
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
@ -94,6 +94,7 @@ impl ProtocolRegistry {
registry registry
} }
/// Do not allow users to enter an arbitrary protocol as this can lead to slowdowns.
pub fn register( pub fn register(
&mut self, &mut self,
scheme: &str, scheme: &str,

View file

@ -3,7 +3,6 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::cell::Cell; use std::cell::Cell;
use std::collections::HashSet;
use std::default::Default; use std::default::Default;
use std::rc::Rc; use std::rc::Rc;
use std::sync::{Arc, LazyLock}; use std::sync::{Arc, LazyLock};
@ -32,6 +31,7 @@ use pixels::{
CorsStatus, ImageMetadata, PixelFormat, Snapshot, SnapshotAlphaMode, SnapshotPixelFormat, CorsStatus, ImageMetadata, PixelFormat, Snapshot, SnapshotAlphaMode, SnapshotPixelFormat,
}; };
use regex::Regex; use regex::Regex;
use rustc_hash::FxHashSet;
use servo_url::ServoUrl; use servo_url::ServoUrl;
use servo_url::origin::MutableOrigin; use servo_url::origin::MutableOrigin;
use style::attr::{AttrValue, LengthOrPercentageOrAuto, parse_unsigned_integer}; use style::attr::{AttrValue, LengthOrPercentageOrAuto, parse_unsigned_integer};
@ -840,7 +840,8 @@ impl HTMLImageElement {
let source_set = self.source_set.borrow(); let source_set = self.source_set.borrow();
let len = source_set.image_sources.len(); 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 { for outer_index in 0..len {
if repeat_indices.contains(&outer_index) { if repeat_indices.contains(&outer_index) {
continue; continue;

View file

@ -12,6 +12,7 @@ use crossbeam_channel::Sender;
use embedder_traits::{AnimationState, EventLoopWaker, TouchEventResult}; use embedder_traits::{AnimationState, EventLoopWaker, TouchEventResult};
use log::warn; use log::warn;
use malloc_size_of_derive::MallocSizeOf; use malloc_size_of_derive::MallocSizeOf;
use rustc_hash::FxHashMap;
use smallvec::SmallVec; use smallvec::SmallVec;
use strum_macros::IntoStaticStr; use strum_macros::IntoStaticStr;
use webrender_api::{DocumentId, FontVariation}; use webrender_api::{DocumentId, FontVariation};
@ -20,7 +21,6 @@ pub mod display_list;
pub mod rendering_context; pub mod rendering_context;
pub mod viewport_description; pub mod viewport_description;
use std::collections::HashMap;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use base::generic_channel::{self, GenericCallback, GenericSender}; use base::generic_channel::{self, GenericCallback, GenericSender};
@ -426,7 +426,7 @@ pub enum WebrenderImageHandlerType {
#[derive(Default)] #[derive(Default)]
pub struct WebrenderExternalImageRegistry { pub struct WebrenderExternalImageRegistry {
/// Map of all generated external images. /// Map of all generated external images.
external_images: HashMap<ExternalImageId, WebrenderImageHandlerType>, external_images: FxHashMap<ExternalImageId, WebrenderImageHandlerType>,
/// Id generator for the next external image identifier. /// Id generator for the next external image identifier.
next_image_id: u64, next_image_id: u64,
} }

View file

@ -14,20 +14,22 @@
//! we don't need to make the code more complex for it. The `mach` update command makes sure that //! we don't need to make the code more complex for it. The `mach` update command makes sure that
//! those cases are not present. //! those cases are not present.
use std::collections::HashSet;
use std::iter::FromIterator; use std::iter::FromIterator;
use std::sync::LazyLock; use std::sync::LazyLock;
use embedder_traits::resources::{self, Resource}; use embedder_traits::resources::{self, Resource};
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps}; use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
use malloc_size_of_derive::MallocSizeOf; use malloc_size_of_derive::MallocSizeOf;
use rustc_hash::FxHashSet;
use servo_url::{Host, ImmutableOrigin, ServoUrl}; 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)] #[derive(Clone, Debug, Default, MallocSizeOf)]
pub struct PubDomainRules { pub struct PubDomainRules {
rules: HashSet<String>, rules: FxHashSet<String>,
wildcards: HashSet<String>, wildcards: FxHashSet<String>,
exceptions: HashSet<String>, exceptions: FxHashSet<String>,
} }
static PUB_DOMAINS: LazyLock<PubDomainRules> = LazyLock::new(load_pub_domains); static PUB_DOMAINS: LazyLock<PubDomainRules> = LazyLock::new(load_pub_domains);

View file

@ -2,7 +2,6 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * 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/. */ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::collections::HashMap;
use std::num::NonZeroU32; use std::num::NonZeroU32;
use canvas_traits::webgl::{ use canvas_traits::webgl::{
@ -25,7 +24,7 @@ use crate::webgl_thread::{GLContextData, WebGLThread};
/// Bridge between WebGL and WebXR /// Bridge between WebGL and WebXR
pub(crate) struct WebXRBridge { pub(crate) struct WebXRBridge {
factory_receiver: crossbeam_channel::Receiver<WebXRLayerManagerFactory<WebXRSurfman>>, factory_receiver: crossbeam_channel::Receiver<WebXRLayerManagerFactory<WebXRSurfman>>,
managers: HashMap<WebXRLayerManagerId, Box<dyn WebXRLayerManagerAPI<WebXRSurfman>>>, managers: FxHashMap<WebXRLayerManagerId, Box<dyn WebXRLayerManagerAPI<WebXRSurfman>>>,
next_manager_id: NonZeroU32, next_manager_id: NonZeroU32,
} }
@ -34,7 +33,7 @@ impl WebXRBridge {
let WebXRBridgeInit { let WebXRBridgeInit {
factory_receiver, .. factory_receiver, ..
} = init; } = init;
let managers = HashMap::new(); let managers = FxHashMap::default();
let next_manager_id = NonZeroU32::MIN; let next_manager_id = NonZeroU32::MIN;
WebXRBridge { WebXRBridge {
factory_receiver, factory_receiver,