From b2d01bc3f0d1413169380c5a6d00e2df129f233c Mon Sep 17 00:00:00 2001 From: Aaron Muir Hamilton Date: Wed, 6 Nov 2024 12:49:38 -0500 Subject: [PATCH] Use `usize` for cache keys instead of `u64`. This improves portability, because `AtomicUsize` is available on 32-bit targets, but generally `AtomicU64` is not. --- src/cache.rs | 16 ++++++++-------- src/scale/hinting_cache.rs | 4 ++-- src/scale/mod.rs | 2 +- src/shape/cache.rs | 6 +++--- src/shape/mod.rs | 8 ++++---- src/shape/partition.rs | 4 ++-- 6 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/cache.rs b/src/cache.rs index b37559a..a94680d 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -3,18 +3,18 @@ use alloc::vec::Vec; /// Uniquely generated value for identifying and caching fonts. #[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)] -pub struct CacheKey(pub(crate) u64); +pub struct CacheKey(pub(crate) usize); impl CacheKey { /// Generates a new cache key. pub fn new() -> Self { use core::sync::atomic::{AtomicUsize, Ordering}; static KEY: AtomicUsize = AtomicUsize::new(1); - Self(KEY.fetch_add(1, Ordering::Relaxed).try_into().unwrap()) + Self(KEY.fetch_add(1, Ordering::Relaxed)) } /// Returns the underlying value of the key. - pub fn value(self) -> u64 { + pub fn value(self) -> usize { self.0 } } @@ -43,10 +43,10 @@ impl FontCache { pub fn get<'a>( &'a mut self, font: &FontRef, - id_override: Option<[u64; 2]>, + id_override: Option<[usize; 2]>, mut f: impl FnMut(&FontRef) -> T, - ) -> ([u64; 2], &'a T) { - let id = id_override.unwrap_or([font.key.value(), u64::MAX]); + ) -> ([usize; 2], &'a T) { + let id = id_override.unwrap_or([font.key.value(), usize::MAX]); let (found, index) = self.find(id); if found { let entry = &mut self.entries[index]; @@ -73,7 +73,7 @@ impl FontCache { } } - fn find(&self, id: [u64; 2]) -> (bool, usize) { + fn find(&self, id: [usize; 2]) -> (bool, usize) { let mut lowest = 0; let mut lowest_epoch = self.epoch; for (i, entry) in self.entries.iter().enumerate() { @@ -95,6 +95,6 @@ impl FontCache { struct Entry { epoch: u64, - id: [u64; 2], + id: [usize; 2], data: T, } diff --git a/src/scale/hinting_cache.rs b/src/scale/hinting_cache.rs index 807ba72..c3a9813 100644 --- a/src/scale/hinting_cache.rs +++ b/src/scale/hinting_cache.rs @@ -12,7 +12,7 @@ use skrifa::{ const MAX_CACHED_HINT_INSTANCES: usize = 8; pub struct HintingKey<'a> { - pub id: [u64; 2], + pub id: [usize; 2], pub outlines: &'a OutlineGlyphCollection<'a>, pub size: Size, pub coords: &'a [NormalizedCoord], @@ -60,7 +60,7 @@ impl HintingCache { } struct HintingEntry { - id: [u64; 2], + id: [usize; 2], instance: HintingInstance, serial: u64, } diff --git a/src/scale/mod.rs b/src/scale/mod.rs index 8fdc44c..aee7eb4 100644 --- a/src/scale/mod.rs +++ b/src/scale/mod.rs @@ -343,7 +343,7 @@ pub struct ScalerBuilder<'a> { font: FontRef<'a>, outlines: Option>, proxy: &'a ScalerProxy, - id: [u64; 2], + id: [usize; 2], coords: &'a mut Vec, size: f32, hint: bool, diff --git a/src/shape/cache.rs b/src/shape/cache.rs index 19d8677..80520c7 100644 --- a/src/shape/cache.rs +++ b/src/shape/cache.rs @@ -29,7 +29,7 @@ impl FontEntry { pub struct FeatureEntry { pub epoch: Epoch, - pub id: [u64; 2], + pub id: [usize; 2], pub coords: Vec, pub tags: [u32; 4], pub store: FeatureStore, @@ -57,7 +57,7 @@ impl FeatureCache { pub fn entry<'a>( &'a mut self, - id: [u64; 2], + id: [usize; 2], coords: &[i16], has_feature_vars: bool, tags: &[u32; 4], @@ -79,7 +79,7 @@ impl FeatureCache { fn find_entry( &mut self, - id: [u64; 2], + id: [usize; 2], coords: &[i16], has_feature_vars: bool, tags: &[u32; 4], diff --git a/src/shape/mod.rs b/src/shape/mod.rs index ac168c3..4915349 100644 --- a/src/shape/mod.rs +++ b/src/shape/mod.rs @@ -319,7 +319,7 @@ impl ShapeContext { pub fn builder_with_id<'a>( &'a mut self, font: impl Into>, - id: [u64; 2], + id: [usize; 2], ) -> ShaperBuilder<'a> { ShaperBuilder::new_with_id(self, font, id) } @@ -368,7 +368,7 @@ pub struct ShaperBuilder<'a> { state: &'a mut State, feature_cache: &'a mut FeatureCache, font: FontRef<'a>, - font_id: [u64; 2], + font_id: [usize; 2], font_entry: &'a FontEntry, coords: &'a mut Vec, charmap: Charmap<'a>, @@ -385,7 +385,7 @@ impl<'a> ShaperBuilder<'a> { /// context and font. fn new(context: &'a mut ShapeContext, font: impl Into>) -> Self { let font = font.into(); - let id = [font.key.value(), u64::MAX]; + let id = [font.key.value(), usize::MAX]; Self::new_with_id(context, font, id) } @@ -394,7 +394,7 @@ impl<'a> ShaperBuilder<'a> { fn new_with_id( context: &'a mut ShapeContext, font: impl Into>, - id: [u64; 2], + id: [usize; 2], ) -> Self { let font = font.into(); let (font_id, font_entry) = context.font_cache.get(&font, Some(id), FontEntry::new); diff --git a/src/shape/partition.rs b/src/shape/partition.rs index 1bedf48..32f4f3e 100644 --- a/src/shape/partition.rs +++ b/src/shape/partition.rs @@ -24,7 +24,7 @@ pub trait SelectedFont: PartialEq { /// Returns a reference to the underlying font. fn font(&self) -> FontRef; - fn id_override(&self) -> Option<[u64; 2]> { + fn id_override(&self) -> Option<[usize; 2]> { None } @@ -215,7 +215,7 @@ where let font_ref = font.font(); let id = font .id_override() - .unwrap_or([font_ref.key.value(), u64::MAX]); + .unwrap_or([font_ref.key.value(), usize::MAX]); let mut shaper = context .builder_with_id(font.font(), id) .script(options.script())