Skip to content

Commit

Permalink
dev: add intern support (#429)
Browse files Browse the repository at this point in the history
  • Loading branch information
Myriad-Dreamin authored Dec 5, 2023
1 parent 9644b70 commit c25f4e4
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 15 deletions.
78 changes: 64 additions & 14 deletions core/src/vector/flat_ir/module.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{
borrow::Borrow,
borrow::{Borrow, Cow},
collections::{BTreeMap, HashMap},
ops::Deref,
sync::Arc,
Expand Down Expand Up @@ -64,6 +64,11 @@ impl Module {
FrozenModule(Arc::new(Prehashed::new(self)))
}

/// Get a font item by its stable ref.
pub fn get_font(&self, id: &FontRef) -> Option<&FontItem> {
self.fonts.get(id.idx as usize)
}

/// Get a glyph item by its stable ref.
pub fn get_glyph(&self, id: &AbsoluteRef) -> Option<&GlyphItem> {
self.glyphs.get(id.id.0 as usize).map(|(_, item)| item)
Expand Down Expand Up @@ -209,6 +214,46 @@ impl<const ENABLE_REF_CNT: bool> BuildGlyph for ModuleBuilderImpl<ENABLE_REF_CNT
}
}

impl ModuleBuilder {
pub fn intern(&mut self, module: &Module, f: &Fingerprint) {
let item = module.get_item(f).unwrap();
match item {
FlatSvgItem::None
| FlatSvgItem::Link(_)
| FlatSvgItem::Image(_)
| FlatSvgItem::Path(_)
| FlatSvgItem::Gradient(_)
| FlatSvgItem::ContentHint(_) => {
self.insert(*f, Cow::Borrowed(item));
}
FlatSvgItem::Text(t) => {
self.glyphs.used_fonts.insert(t.font.clone());
self.glyphs
.used_glyphs
.extend(t.content.glyphs.iter().map(|(_, _, glyph)| glyph).cloned());

self.insert(*f, Cow::Borrowed(item));
}
FlatSvgItem::Item(t) => {
self.insert(*f, Cow::Borrowed(item));

if !self.items.contains_key(&t.1) {
self.intern(module, &t.1);
}
}
FlatSvgItem::Group(g, _) => {
self.insert(*f, Cow::Borrowed(item));

for (_, id) in g.0.iter() {
if !self.items.contains_key(id) {
self.intern(module, id);
}
}
}
}
}
}

impl<const ENABLE_REF_CNT: bool> ModuleBuilderImpl<ENABLE_REF_CNT> {
pub fn reset(&mut self) {
self.source_mapping.clear();
Expand All @@ -235,6 +280,23 @@ impl<const ENABLE_REF_CNT: bool> ModuleBuilderImpl<ENABLE_REF_CNT> {
}
}

fn insert(&mut self, fg: Fingerprint, item: Cow<FlatSvgItem>) -> bool {
if let Some(pos) = self.items.get_mut(&fg) {
if ENABLE_REF_CNT && pos.0 != self.lifetime {
pos.0 = self.lifetime - 1;
}
return true;
}

if ENABLE_REF_CNT {
self.items.insert(fg, (self.lifetime, item.into_owned()));
} else {
self.items.insert(fg, (0, item.into_owned()));
}

false
}

pub fn build(&mut self, item: SvgItem) -> Fingerprint {
let resolved_item = match item {
SvgItem::Image((image, span_id)) => {
Expand Down Expand Up @@ -321,19 +383,7 @@ impl<const ENABLE_REF_CNT: bool> ModuleBuilderImpl<ENABLE_REF_CNT> {

let fingerprint = self.fingerprint_builder.resolve(&resolved_item);

if let Some(pos) = self.items.get_mut(&fingerprint) {
if ENABLE_REF_CNT && pos.0 != self.lifetime {
pos.0 = self.lifetime - 1;
}
return fingerprint;
}

if ENABLE_REF_CNT {
self.items
.insert(fingerprint, (self.lifetime, resolved_item));
} else {
self.items.insert(fingerprint, (0, resolved_item));
}
self.insert(fingerprint, Cow::Owned(resolved_item));
fingerprint
}
}
Expand Down
8 changes: 7 additions & 1 deletion core/src/vector/ir.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use core::fmt;
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::hash::Hash;
use std::sync::Arc;

Expand Down Expand Up @@ -665,6 +665,10 @@ pub struct GlyphPackBuilderImpl<const ENABLE_REF_CNT: bool = false> {
pub lifetime: u64,
pub incr_fonts: Vec<u64>,
pub incr_glyphs: Vec<u64>,

/// for interning
pub used_fonts: HashSet<FontRef>,
pub used_glyphs: HashSet<GlyphRef>,
}

pub type GlyphPackBuilder = GlyphPackBuilderImpl</* ENABLE_REF_CNT */ false>;
Expand All @@ -678,6 +682,8 @@ impl<const ENABLE_REF_CNT: bool> Default for GlyphPackBuilderImpl<ENABLE_REF_CNT
glyph_defs: Default::default(),
incr_fonts: Default::default(),
incr_glyphs: Default::default(),
used_fonts: Default::default(),
used_glyphs: Default::default(),
}
}
}
Expand Down

0 comments on commit c25f4e4

Please sign in to comment.