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

@ -4,7 +4,6 @@
//! This module implements structured cloning, as defined by [HTML](https://html.spec.whatwg.org/multipage/#safe-passing-of-structured-data).
use std::collections::HashMap;
use std::ffi::CStr;
use std::os::raw;
use std::ptr;
@ -34,6 +33,7 @@ use js::rust::wrappers::{JS_ReadStructuredClone, JS_WriteStructuredClone};
use js::rust::{
CustomAutoRooterGuard, HandleValue, JSAutoStructuredCloneBufferWrapper, MutableHandleValue,
};
use rustc_hash::FxHashMap;
use script_bindings::conversions::{IDLInterface, SafeToJSValConvertible};
use strum::IntoEnumIterator;
@ -191,7 +191,7 @@ unsafe fn write_object<T: Serializable>(
) -> bool {
if let Ok((new_id, serialized)) = object.serialize() {
let objects = T::serialized_storage(StructuredData::Writer(sc_writer))
.get_or_insert_with(HashMap::new);
.get_or_insert(FxHashMap::default());
objects.insert(new_id, serialized);
let storage_key = StorageKey::new(new_id);
@ -416,8 +416,8 @@ unsafe fn try_transfer<T: Transferable + IDLInterface>(
let (id, object) = object.transfer().map_err(OperationError::Exception)?;
// 2. Store the transferred object at a given key.
let objects =
T::serialized_storage(StructuredData::Writer(sc_writer)).get_or_insert_with(HashMap::new);
let objects = T::serialized_storage(StructuredData::Writer(sc_writer))
.get_or_insert(FxHashMap::default());
objects.insert(id, object);
let index = id.index.0.get();
@ -587,32 +587,33 @@ pub(crate) struct StructuredDataReader<'a> {
/// A map of port implementations,
/// used as part of the "transfer-receiving" steps of ports,
/// to produce the DOM ports stored in `message_ports` above.
pub(crate) port_impls: Option<HashMap<MessagePortId, MessagePortImpl>>,
pub(crate) port_impls: Option<FxHashMap<MessagePortId, MessagePortImpl>>,
/// A map of transform stream implementations,
pub(crate) transform_streams_port_impls: Option<HashMap<MessagePortId, TransformStreamData>>,
pub(crate) transform_streams_port_impls: Option<FxHashMap<MessagePortId, TransformStreamData>>,
/// A map of blob implementations,
/// used as part of the "deserialize" steps of blobs,
/// to produce the DOM blobs stored in `blobs` above.
pub(crate) blob_impls: Option<HashMap<BlobId, BlobImpl>>,
pub(crate) blob_impls: Option<FxHashMap<BlobId, BlobImpl>>,
/// A map of serialized points.
pub(crate) points: Option<HashMap<DomPointId, DomPoint>>,
pub(crate) points: Option<FxHashMap<DomPointId, DomPoint>>,
/// A map of serialized rects.
pub(crate) rects: Option<HashMap<DomRectId, DomRect>>,
pub(crate) rects: Option<FxHashMap<DomRectId, DomRect>>,
/// A map of serialized quads.
pub(crate) quads: Option<HashMap<DomQuadId, DomQuad>>,
pub(crate) quads: Option<FxHashMap<DomQuadId, DomQuad>>,
/// A map of serialized matrices.
pub(crate) matrices: Option<HashMap<DomMatrixId, DomMatrix>>,
pub(crate) matrices: Option<FxHashMap<DomMatrixId, DomMatrix>>,
/// A map of serialized exceptions.
pub(crate) exceptions: Option<HashMap<DomExceptionId, DomException>>,
pub(crate) exceptions: Option<FxHashMap<DomExceptionId, DomException>>,
/// A map of serialized quota exceeded errors.
pub(crate) quota_exceeded_errors:
Option<HashMap<QuotaExceededErrorId, SerializableQuotaExceededError>>,
Option<FxHashMap<QuotaExceededErrorId, SerializableQuotaExceededError>>,
// A map of serialized image bitmaps.
pub(crate) image_bitmaps: Option<HashMap<ImageBitmapId, SerializableImageBitmap>>,
pub(crate) image_bitmaps: Option<FxHashMap<ImageBitmapId, SerializableImageBitmap>>,
/// A map of transferred image bitmaps.
pub(crate) transferred_image_bitmaps: Option<HashMap<ImageBitmapId, SerializableImageBitmap>>,
pub(crate) transferred_image_bitmaps: Option<FxHashMap<ImageBitmapId, SerializableImageBitmap>>,
/// A map of transferred offscreen canvases.
pub(crate) offscreen_canvases: Option<HashMap<OffscreenCanvasId, TransferableOffscreenCanvas>>,
pub(crate) offscreen_canvases:
Option<FxHashMap<OffscreenCanvasId, TransferableOffscreenCanvas>>,
}
/// A data holder for transferred and serialized objects.
@ -622,30 +623,31 @@ pub(crate) struct StructuredDataWriter {
/// Error record.
pub(crate) error: Option<Error>,
/// Transferred ports.
pub(crate) ports: Option<HashMap<MessagePortId, MessagePortImpl>>,
pub(crate) ports: Option<FxHashMap<MessagePortId, MessagePortImpl>>,
/// Transferred transform streams.
pub(crate) transform_streams_port: Option<HashMap<MessagePortId, TransformStreamData>>,
pub(crate) transform_streams_port: Option<FxHashMap<MessagePortId, TransformStreamData>>,
/// Serialized points.
pub(crate) points: Option<HashMap<DomPointId, DomPoint>>,
pub(crate) points: Option<FxHashMap<DomPointId, DomPoint>>,
/// Serialized rects.
pub(crate) rects: Option<HashMap<DomRectId, DomRect>>,
pub(crate) rects: Option<FxHashMap<DomRectId, DomRect>>,
/// Serialized quads.
pub(crate) quads: Option<HashMap<DomQuadId, DomQuad>>,
pub(crate) quads: Option<FxHashMap<DomQuadId, DomQuad>>,
/// Serialized matrices.
pub(crate) matrices: Option<HashMap<DomMatrixId, DomMatrix>>,
pub(crate) matrices: Option<FxHashMap<DomMatrixId, DomMatrix>>,
/// Serialized exceptions.
pub(crate) exceptions: Option<HashMap<DomExceptionId, DomException>>,
pub(crate) exceptions: Option<FxHashMap<DomExceptionId, DomException>>,
/// Serialized quota exceeded errors.
pub(crate) quota_exceeded_errors:
Option<HashMap<QuotaExceededErrorId, SerializableQuotaExceededError>>,
Option<FxHashMap<QuotaExceededErrorId, SerializableQuotaExceededError>>,
/// Serialized blobs.
pub(crate) blobs: Option<HashMap<BlobId, BlobImpl>>,
pub(crate) blobs: Option<FxHashMap<BlobId, BlobImpl>>,
/// Serialized image bitmaps.
pub(crate) image_bitmaps: Option<HashMap<ImageBitmapId, SerializableImageBitmap>>,
pub(crate) image_bitmaps: Option<FxHashMap<ImageBitmapId, SerializableImageBitmap>>,
/// Transferred image bitmaps.
pub(crate) transferred_image_bitmaps: Option<HashMap<ImageBitmapId, SerializableImageBitmap>>,
pub(crate) transferred_image_bitmaps: Option<FxHashMap<ImageBitmapId, SerializableImageBitmap>>,
/// Transferred offscreen canvases.
pub(crate) offscreen_canvases: Option<HashMap<OffscreenCanvasId, TransferableOffscreenCanvas>>,
pub(crate) offscreen_canvases:
Option<FxHashMap<OffscreenCanvasId, TransferableOffscreenCanvas>>,
}
/// Writes a structured clone. Returns a `DataClone` error if that fails.