From 7b9f32300ee0a249c0872302c97635b460e45ba5 Mon Sep 17 00:00:00 2001 From: Kirill Chibisov Date: Mon, 24 Jul 2023 06:11:25 +0000 Subject: Use ahash instead of fnv and regular hash function After evaluation of the ahash with the data alacritty uses it was discovered that it's 1.5-2x times faster when getting the already hashed values, which is the primary cases for alacritty's renderer. Given that ahash is generally faster, all the HashSet and HashMap's inside the alacritty were changed to use it as a hasher function. --- alacritty/src/display/hint.rs | 3 ++- alacritty/src/event.rs | 11 ++++++----- alacritty/src/input.rs | 2 +- alacritty/src/renderer/mod.rs | 7 ++++--- alacritty/src/renderer/rects.rs | 3 ++- alacritty/src/renderer/text/glyph_cache.rs | 7 +++---- 6 files changed, 18 insertions(+), 15 deletions(-) (limited to 'alacritty/src') diff --git a/alacritty/src/display/hint.rs b/alacritty/src/display/hint.rs index 7e2e4126..57087e45 100644 --- a/alacritty/src/display/hint.rs +++ b/alacritty/src/display/hint.rs @@ -2,6 +2,7 @@ use std::cmp::Reverse; use std::collections::HashSet; use std::iter; +use ahash::RandomState; use winit::keyboard::ModifiersState; use alacritty_terminal::grid::{BidirectionalIterator, Dimensions}; @@ -307,7 +308,7 @@ pub fn visible_unique_hyperlinks_iter(term: &Term) -> impl Iterator::default(); iter::from_fn(move || { // Find the start of the next unique hyperlink. diff --git a/alacritty/src/event.rs b/alacritty/src/event.rs index 426b207e..0ffc46a0 100644 --- a/alacritty/src/event.rs +++ b/alacritty/src/event.rs @@ -14,6 +14,7 @@ use std::sync::atomic::Ordering; use std::time::{Duration, Instant}; use std::{env, f32, mem}; +use ahash::RandomState; use log::{debug, error, info, warn}; #[cfg(all(feature = "wayland", not(any(target_os = "macos", windows))))] use wayland_client::{Display as WaylandDisplay, EventQueue}; @@ -1044,7 +1045,7 @@ pub enum TouchPurpose { Scroll(TouchEvent), Zoom(TouchZoom), Tap(TouchEvent), - Invalid(HashSet), + Invalid(HashSet), } impl Default for TouchPurpose { @@ -1085,8 +1086,8 @@ impl TouchZoom { } /// Get active touch slots. - pub fn slots(&self) -> HashSet { - let mut set = HashSet::new(); + pub fn slots(&self) -> HashSet { + let mut set = HashSet::default(); set.insert(self.slots.0.id); set.insert(self.slots.1.id); set @@ -1401,7 +1402,7 @@ impl input::Processor> { pub struct Processor { #[cfg(all(feature = "wayland", not(any(target_os = "macos", windows))))] wayland_event_queue: Option, - windows: HashMap, + windows: HashMap, cli_options: CliOptions, config: Rc, } @@ -1423,7 +1424,7 @@ impl Processor { }); Processor { - windows: HashMap::new(), + windows: Default::default(), config: Rc::new(config), cli_options, #[cfg(all(feature = "wayland", not(any(target_os = "macos", windows))))] diff --git a/alacritty/src/input.rs b/alacritty/src/input.rs index a8fb09b5..56832aff 100644 --- a/alacritty/src/input.rs +++ b/alacritty/src/input.rs @@ -780,7 +780,7 @@ impl> Processor { TouchPurpose::Tap(start) => TouchPurpose::Zoom(TouchZoom::new((start, touch))), TouchPurpose::Zoom(zoom) => TouchPurpose::Invalid(zoom.slots()), TouchPurpose::Scroll(event) | TouchPurpose::Select(event) => { - let mut set = HashSet::new(); + let mut set = HashSet::default(); set.insert(event.id); TouchPurpose::Invalid(set) }, diff --git a/alacritty/src/renderer/mod.rs b/alacritty/src/renderer/mod.rs index fc586dc9..98e29542 100644 --- a/alacritty/src/renderer/mod.rs +++ b/alacritty/src/renderer/mod.rs @@ -3,6 +3,7 @@ use std::ffi::{CStr, CString}; use std::sync::atomic::{AtomicBool, Ordering}; use std::{fmt, ptr}; +use ahash::RandomState; use crossfont::Metrics; use glutin::context::{ContextApi, GlContext, PossiblyCurrentContext}; use glutin::display::{GetGlDisplay, GlDisplay}; @@ -290,13 +291,13 @@ impl GlExtensions { /// /// This function will lazyly load OpenGL extensions. fn contains(extension: &str) -> bool { - static OPENGL_EXTENSIONS: OnceCell> = OnceCell::new(); + static OPENGL_EXTENSIONS: OnceCell> = OnceCell::new(); OPENGL_EXTENSIONS.get_or_init(Self::load_extensions).contains(extension) } /// Load available OpenGL extensions. - fn load_extensions() -> HashSet<&'static str> { + fn load_extensions() -> HashSet<&'static str, RandomState> { unsafe { let extensions = gl::GetString(gl::EXTENSIONS); @@ -313,7 +314,7 @@ impl GlExtensions { } else { match CStr::from_ptr(extensions as *mut _).to_str() { Ok(ext) => ext.split_whitespace().collect(), - Err(_) => HashSet::new(), + Err(_) => Default::default(), } } } diff --git a/alacritty/src/renderer/rects.rs b/alacritty/src/renderer/rects.rs index 8f6204c2..8d7a78e6 100644 --- a/alacritty/src/renderer/rects.rs +++ b/alacritty/src/renderer/rects.rs @@ -1,6 +1,7 @@ use std::collections::HashMap; use std::mem; +use ahash::RandomState; use crossfont::Metrics; use alacritty_terminal::grid::Dimensions; @@ -157,7 +158,7 @@ impl RenderLine { /// Lines for underline and strikeout. #[derive(Default)] pub struct RenderLines { - inner: HashMap>, + inner: HashMap, RandomState>, } impl RenderLines { diff --git a/alacritty/src/renderer/text/glyph_cache.rs b/alacritty/src/renderer/text/glyph_cache.rs index 72415900..a750e4e7 100644 --- a/alacritty/src/renderer/text/glyph_cache.rs +++ b/alacritty/src/renderer/text/glyph_cache.rs @@ -1,11 +1,10 @@ use std::collections::HashMap; -use std::hash::BuildHasherDefault; +use ahash::RandomState; use crossfont::{ Error as RasterizerError, FontDesc, FontKey, GlyphKey, Metrics, Rasterize, RasterizedGlyph, Rasterizer, Size, Slant, Style, Weight, }; -use fnv::FnvHasher; use log::{error, info}; use unicode_width::UnicodeWidthChar; @@ -46,7 +45,7 @@ pub struct Glyph { /// representations of the same code point. pub struct GlyphCache { /// Cache of buffered glyphs. - cache: HashMap>, + cache: HashMap, /// Rasterizer for loading new glyphs. rasterizer: Rasterizer, @@ -91,7 +90,7 @@ impl GlyphCache { let metrics = rasterizer.metrics(regular, font.size())?; Ok(Self { - cache: HashMap::default(), + cache: Default::default(), rasterizer, font_size: font.size(), font_key: regular, -- cgit