Replace Hash Algorithm in HashMap/Set with FxHashMap/Set for simple types (#39166)

FxHash is faster than FnvHash and SipHash for simple types up to at
least 64 bytes. The cryptographic guarantees are not needed for any
types changed here because they are simple ids.
This changes the types in script and net crates.
In a future PR we will change the remaining Fnv to be also Fx unless
there is a reason to keep them as Fnv.

Testing: Should not change functionality but unit test and wpt will find
it.

Signed-off-by: Narfinger <Narfinger@users.noreply.github.com>
This commit is contained in:
Narfinger 2025-09-09 10:33:46 +02:00 committed by GitHub
parent 1deb7b5957
commit 177f6d6502
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
46 changed files with 226 additions and 215 deletions

View file

@ -24,6 +24,7 @@ embedder_traits = { workspace = true }
euclid = { workspace = true }
fonts_traits = { workspace = true }
fnv = { workspace = true }
rustc-hash = { workspace = true }
http = { workspace = true }
hyper_serde = { workspace = true }
ipc-channel = { workspace = true }

View file

@ -8,14 +8,13 @@
mod serializable;
mod transferable;
use std::collections::HashMap;
use base::id::{
BlobId, DomExceptionId, DomMatrixId, DomPointId, DomQuadId, DomRectId, ImageBitmapId,
MessagePortId, OffscreenCanvasId, QuotaExceededErrorId,
};
use log::warn;
use malloc_size_of_derive::MallocSizeOf;
use rustc_hash::{FxBuildHasher, FxHashMap};
use serde::{Deserialize, Serialize};
pub use serializable::*;
use strum::IntoEnumIterator;
@ -28,35 +27,35 @@ pub struct StructuredSerializedData {
/// Data serialized by SpiderMonkey.
pub serialized: Vec<u8>,
/// Serialized in a structured callback,
pub blobs: Option<HashMap<BlobId, BlobImpl>>,
pub blobs: Option<FxHashMap<BlobId, BlobImpl>>,
/// Serialized point objects.
pub points: Option<HashMap<DomPointId, DomPoint>>,
pub points: Option<FxHashMap<DomPointId, DomPoint>>,
/// Serialized rect objects.
pub rects: Option<HashMap<DomRectId, DomRect>>,
pub rects: Option<FxHashMap<DomRectId, DomRect>>,
/// Serialized quad objects.
pub quads: Option<HashMap<DomQuadId, DomQuad>>,
pub quads: Option<FxHashMap<DomQuadId, DomQuad>>,
/// Serialized matrix objects.
pub matrices: Option<HashMap<DomMatrixId, DomMatrix>>,
pub matrices: Option<FxHashMap<DomMatrixId, DomMatrix>>,
/// Serialized exception objects.
pub exceptions: Option<HashMap<DomExceptionId, DomException>>,
pub exceptions: Option<FxHashMap<DomExceptionId, DomException>>,
/// Serialized quota exceeded errors.
pub quota_exceeded_errors:
Option<HashMap<QuotaExceededErrorId, SerializableQuotaExceededError>>,
Option<FxHashMap<QuotaExceededErrorId, SerializableQuotaExceededError>>,
/// Transferred objects.
pub ports: Option<HashMap<MessagePortId, MessagePortImpl>>,
pub ports: Option<FxHashMap<MessagePortId, MessagePortImpl>>,
/// Transform streams transferred objects.
pub transform_streams: Option<HashMap<MessagePortId, TransformStreamData>>,
pub transform_streams: Option<FxHashMap<MessagePortId, TransformStreamData>>,
/// Serialized image bitmap objects.
pub image_bitmaps: Option<HashMap<ImageBitmapId, SerializableImageBitmap>>,
pub image_bitmaps: Option<FxHashMap<ImageBitmapId, SerializableImageBitmap>>,
/// Transferred image bitmap objects.
pub transferred_image_bitmaps: Option<HashMap<ImageBitmapId, SerializableImageBitmap>>,
pub transferred_image_bitmaps: Option<FxHashMap<ImageBitmapId, SerializableImageBitmap>>,
/// Transferred offscreen canvas objects.
pub offscreen_canvases: Option<HashMap<OffscreenCanvasId, TransferableOffscreenCanvas>>,
pub offscreen_canvases: Option<FxHashMap<OffscreenCanvasId, TransferableOffscreenCanvas>>,
}
impl StructuredSerializedData {
fn is_empty(&self, val: Transferrable) -> bool {
fn is_field_empty<K, V>(field: &Option<HashMap<K, V>>) -> bool {
fn is_field_empty<K, V>(field: &Option<FxHashMap<K, V>>) -> bool {
field.as_ref().is_none_or(|h| h.is_empty())
}
match val {
@ -74,7 +73,7 @@ impl StructuredSerializedData {
fn clone_all_of_type<T: BroadcastClone>(&self, cloned: &mut StructuredSerializedData) {
let existing = T::source(self);
let Some(existing) = existing else { return };
let mut clones = HashMap::with_capacity(existing.len());
let mut clones = FxHashMap::with_capacity_and_hasher(existing.len(), FxBuildHasher);
for (original_id, obj) in existing.iter() {
if let Some(clone) = obj.clone_for_broadcast() {

View file

@ -8,7 +8,6 @@
//! be passed through the Constellation.
use std::cell::RefCell;
use std::collections::HashMap;
use std::path::PathBuf;
use base::id::{
@ -19,6 +18,7 @@ use euclid::default::Transform3D;
use malloc_size_of_derive::MallocSizeOf;
use net_traits::filemanager_thread::RelativePos;
use pixels::Snapshot;
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};
use servo_url::ImmutableOrigin;
use strum::EnumIter;
@ -36,9 +36,9 @@ where
/// Only return None if cloning is impossible.
fn clone_for_broadcast(&self) -> Option<Self>;
/// The field from which to clone values.
fn source(data: &StructuredSerializedData) -> &Option<HashMap<Self::Id, Self>>;
fn source(data: &StructuredSerializedData) -> &Option<FxHashMap<Self::Id, Self>>;
/// The field into which to place cloned values.
fn destination(data: &mut StructuredSerializedData) -> &mut Option<HashMap<Self::Id, Self>>;
fn destination(data: &mut StructuredSerializedData) -> &mut Option<FxHashMap<Self::Id, Self>>;
}
/// All the DOM interfaces that can be serialized.
@ -168,15 +168,11 @@ impl FileBlob {
impl BroadcastClone for BlobImpl {
type Id = BlobId;
fn source(
data: &StructuredSerializedData,
) -> &Option<std::collections::HashMap<Self::Id, Self>> {
fn source(data: &StructuredSerializedData) -> &Option<FxHashMap<Self::Id, Self>> {
&data.blobs
}
fn destination(
data: &mut StructuredSerializedData,
) -> &mut Option<std::collections::HashMap<Self::Id, Self>> {
fn destination(data: &mut StructuredSerializedData) -> &mut Option<FxHashMap<Self::Id, Self>> {
&mut data.blobs
}
@ -304,15 +300,11 @@ pub struct DomPoint {
impl BroadcastClone for DomPoint {
type Id = DomPointId;
fn source(
data: &StructuredSerializedData,
) -> &Option<std::collections::HashMap<Self::Id, Self>> {
fn source(data: &StructuredSerializedData) -> &Option<FxHashMap<Self::Id, Self>> {
&data.points
}
fn destination(
data: &mut StructuredSerializedData,
) -> &mut Option<std::collections::HashMap<Self::Id, Self>> {
fn destination(data: &mut StructuredSerializedData) -> &mut Option<FxHashMap<Self::Id, Self>> {
&mut data.points
}
@ -337,15 +329,11 @@ pub struct DomRect {
impl BroadcastClone for DomRect {
type Id = DomRectId;
fn source(
data: &StructuredSerializedData,
) -> &Option<std::collections::HashMap<Self::Id, Self>> {
fn source(data: &StructuredSerializedData) -> &Option<FxHashMap<Self::Id, Self>> {
&data.rects
}
fn destination(
data: &mut StructuredSerializedData,
) -> &mut Option<std::collections::HashMap<Self::Id, Self>> {
fn destination(data: &mut StructuredSerializedData) -> &mut Option<FxHashMap<Self::Id, Self>> {
&mut data.rects
}
@ -370,15 +358,11 @@ pub struct DomQuad {
impl BroadcastClone for DomQuad {
type Id = DomQuadId;
fn source(
data: &StructuredSerializedData,
) -> &Option<std::collections::HashMap<Self::Id, Self>> {
fn source(data: &StructuredSerializedData) -> &Option<FxHashMap<Self::Id, Self>> {
&data.quads
}
fn destination(
data: &mut StructuredSerializedData,
) -> &mut Option<std::collections::HashMap<Self::Id, Self>> {
fn destination(data: &mut StructuredSerializedData) -> &mut Option<FxHashMap<Self::Id, Self>> {
&mut data.quads
}
@ -399,15 +383,11 @@ pub struct DomMatrix {
impl BroadcastClone for DomMatrix {
type Id = DomMatrixId;
fn source(
data: &StructuredSerializedData,
) -> &Option<std::collections::HashMap<Self::Id, Self>> {
fn source(data: &StructuredSerializedData) -> &Option<FxHashMap<Self::Id, Self>> {
&data.matrices
}
fn destination(
data: &mut StructuredSerializedData,
) -> &mut Option<std::collections::HashMap<Self::Id, Self>> {
fn destination(data: &mut StructuredSerializedData) -> &mut Option<FxHashMap<Self::Id, Self>> {
&mut data.matrices
}
@ -426,15 +406,11 @@ pub struct DomException {
impl BroadcastClone for DomException {
type Id = DomExceptionId;
fn source(
data: &StructuredSerializedData,
) -> &Option<std::collections::HashMap<Self::Id, Self>> {
fn source(data: &StructuredSerializedData) -> &Option<FxHashMap<Self::Id, Self>> {
&data.exceptions
}
fn destination(
data: &mut StructuredSerializedData,
) -> &mut Option<std::collections::HashMap<Self::Id, Self>> {
fn destination(data: &mut StructuredSerializedData) -> &mut Option<FxHashMap<Self::Id, Self>> {
&mut data.exceptions
}
@ -454,11 +430,11 @@ pub struct SerializableQuotaExceededError {
impl BroadcastClone for SerializableQuotaExceededError {
type Id = QuotaExceededErrorId;
fn source(data: &StructuredSerializedData) -> &Option<HashMap<Self::Id, Self>> {
fn source(data: &StructuredSerializedData) -> &Option<FxHashMap<Self::Id, Self>> {
&data.quota_exceeded_errors
}
fn destination(data: &mut StructuredSerializedData) -> &mut Option<HashMap<Self::Id, Self>> {
fn destination(data: &mut StructuredSerializedData) -> &mut Option<FxHashMap<Self::Id, Self>> {
&mut data.quota_exceeded_errors
}
@ -476,15 +452,11 @@ pub struct SerializableImageBitmap {
impl BroadcastClone for SerializableImageBitmap {
type Id = ImageBitmapId;
fn source(
data: &StructuredSerializedData,
) -> &Option<std::collections::HashMap<Self::Id, Self>> {
fn source(data: &StructuredSerializedData) -> &Option<FxHashMap<Self::Id, Self>> {
&data.image_bitmaps
}
fn destination(
data: &mut StructuredSerializedData,
) -> &mut Option<std::collections::HashMap<Self::Id, Self>> {
fn destination(data: &mut StructuredSerializedData) -> &mut Option<FxHashMap<Self::Id, Self>> {
&mut data.image_bitmaps
}