Skip to content

Commit

Permalink
Use usize for cache keys instead of u64.
Browse files Browse the repository at this point in the history
This improves portability, because `AtomicUsize` is available on
32-bit targets, but generally `AtomicU64` is not.
  • Loading branch information
xorgy committed Nov 6, 2024
1 parent 648e0cf commit 40b3b4c
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 21 deletions.
18 changes: 9 additions & 9 deletions src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ use super::FontRef;

/// 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::{AtomicU64, Ordering};
static KEY: AtomicU64 = AtomicU64::new(1);
use core::sync::atomic::{AtomicUsize, Ordering};
static KEY: AtomicUsize = AtomicUsize::new(1);
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
}
}
Expand Down Expand Up @@ -42,10 +42,10 @@ impl<T> FontCache<T> {
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];
Expand All @@ -72,7 +72,7 @@ impl<T> FontCache<T> {
}
}

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() {
Expand All @@ -94,6 +94,6 @@ impl<T> FontCache<T> {

struct Entry<T> {
epoch: u64,
id: [u64; 2],
id: [usize; 2],
data: T,
}
4 changes: 2 additions & 2 deletions src/scale/hinting_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,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],
Expand Down Expand Up @@ -58,7 +58,7 @@ impl HintingCache {
}

struct HintingEntry {
id: [u64; 2],
id: [usize; 2],
instance: HintingInstance,
serial: u64,
}
Expand Down
2 changes: 1 addition & 1 deletion src/scale/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ pub struct ScalerBuilder<'a> {
font: FontRef<'a>,
outlines: Option<OutlineGlyphCollection<'a>>,
proxy: &'a ScalerProxy,
id: [u64; 2],
id: [usize; 2],
coords: &'a mut Vec<SkrifaNormalizedCoord>,
size: f32,
hint: bool,
Expand Down
6 changes: 3 additions & 3 deletions src/shape/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl FontEntry {

pub struct FeatureEntry {
pub epoch: Epoch,
pub id: [u64; 2],
pub id: [usize; 2],
pub coords: Vec<i16>,
pub tags: [u32; 4],
pub store: FeatureStore,
Expand Down Expand Up @@ -55,7 +55,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],
Expand All @@ -77,7 +77,7 @@ impl FeatureCache {

fn find_entry(
&mut self,
id: [u64; 2],
id: [usize; 2],
coords: &[i16],
has_feature_vars: bool,
tags: &[u32; 4],
Expand Down
8 changes: 4 additions & 4 deletions src/shape/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ impl ShapeContext {
pub fn builder_with_id<'a>(
&'a mut self,
font: impl Into<FontRef<'a>>,
id: [u64; 2],
id: [usize; 2],
) -> ShaperBuilder<'a> {
ShaperBuilder::new_with_id(self, font, id)
}
Expand Down Expand Up @@ -367,7 +367,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<i16>,
charmap: Charmap<'a>,
Expand All @@ -384,7 +384,7 @@ impl<'a> ShaperBuilder<'a> {
/// context and font.
fn new(context: &'a mut ShapeContext, font: impl Into<FontRef<'a>>) -> 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)
}

Expand All @@ -393,7 +393,7 @@ impl<'a> ShaperBuilder<'a> {
fn new_with_id(
context: &'a mut ShapeContext,
font: impl Into<FontRef<'a>>,
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);
Expand Down
4 changes: 2 additions & 2 deletions src/shape/partition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit 40b3b4c

Please sign in to comment.