constellation: Use FnvHashMap for hashmaps that use ids as keys (#39106)

FNV is faster for hashing less than 16 bytes of data and the
cryptographic properties of the default HashMap are not needed for the
various ids.

Testing: This does not change functionality.

Signed-off-by: Narfinger <Narfinger@users.noreply.github.com>
This commit is contained in:
Narfinger 2025-09-03 20:15:19 +02:00 committed by GitHub
parent 0ae9ee28d5
commit 5c7ea4bdee
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 71 additions and 54 deletions

View file

@ -22,6 +22,7 @@ crossbeam-channel = { workspace = true }
dpi = { version = "0.1" }
embedder_traits = { workspace = true }
euclid = { workspace = true }
fnv = { workspace = true }
gleam = { workspace = true }
glow = { workspace = true }
image = { workspace = true }

View file

@ -12,6 +12,7 @@ use base::print_tree::PrintTree;
use bitflags::bitflags;
use embedder_traits::ViewportDetails;
use euclid::SideOffsets2D;
use fnv::FnvHashMap;
use malloc_size_of_derive::MallocSizeOf;
use serde::{Deserialize, Serialize};
use servo_geometry::FastLayoutTransform;
@ -558,7 +559,10 @@ impl ScrollTree {
/// Given a set of all scroll offsets coming from the Servo renderer, update all of the offsets
/// for nodes that actually exist in this tree.
pub fn set_all_scroll_offsets(&mut self, offsets: &HashMap<ExternalScrollId, LayoutVector2D>) {
pub fn set_all_scroll_offsets(
&mut self,
offsets: &FnvHashMap<ExternalScrollId, LayoutVector2D>,
) {
for node in self.nodes.iter_mut() {
if let SpatialTreeNodeInfo::Scroll(ref mut scroll_info) = node.info {
if let Some(offset) = offsets.get(&scroll_info.external_id) {
@ -583,7 +587,7 @@ impl ScrollTree {
/// Collect all of the scroll offsets of the scrolling nodes of this tree into a
/// [`HashMap`] which can be applied to another tree.
pub fn scroll_offsets(&self) -> HashMap<ExternalScrollId, LayoutVector2D> {
pub fn scroll_offsets(&self) -> FnvHashMap<ExternalScrollId, LayoutVector2D> {
HashMap::from_iter(self.nodes.iter().filter_map(|node| match node.info {
SpatialTreeNodeInfo::Scroll(ref scroll_info) => {
Some((scroll_info.external_id, scroll_info.offset))

View file

@ -23,6 +23,7 @@ devtools_traits = { workspace = true }
embedder_traits = { workspace = true }
euclid = { workspace = true }
fonts_traits = { workspace = true }
fnv = { workspace = true }
http = { workspace = true }
hyper_serde = { workspace = true }
ipc-channel = { workspace = true }

View file

@ -4,7 +4,6 @@
//! Messages send from the ScriptThread to the Constellation.
use std::collections::HashMap;
use std::fmt;
use base::Epoch;
@ -22,6 +21,7 @@ use embedder_traits::{
ViewportDetails, WebDriverMessageId,
};
use euclid::default::Size2D as UntypedSize2D;
use fnv::FnvHashMap;
use fonts_traits::SystemFontServiceProxySender;
use http::{HeaderMap, Method};
use ipc_channel::ipc::IpcSender;
@ -492,7 +492,7 @@ pub enum ScriptToConstellationMessage {
/* The ids of ports transferred successfully */
Vec<MessagePortId>,
/* The ids, and buffers, of ports whose transfer failed */
HashMap<MessagePortId, PortTransferInfo>,
FnvHashMap<MessagePortId, PortTransferInfo>,
),
/// A new message-port was created or transferred, with corresponding control-sender.
NewMessagePort(MessagePortRouterId, MessagePortId),

View file

@ -11,7 +11,7 @@
mod from_script_message;
mod structured_data;
use std::collections::{HashMap, VecDeque};
use std::collections::VecDeque;
use std::fmt;
use std::time::Duration;
@ -22,6 +22,7 @@ use embedder_traits::{
CompositorHitTestResult, FocusId, InputEvent, JavaScriptEvaluationId, MediaSessionActionType,
Theme, TraversalId, ViewportDetails, WebDriverCommandMsg, WebDriverCommandResponse,
};
use fnv::FnvHashMap;
pub use from_script_message::*;
use ipc_channel::ipc::IpcSender;
use malloc_size_of_derive::MallocSizeOf;
@ -41,7 +42,7 @@ pub enum EmbedderToConstellationMessage {
/// Exit the constellation.
Exit,
/// Query the constellation to see if the current compositor output is stable
IsReadyToSaveImage(HashMap<PipelineId, Epoch>),
IsReadyToSaveImage(FnvHashMap<PipelineId, Epoch>),
/// Whether to allow script to navigate.
AllowNavigationResponse(PipelineId, bool),
/// Request to load a page.
@ -94,7 +95,7 @@ pub enum EmbedderToConstellationMessage {
SetWebViewThrottled(WebViewId, bool),
/// The Servo renderer scrolled and is updating the scroll states of the nodes in the
/// given pipeline via the constellation.
SetScrollStates(PipelineId, HashMap<ExternalScrollId, LayoutVector2D>),
SetScrollStates(PipelineId, FnvHashMap<ExternalScrollId, LayoutVector2D>),
/// Notify the constellation that a particular paint metric event has happened for the given pipeline.
PaintMetric(PipelineId, PaintMetricEvent),
/// Evaluate a JavaScript string in the context of a `WebView`. When execution is complete or an
@ -180,7 +181,7 @@ pub struct PortTransferInfo {
#[allow(clippy::large_enum_variant)]
pub enum MessagePortMsg {
/// Complete the transfer for a batch of ports.
CompleteTransfer(HashMap<MessagePortId, PortTransferInfo>),
CompleteTransfer(FnvHashMap<MessagePortId, PortTransferInfo>),
/// Complete the transfer of a single port,
/// whose transfer was pending because it had been requested
/// while a previous failed transfer was being rolled-back.

View file

@ -12,7 +12,6 @@ mod layout_damage;
pub mod wrapper_traits;
use std::any::Any;
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::atomic::{AtomicIsize, AtomicU64, Ordering};
use std::thread::JoinHandle;
@ -281,7 +280,7 @@ pub trait Layout {
/// Set the scroll states of this layout after a compositor scroll.
fn set_scroll_offsets_from_renderer(
&mut self,
scroll_states: &HashMap<ExternalScrollId, LayoutVector2D>,
scroll_states: &FnvHashMap<ExternalScrollId, LayoutVector2D>,
);
/// Get the scroll offset of the given scroll node with id of [`ExternalScrollId`] or `None` if it does

View file

@ -26,6 +26,7 @@ crossbeam-channel = { workspace = true }
devtools_traits = { workspace = true }
embedder_traits = { workspace = true }
euclid = { workspace = true }
fnv = { workspace = true }
ipc-channel = { workspace = true }
keyboard-types = { workspace = true }
malloc_size_of = { workspace = true }

View file

@ -9,7 +9,6 @@
#![deny(missing_docs)]
#![deny(unsafe_code)]
use std::collections::HashMap;
use std::fmt;
use std::sync::Arc;
@ -33,6 +32,7 @@ use embedder_traits::{
MediaSessionActionType, ScriptToEmbedderChan, Theme, ViewportDetails, WebDriverScriptCommand,
};
use euclid::{Rect, Scale, Size2D, UnknownUnit};
use fnv::FnvHashMap;
use ipc_channel::ipc::{IpcReceiver, IpcSender};
use keyboard_types::Modifiers;
use malloc_size_of_derive::MallocSizeOf;
@ -253,7 +253,7 @@ pub enum ScriptThreadMessage {
SetWebGPUPort(IpcReceiver<WebGPUMsg>),
/// The compositor scrolled and is updating the scroll states of the nodes in the given
/// pipeline via the Constellation.
SetScrollStates(PipelineId, HashMap<ExternalScrollId, LayoutVector2D>),
SetScrollStates(PipelineId, FnvHashMap<ExternalScrollId, LayoutVector2D>),
/// Evaluate the given JavaScript and return a result via a corresponding message
/// to the Constellation.
EvaluateJavaScript(PipelineId, JavaScriptEvaluationId, String),