Skip to content

Commit

Permalink
Try buf.clear
Browse files Browse the repository at this point in the history
  • Loading branch information
rsheeter committed Aug 24, 2023
1 parent b142add commit 0dd99f9
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ pretty_assertions = "1.0"
expect-test = "1.4.1"
criterion = "0.5"

# https://github.com/bheisler/criterion.rs/issues/193
# Make it possible to save baseline, e.g. cargo bench -- --save-baseline master
[lib]
bench = false

[[bench]]
name = "glif_parse"
harness = false
28 changes: 27 additions & 1 deletion benches/glif_parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,36 @@
//!
//! This should be run when making any changes to glyph parsing.

use std::path::{Path, PathBuf};

use criterion::{black_box, criterion_group, criterion_main, Criterion};
use norad::Glyph;

static MUTATOR_SANS_GLYPHS_DIR: &str = "testdata/MutatorSansLightWide.ufo/glyphs";
static S_GLYPH: &str = "testdata/MutatorSansLightWide.ufo/glyphs/S_.glif";
static DOT: &str = "testdata/MutatorSansLightWide.ufo/glyphs/dot.glif";
static A_ACUTE_GLYPH: &str = "testdata/MutatorSansLightWide.ufo/glyphs/A_acute.glif";
// largest glyph in noto cjk
static CID61855: &str = "testdata/cid61855.glif";

fn load_bytes(path: &str) -> Vec<u8> {
fn load_bytes<P>(path: P) -> Vec<u8>
where
P: AsRef<Path>,
{
std::fs::read(path).unwrap()
}

fn load_all(dir: &str) -> Vec<(PathBuf, Vec<u8>)> {
std::fs::read_dir(dir)
.unwrap()
.into_iter()
.map(|e| e.unwrap())
.filter_map(
|e| if e.path().is_file() { Some((e.path(), load_bytes(e.path()))) } else { None },
)
.collect()
}

pub fn criterion_benchmark(c: &mut Criterion) {
// a normal glyph
c.bench_function("parse S", |b| {
Expand Down Expand Up @@ -44,6 +61,15 @@ pub fn criterion_benchmark(c: &mut Criterion) {
Glyph::parse_raw(black_box(&bytes)).unwrap();
})
});
// many glyphs
c.bench_function("parse MutatorSansLightWide glyphs", |b| {
let glyphs = load_all(MUTATOR_SANS_GLYPHS_DIR);
b.iter(|| {
for (_, glyph_bytes) in glyphs.iter().filter(|(p, _)| p.ends_with(".glif")) {
Glyph::parse_raw(black_box(&glyph_bytes)).unwrap();
}
})
});
// Note to somebody using this:
//
// It might be nice if we also had some other examples, like a glyph with
Expand Down
6 changes: 6 additions & 0 deletions src/glyph/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ impl<'names> GlifParser<'names> {
Event::End(ref end) if end.name().as_ref() == b"glyph" => break,
_other => return Err(ErrorKind::MissingCloseTag.into()),
}
buf.clear();
}

self.glyph.load_object_libs()?;
Expand Down Expand Up @@ -158,6 +159,7 @@ impl<'names> GlifParser<'names> {
Event::Eof => return Err(ErrorKind::UnexpectedEof.into()),
_other => return Err(ErrorKind::UnexpectedElement.into()),
}
buf.clear();
}

let (mut contours, components) = outline_builder.finish()?;
Expand Down Expand Up @@ -235,6 +237,7 @@ impl<'names> GlifParser<'names> {
Event::Eof => return Err(ErrorKind::UnexpectedEof.into()),
_other => return Err(ErrorKind::UnexpectedElement.into()),
}
buf.clear();
}
outline_builder.end_path()?;

Expand Down Expand Up @@ -302,6 +305,7 @@ impl<'names> GlifParser<'names> {
Event::Eof => return Err(ErrorKind::UnexpectedEof.into()),
_other => end = reader.buffer_position(),
}
buf.clear();
}

let plist_slice = &raw_xml[start..end];
Expand All @@ -328,6 +332,7 @@ impl<'names> GlifParser<'names> {
Event::Eof => return Err(ErrorKind::UnexpectedEof.into()),
_other => (),
}
buf.clear();
}
Ok(())
}
Expand Down Expand Up @@ -576,5 +581,6 @@ fn start(
}
_other => return Err(ErrorKind::WrongFirstElement.into()),
}
buf.clear();
}
}

0 comments on commit 0dd99f9

Please sign in to comment.