This commit is contained in:
Robin Appelman 2024-11-05 15:50:53 +01:00
commit f96b81aa3a
5 changed files with 28 additions and 17 deletions

View file

@ -1,12 +1,15 @@
use php_literal_parser::{from_str, Value};
use criterion::{black_box, criterion_group, criterion_main, Criterion}; use criterion::{black_box, criterion_group, criterion_main, Criterion};
use php_literal_parser::{from_str, Value};
fn perf_parse_int_basic(b: &mut Criterion) { fn perf_parse_int_basic(b: &mut Criterion) {
let input = "12345676"; let input = "12345676";
b.bench_function("parse int", |b| { b.bench_function("parse int", |b| {
b.iter(|| { b.iter(|| {
assert_eq!(black_box(from_str::<Value>(black_box(input)).unwrap()), 12345676); assert_eq!(
black_box(from_str::<Value>(black_box(input)).unwrap()),
12345676
);
}); });
}); });
} }
@ -46,10 +49,19 @@ fn perf_str_single_escape(b: &mut Criterion) {
b.bench_function("parse single quote escaped string", |b| { b.bench_function("parse single quote escaped string", |b| {
b.iter(|| { b.iter(|| {
assert!(black_box(from_str::<Value>(black_box(input)).unwrap().is_string())); assert!(black_box(
from_str::<Value>(black_box(input)).unwrap().is_string()
));
}); });
}); });
} }
criterion_group!(benches, perf_str_single_escape, perf_str_single_basic, perf_str_double_escape, perf_str_double_basic, perf_parse_int_basic); criterion_group!(
benches,
perf_str_single_escape,
perf_str_single_basic,
perf_str_double_escape,
perf_str_double_basic,
perf_parse_int_basic
);
criterion_main!(benches); criterion_main!(benches);

View file

@ -297,10 +297,7 @@ impl<'source> ExpectToken<'source> for SpannedToken<'source> {
} }
fn map_span(span: &Span) -> SourceSpan { fn map_span(span: &Span) -> SourceSpan {
SourceSpan::new( SourceSpan::new(SourceOffset::from(span.start), span.end - span.start)
SourceOffset::from(span.start),
span.end - span.start,
)
} }
pub trait ResultExt<T> { pub trait ResultExt<T> {

View file

@ -34,7 +34,7 @@ impl UnescapeState {
Some(c) => { Some(c) => {
self.push_char(c); self.push_char(c);
Ok(()) Ok(())
}, }
None => Err(UnescapeError), None => Err(UnescapeError),
} }
} }
@ -205,8 +205,10 @@ impl<'a> PeekableBytes<'a> {
pub fn is_array_key_numeric(string: &str) -> bool { pub fn is_array_key_numeric(string: &str) -> bool {
let mut bytes = string.bytes(); let mut bytes = string.bytes();
if !matches!((bytes.next(), string.len()), (Some(b'-'), _) | (Some(b'0'..=b'9'), 1) | (Some(b'1'..=b'9'), _)) if !matches!(
{ (bytes.next(), string.len()),
(Some(b'-'), _) | (Some(b'0'..=b'9'), 1) | (Some(b'1'..=b'9'), _)
) {
return false; return false;
} }