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 7, 2024
1 parent b90a9d6 commit b2d01bc
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 20 deletions.
16 changes: 8 additions & 8 deletions src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -43,10 +43,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 @@ -73,7 +73,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 @@ -95,6 +95,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 @@ -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],
Expand Down Expand Up @@ -60,7 +60,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 @@ -343,7 +343,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 @@ -29,7 +29,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 @@ -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],
Expand All @@ -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],
Expand Down
8 changes: 4 additions & 4 deletions src/shape/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,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 @@ -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<i16>,
charmap: Charmap<'a>,
Expand All @@ -385,7 +385,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 @@ -394,7 +394,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 b2d01bc

Please sign in to comment.