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

@ -5,11 +5,12 @@
//! Machinery for [task-queue](https://html.spec.whatwg.org/multipage/#task-queue).
use std::cell::Cell;
use std::collections::{HashMap, HashSet, VecDeque};
use std::collections::VecDeque;
use std::default::Default;
use base::id::PipelineId;
use crossbeam_channel::{self, Receiver, Sender};
use rustc_hash::{FxHashMap, FxHashSet};
use strum::VariantArray;
use crate::dom::bindings::cell::DomRefCell;
@ -47,9 +48,9 @@ pub(crate) struct TaskQueue<T> {
/// A "business" counter, reset for each iteration of the event-loop
taken_task_counter: Cell<u64>,
/// Tasks that will be throttled for as long as we are "busy".
throttled: DomRefCell<HashMap<TaskSourceName, VecDeque<QueuedTask>>>,
throttled: DomRefCell<FxHashMap<TaskSourceName, VecDeque<QueuedTask>>>,
/// Tasks for not fully-active documents.
inactive: DomRefCell<HashMap<PipelineId, VecDeque<QueuedTask>>>,
inactive: DomRefCell<FxHashMap<PipelineId, VecDeque<QueuedTask>>>,
}
impl<T: QueuedTaskConversion> TaskQueue<T> {
@ -68,7 +69,7 @@ impl<T: QueuedTaskConversion> TaskQueue<T> {
/// <https://html.spec.whatwg.org/multipage/#event-loop-processing-model:fully-active>
fn release_tasks_for_fully_active_documents(
&self,
fully_active: &HashSet<PipelineId>,
fully_active: &FxHashSet<PipelineId>,
) -> Vec<T> {
self.inactive
.borrow_mut()
@ -103,7 +104,7 @@ impl<T: QueuedTaskConversion> TaskQueue<T> {
/// Process incoming tasks, immediately sending priority ones downstream,
/// and categorizing potential throttles.
fn process_incoming_tasks(&self, first_msg: T, fully_active: &HashSet<PipelineId>) {
fn process_incoming_tasks(&self, first_msg: T, fully_active: &FxHashSet<PipelineId>) {
// 1. Make any previously stored task from now fully-active document available.
let mut incoming = self.release_tasks_for_fully_active_documents(fully_active);
@ -196,14 +197,17 @@ impl<T: QueuedTaskConversion> TaskQueue<T> {
}
/// Take all tasks again and then run `recv()`.
pub(crate) fn take_tasks_and_recv(&self, fully_active: &HashSet<PipelineId>) -> Result<T, ()> {
pub(crate) fn take_tasks_and_recv(
&self,
fully_active: &FxHashSet<PipelineId>,
) -> Result<T, ()> {
self.take_tasks(T::wake_up_msg(), fully_active);
self.recv()
}
/// Drain the queue for the current iteration of the event-loop.
/// Holding-back throttles above a given high-water mark.
pub(crate) fn take_tasks(&self, first_msg: T, fully_active: &HashSet<PipelineId>) {
pub(crate) fn take_tasks(&self, first_msg: T, fully_active: &FxHashSet<PipelineId>) {
// High-watermark: once reached, throttled tasks will be held-back.
const PER_ITERATION_MAX: u64 = 5;
// Always first check for new tasks, but don't reset 'taken_task_counter'.