Skip to content

Commit 579d8a7

Browse files
authored
Use &str in Token instead of String (#90)
This improves parsing performance by nearly 2x. Old time: 10.745 µs New time: 6.0038 µs
1 parent 1158866 commit 579d8a7

3 files changed

Lines changed: 16 additions & 16 deletions

File tree

src/constructor_parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ enum ConstructorStringParserState {
2525
// Ref: https://wicg.github.io/urlpattern/#constructor-string-parser
2626
struct ConstructorStringParser<'a> {
2727
input: &'a str,
28-
token_list: Vec<Token>,
28+
token_list: Vec<Token<'a>>,
2929
result: UrlPatternInit,
3030
component_start: usize,
3131
token_index: usize,

src/parser.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,11 @@ impl Part {
176176
}
177177

178178
// Ref: https://wicg.github.io/urlpattern/#pattern-parser
179-
struct PatternParser<F>
179+
struct PatternParser<'a, F>
180180
where
181181
F: Fn(&str) -> Result<String, Error>,
182182
{
183-
token_list: Vec<Token>,
183+
token_list: Vec<Token<'a>>,
184184
encoding_callback: F,
185185
segment_wildcard_regexp: String,
186186
part_list: Vec<Part>,
@@ -189,12 +189,12 @@ where
189189
next_numeric_name: usize,
190190
}
191191

192-
impl<F> PatternParser<F>
192+
impl<'a, F> PatternParser<'a, F>
193193
where
194194
F: Fn(&str) -> Result<String, Error>,
195195
{
196196
// Ref: https://wicg.github.io/urlpattern/#try-to-consume-a-token
197-
fn try_consume_token(&mut self, kind: TokenType) -> Option<Token> {
197+
fn try_consume_token(&mut self, kind: TokenType) -> Option<Token<'a>> {
198198
assert!(self.index < self.token_list.len());
199199
let next_token = self.token_list[self.index].clone();
200200
if next_token.kind != kind {
@@ -210,7 +210,7 @@ where
210210
fn try_consume_regexp_or_wildcard_token(
211211
&mut self,
212212
name_token_is_none: bool,
213-
) -> Option<Token> {
213+
) -> Option<Token<'a>> {
214214
let token = self.try_consume_token(TokenType::Regexp);
215215
if name_token_is_none && token.is_none() {
216216
self.try_consume_token(TokenType::Asterisk)
@@ -221,7 +221,7 @@ where
221221

222222
// Ref: https://wicg.github.io/urlpattern/#try-to-consume-a-modifier-token
223223
#[inline]
224-
fn try_consume_modifier_token(&mut self) -> Option<Token> {
224+
fn try_consume_modifier_token(&mut self) -> Option<Token<'a>> {
225225
self
226226
.try_consume_token(TokenType::OtherModifier)
227227
.or_else(|| self.try_consume_token(TokenType::Asterisk))
@@ -255,7 +255,7 @@ where
255255
) -> Result<(), Error> {
256256
let mut modifier = PartModifier::None;
257257
if let Some(modifier_token) = modifier_token {
258-
modifier = match modifier_token.value.as_ref() {
258+
modifier = match modifier_token.value {
259259
"?" => PartModifier::Optional,
260260
"*" => PartModifier::ZeroOrMore,
261261
"+" => PartModifier::OneOrMore,
@@ -306,7 +306,7 @@ where
306306

307307
let mut name = String::new();
308308
if let Some(name_token) = name_token {
309-
name = name_token.value;
309+
name = name_token.value.to_owned();
310310
} else if regexp_or_wildcard_token.is_some() {
311311
name = self.next_numeric_name.to_string();
312312
self.next_numeric_name += 1;
@@ -344,7 +344,7 @@ where
344344
if token.is_none() {
345345
break;
346346
}
347-
result.push_str(&token.unwrap().value);
347+
result.push_str(token.unwrap().value);
348348
}
349349
result
350350
}
@@ -359,7 +359,7 @@ where
359359
Error::Parser(ParserError::ExpectedToken(
360360
kind,
361361
self.token_list[self.index].kind.clone(),
362-
self.token_list[self.index].value.clone(),
362+
self.token_list[self.index].value.to_owned(),
363363
))
364364
})
365365
}
@@ -419,7 +419,7 @@ where
419419
fixed_token = parser.try_consume_token(TokenType::EscapedChar);
420420
}
421421
if let Some(fixed_token) = fixed_token {
422-
parser.pending_fixed_value.push_str(&fixed_token.value);
422+
parser.pending_fixed_value.push_str(fixed_token.value);
423423
continue;
424424
}
425425
let open_token = parser.try_consume_token(TokenType::Open);

src/tokenizer.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ pub enum TokenType {
2727

2828
// Ref: https://wicg.github.io/urlpattern/#token
2929
#[derive(Debug, Clone)]
30-
pub struct Token {
30+
pub struct Token<'a> {
3131
pub kind: TokenType,
3232
pub index: usize,
33-
pub value: String,
33+
pub value: &'a str,
3434
}
3535

3636
// Ref: https://wicg.github.io/urlpattern/#tokenize-policy
@@ -44,7 +44,7 @@ pub enum TokenizePolicy {
4444
struct Tokenizer<'a> {
4545
input: &'a str,
4646
policy: TokenizePolicy,
47-
token_list: Vec<Token>,
47+
token_list: Vec<Token<'a>>,
4848
index: usize,
4949
next_index: usize,
5050
code_point: Option<char>, // TODO: get rid of Option
@@ -86,7 +86,7 @@ impl<'a> Tokenizer<'a> {
8686
value_len: usize,
8787
) {
8888
let range = value_pos..(value_pos + value_len);
89-
let value = self.input[range].to_owned();
89+
let value = &self.input[range];
9090
self.token_list.push(Token {
9191
kind,
9292
index: self.index,

0 commit comments

Comments
 (0)