Skip to content

Commit 4968d22

Browse files
Simplify event-id grammar by removing the argument tag.
The tag was meant to disambiguate between arguments and other kinds of annotations (like event category). However, there are only arguments at the moment, so the tag is redundant. It can be add back in later.
1 parent 1a17bb4 commit 4968d22

File tree

3 files changed

+20
-21
lines changed

3 files changed

+20
-21
lines changed

analyzeme/src/event.rs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ use memchr::memchr;
33
use std::borrow::Cow;
44
use std::time::Duration;
55

6-
const SEPARATOR: u8 = 0x1E;
7-
const ARG_TAG: u8 = 0x11;
8-
96
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
107
pub struct Event<'a> {
118
pub event_kind: Cow<'a, str>,
@@ -78,6 +75,8 @@ struct Parser<'a> {
7875
pos: usize,
7976
}
8077

78+
const SEPARATOR_BYTE: u8 = measureme::event_id::SEPARATOR_BYTE.as_bytes()[0];
79+
8180
impl<'a> Parser<'a> {
8281
fn new(full_text: Cow<'a, [u8]>) -> Parser<'a> {
8382
Parser { full_text, pos: 0 }
@@ -95,7 +94,7 @@ impl<'a> Parser<'a> {
9594
fn parse_separator_terminated_text(&mut self) -> Result<Cow<'a, str>, String> {
9695
let start = self.pos;
9796

98-
let end = memchr(SEPARATOR, &self.full_text[start..])
97+
let end = memchr(SEPARATOR_BYTE, &self.full_text[start..])
9998
.map(|pos| pos + start)
10099
.unwrap_or(self.full_text.len());
101100

@@ -105,27 +104,23 @@ impl<'a> Parser<'a> {
105104

106105
self.pos = end;
107106

107+
if self.full_text[start .. end].iter().any(u8::is_ascii_control) {
108+
return self.err("Found ASCII control character in <text>");
109+
}
110+
108111
Ok(self.substring(start, end))
109112
}
110113

111114
fn parse_arg(&mut self) -> Result<Cow<'a, str>, String> {
112-
if self.peek() != SEPARATOR {
115+
if self.peek() != SEPARATOR_BYTE {
113116
return self.err(&format!(
114117
"Expected '\\x{:x}' char at start of <argument>",
115-
SEPARATOR
118+
SEPARATOR_BYTE
116119
));
117120
}
118121

119122
self.pos += 1;
120-
let tag = self.peek();
121-
122-
match tag {
123-
ARG_TAG => {
124-
self.pos += 1;
125-
self.parse_separator_terminated_text()
126-
}
127-
other => self.err(&format!("Unexpected argument tag '{:x}'", other)),
128-
}
123+
self.parse_separator_terminated_text()
129124
}
130125

131126
fn err<T>(&self, message: &str) -> Result<T, String> {
@@ -163,7 +158,7 @@ mod tests {
163158

164159
#[test]
165160
fn parse_event_id_one_arg() {
166-
let (label, args) = Event::parse_event_id(Cow::from("foo\x1e\x11my_arg"));
161+
let (label, args) = Event::parse_event_id(Cow::from("foo\x1emy_arg"));
167162

168163
assert_eq!(label, "foo");
169164
assert_eq!(args, vec![Cow::from("my_arg")]);
@@ -172,7 +167,7 @@ mod tests {
172167
#[test]
173168
fn parse_event_id_n_args() {
174169
let (label, args) =
175-
Event::parse_event_id(Cow::from("foo\x1e\x11arg1\x1e\x11arg2\x1e\x11arg3"));
170+
Event::parse_event_id(Cow::from("foo\x1earg1\x1earg2\x1earg3"));
176171

177172
assert_eq!(label, "foo");
178173
assert_eq!(

measureme/src/event_id.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@ use crate::{Profiler, SerializationSink, StringComponent, StringId};
55
/// ```ignore
66
/// <event_id> = <label> {<argument>}
77
/// <label> = <text>
8-
/// <argument> = '\x1E' '\x11' <text>
9-
/// <text> = regex([^0x1E]+) // Anything but the separator byte
8+
/// <argument> = '\x1E' <text>
9+
/// <text> = regex([^[[:cntrl:]]]+) // Anything but ASCII control characters
1010
/// ```
1111
///
1212
/// This means there's always a "label", followed by an optional list of
1313
/// arguments. Future versions my support other optional suffixes (with a tag
1414
/// other than '\x11' after the '\x1E' separator), such as a "category".
1515
16+
17+
/// The byte used to separate arguments from the label and each other.
18+
pub const SEPARATOR_BYTE: &str = "\x1E";
19+
1620
pub struct EventIdBuilder<'p, S: SerializationSink> {
1721
profiler: &'p Profiler<S>,
1822
}
@@ -32,7 +36,7 @@ impl<'p, S: SerializationSink> EventIdBuilder<'p, S> {
3236
// Label
3337
StringComponent::Ref(label),
3438
// Seperator and start tag for arg
35-
StringComponent::Value("\x1E\x11"),
39+
StringComponent::Value(SEPARATOR_BYTE),
3640
// Arg string id
3741
StringComponent::Ref(arg),
3842
])

measureme/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
3838
#![deny(warnings)]
3939

40-
mod event_id;
40+
pub mod event_id;
4141
pub mod file_header;
4242
#[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))]
4343
mod file_serialization_sink;

0 commit comments

Comments
 (0)