diff options
author | Joe Wilm <joe@jwilm.com> | 2017-01-13 10:03:51 -0800 |
---|---|---|
committer | Joe Wilm <jwilm@users.noreply.github.com> | 2017-01-26 08:43:53 -0800 |
commit | 08f348ecea0b782cd8539850abe6309d0e5b06c9 (patch) | |
tree | fe2bdb4795e82918bbdcc399d1ebf5ce8c5c47e9 /font/src | |
parent | 4ebcbf1c4d02d83b023cdb93a521b55a150418ee (diff) | |
download | alacritty-08f348ecea0b782cd8539850abe6309d0e5b06c9.tar.gz alacritty-08f348ecea0b782cd8539850abe6309d0e5b06c9.zip |
Optimize glyph cache access
Loading a glyph from the cache is a very hot operation in the renderer.
The original implementation would first check if a glyph was loaded and
then call `get()` which would have to search a second time. This showed
up as a very slow point in profiles.
This patch addresses glyph cache access in two ways: by using a faster
hasher optimized for small keys (fnv), and by using the entry API for
fetching a cached glyph. The `fnv` hasher is faster than the default and
is very efficient for small keys. Using the entry API on the HashMap
means only 1 lookup instead of two. The entry API has a downside where
the key needs to get cloned on fetches.
Reducing the GlyphKey width to 64-bits helps in both areas. Copying an
8-byte wide type is very cheap and thus limits downside of the entry
API. The small width also helps with the hasher performance.
Over all, this patch reduced typical render times by several hundred
microseconds on a 2013 MacBook Pro with a full screen terminal full of
text.
Diffstat (limited to 'font/src')
-rw-r--r-- | font/src/lib.rs | 38 |
1 files changed, 32 insertions, 6 deletions
diff --git a/font/src/lib.rs b/font/src/lib.rs index bb9b9f3c..c5c0a2c7 100644 --- a/font/src/lib.rs +++ b/font/src/lib.rs @@ -42,8 +42,9 @@ extern crate ffi_util; #[macro_use] extern crate log; +use std::hash::{Hash, Hasher}; use std::fmt; -use std::sync::atomic::{AtomicU32, ATOMIC_U32_INIT, Ordering}; +use std::sync::atomic::{AtomicU16, ATOMIC_U16_INIT, Ordering}; // If target isn't macos, reexport everything from ft #[cfg(not(target_os = "macos"))] @@ -114,7 +115,7 @@ impl fmt::Display for FontDesc { /// Identifier for a Font for use in maps/etc #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)] pub struct FontKey { - token: u32, + token: u16, } impl FontKey { @@ -122,7 +123,7 @@ impl FontKey { /// /// The generated key will be globally unique pub fn next() -> FontKey { - static TOKEN: AtomicU32 = ATOMIC_U32_INIT; + static TOKEN: AtomicU16 = ATOMIC_U16_INIT; FontKey { token: TOKEN.fetch_add(1, Ordering::SeqCst), @@ -130,16 +131,28 @@ impl FontKey { } } -#[derive(Debug, Clone, Hash, PartialEq, Eq)] +#[derive(Debug, Copy, Clone, PartialEq, Eq)] pub struct GlyphKey { pub c: char, pub font_key: FontKey, pub size: Size, } +impl Hash for GlyphKey { + fn hash<H: Hasher>(&self, state: &mut H) { + unsafe { + // This transmute is fine: + // + // - If GlyphKey ever becomes a different size, this will fail to compile + // - Result is being used for hashing and has no fields (it's a u64) + ::std::mem::transmute::<GlyphKey, u64>(*self) + }.hash(state); + } +} + /// Font size stored as integer #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] -pub struct Size(i32); +pub struct Size(i16); impl Size { /// Scale factor between font "Size" type and point size @@ -150,7 +163,7 @@ impl Size { /// Create a new `Size` from a f32 size in points pub fn new(size: f32) -> Size { - Size((size * Size::factor()) as i32) + Size((size * Size::factor()) as i16) } /// Get the f32 size in points @@ -168,6 +181,19 @@ pub struct RasterizedGlyph { pub buf: Vec<u8>, } +impl Default for RasterizedGlyph { + fn default() -> RasterizedGlyph { + RasterizedGlyph { + c: ' ', + width: 0, + height: 0, + top: 0, + left: 0, + buf: Vec::new(), + } + } +} + struct BufDebugger<'a>(&'a [u8]); impl<'a> fmt::Debug for BufDebugger<'a> { |