mirror of
https://github.com/servo/servo.git
synced 2025-09-30 08:39:16 +01:00
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:
parent
1deb7b5957
commit
177f6d6502
46 changed files with 226 additions and 215 deletions
|
@ -5,9 +5,8 @@
|
|||
//! Trait representing the concept of [serializable objects]
|
||||
//! (<https://html.spec.whatwg.org/multipage/#serializable-objects>).
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use base::id::{Index, NamespaceIndex, PipelineNamespaceId};
|
||||
use rustc_hash::FxHashMap;
|
||||
use script_bindings::structuredclone::MarkedAsSerializableInIdl;
|
||||
|
||||
use crate::dom::bindings::reflector::DomObject;
|
||||
|
@ -68,7 +67,7 @@ where
|
|||
/// should be used to read/store serialized instances of this type.
|
||||
fn serialized_storage<'a>(
|
||||
data: StructuredData<'a, '_>,
|
||||
) -> &'a mut Option<HashMap<NamespaceIndex<Self::Index>, Self::Data>>;
|
||||
) -> &'a mut Option<FxHashMap<NamespaceIndex<Self::Index>, Self::Data>>;
|
||||
}
|
||||
|
||||
pub(crate) fn assert_serializable<T: Serializable>() {}
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -40,6 +40,7 @@ use js::glue::{CallScriptTracer, CallStringTracer, CallValueTracer};
|
|||
use js::jsapi::{GCTraceKindToAscii, Heap, JSScript, JSString, JSTracer, TraceKind};
|
||||
use js::jsval::JSVal;
|
||||
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
|
||||
use rustc_hash::FxBuildHasher;
|
||||
pub(crate) use script_bindings::trace::*;
|
||||
|
||||
use crate::dom::bindings::cell::DomRefCell;
|
||||
|
@ -92,6 +93,9 @@ impl<T: MallocSizeOf> MallocSizeOf for NoTrace<T> {
|
|||
/// HashMap wrapper, that has non-jsmanaged keys
|
||||
///
|
||||
/// Not all methods are reexposed, but you can access inner type via .0
|
||||
/// If you need cryptographic secure hashs, or your keys are arbitrary large inputs
|
||||
/// stick with the default hasher. Otherwise, stronlgy think about using FxHashBuilder
|
||||
/// with `new_fx()`
|
||||
#[cfg_attr(crown, crown::trace_in_no_trace_lint::must_not_have_traceable(0))]
|
||||
#[derive(Clone, Debug)]
|
||||
pub(crate) struct HashMapTracedValues<K, V, S = RandomState>(pub(crate) HashMap<K, V, S>);
|
||||
|
@ -111,6 +115,14 @@ impl<K, V> HashMapTracedValues<K, V, RandomState> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<K, V> HashMapTracedValues<K, V, FxBuildHasher> {
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub(crate) fn new_fx() -> HashMapTracedValues<K, V, FxBuildHasher> {
|
||||
Self(HashMap::with_hasher(FxBuildHasher))
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V, S> HashMapTracedValues<K, V, S> {
|
||||
#[inline]
|
||||
pub(crate) fn iter(&self) -> std::collections::hash_map::Iter<'_, K, V> {
|
||||
|
|
|
@ -5,10 +5,10 @@
|
|||
//! Trait representing the concept of [transferable objects]
|
||||
//! (<https://html.spec.whatwg.org/multipage/#transferable-objects>).
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::hash::Hash;
|
||||
|
||||
use base::id::NamespaceIndex;
|
||||
use rustc_hash::FxHashMap;
|
||||
use script_bindings::structuredclone::MarkedAsTransferableInIdl;
|
||||
|
||||
use crate::dom::bindings::error::Fallible;
|
||||
|
@ -40,7 +40,7 @@ where
|
|||
|
||||
fn serialized_storage<'a>(
|
||||
data: StructuredData<'a, '_>,
|
||||
) -> &'a mut Option<HashMap<NamespaceIndex<Self::Index>, Self::Data>>;
|
||||
) -> &'a mut Option<FxHashMap<NamespaceIndex<Self::Index>, Self::Data>>;
|
||||
}
|
||||
|
||||
pub(crate) fn assert_transferable<T: Transferable>() {}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue