Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear buf #320

Merged
merged 2 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
27 changes: 26 additions & 1 deletion benches/glif_parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,35 @@
//!
//! 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()
.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 +60,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();
}
}