Move pooling of line wrappers into FontCache

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Antonio Scandurra
2021-08-25 17:28:25 +02:00
co-authored by Nathan Sobo
parent 0c9e72cce7
commit 1aa1e6c6ab
5 changed files with 65 additions and 60 deletions
+2 -48
View File
@@ -7,7 +7,6 @@ use crate::{
},
platform, scene, FontSystem, PaintContext,
};
use lazy_static::lazy_static;
use ordered_float::OrderedFloat;
use parking_lot::{Mutex, RwLock, RwLockUpgradableReadGuard};
use smallvec::SmallVec;
@@ -16,7 +15,6 @@ use std::{
collections::HashMap,
hash::{Hash, Hasher},
iter,
ops::{Deref, DerefMut},
sync::Arc,
};
@@ -307,10 +305,6 @@ impl Run {
}
}
lazy_static! {
static ref WRAPPER_POOL: Mutex<Vec<LineWrapper>> = Default::default();
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Boundary {
pub ix: usize,
@@ -331,8 +325,8 @@ impl Boundary {
pub struct LineWrapper {
font_system: Arc<dyn FontSystem>,
font_id: FontId,
font_size: f32,
pub(crate) font_id: FontId,
pub(crate) font_size: f32,
cached_ascii_char_widths: [f32; 128],
cached_other_char_widths: HashMap<char, f32>,
}
@@ -340,23 +334,6 @@ pub struct LineWrapper {
impl LineWrapper {
pub const MAX_INDENT: u32 = 256;
pub fn acquire(
font_id: FontId,
font_size: f32,
font_system: Arc<dyn FontSystem>,
) -> LineWrapperHandle {
let wrapper = if let Some(mut wrapper) = WRAPPER_POOL.lock().pop() {
if wrapper.font_id != font_id || wrapper.font_size != font_size {
wrapper.cached_ascii_char_widths = [f32::NAN; 128];
wrapper.cached_other_char_widths.clear();
}
wrapper
} else {
LineWrapper::new(font_id, font_size, font_system)
};
LineWrapperHandle(Some(wrapper))
}
pub fn new(font_id: FontId, font_size: f32, font_system: Arc<dyn FontSystem>) -> Self {
Self {
font_system,
@@ -534,29 +511,6 @@ impl LineWrapper {
}
}
pub struct LineWrapperHandle(Option<LineWrapper>);
impl Drop for LineWrapperHandle {
fn drop(&mut self) {
let wrapper = self.0.take().unwrap();
WRAPPER_POOL.lock().push(wrapper)
}
}
impl Deref for LineWrapperHandle {
type Target = LineWrapper;
fn deref(&self) -> &Self::Target {
self.0.as_ref().unwrap()
}
}
impl DerefMut for LineWrapperHandle {
fn deref_mut(&mut self) -> &mut Self::Target {
self.0.as_mut().unwrap()
}
}
#[cfg(test)]
mod tests {
use super::*;