Skip to content

Commit 0fa07a8

Browse files
chore(parser): reduce intermediate string allocations (#820)
* chore(parser): reduce intermediate allocations By threading the appropriate input lifetime through lexer and parser methods, we can avoid copying strings in a few places. Especially nice is that we can avoid copying the same strings multiple times when peeking at tokens, and we can avoid an eager copy that is almost always just thrown away in the implementation of `.expect()`. * It's significantly faster to use references here
1 parent 12db7e8 commit 0fa07a8

File tree

15 files changed

+42
-45
lines changed

15 files changed

+42
-45
lines changed

crates/apollo-parser/src/lexer/token.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ pub struct Token<'a> {
1111
}
1212

1313
impl<'a> Token<'a> {
14-
/// Get a reference to the token's kind.
14+
/// Returns the kind of token.
1515
pub fn kind(&self) -> TokenKind {
1616
self.kind
1717
}
1818

19-
/// Get a reference to the token's data.
19+
/// Returns the source text for this token.
2020
pub fn data(&self) -> &'a str {
2121
self.data
2222
}
2323

24-
/// Get a reference to the token's loc.
24+
/// Returns the byte offset of this token in the source text.
2525
pub fn index(&self) -> usize {
2626
self.index
2727
}

crates/apollo-parser/src/parser/grammar/directive.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub(crate) fn directive_definition(p: &mut Parser) {
1313
description::description(p);
1414
}
1515

16-
if let Some("directive") = p.peek_data().as_deref() {
16+
if let Some("directive") = p.peek_data() {
1717
p.bump(SyntaxKind::directive_KW);
1818
}
1919

@@ -38,13 +38,13 @@ pub(crate) fn directive_definition(p: &mut Parser) {
3838
}
3939

4040
if let Some(node) = p.peek_data() {
41-
if node.as_str() == "repeatable" {
41+
if node == "repeatable" {
4242
p.bump(SyntaxKind::repeatable_KW);
4343
}
4444
}
4545

4646
if let Some(node) = p.peek_data() {
47-
match node.as_str() {
47+
match node {
4848
"on" => p.bump(SyntaxKind::on_KW),
4949
_ => p.err("expected Directive Locations"),
5050
}
@@ -74,7 +74,7 @@ pub(crate) fn directive_locations(p: &mut Parser, is_location: bool) {
7474

7575
if let Some(TokenKind::Name) = p.peek() {
7676
let loc = p.peek_data().unwrap();
77-
match loc.as_str() {
77+
match loc {
7878
"QUERY" => {
7979
let _g = p.start_node(SyntaxKind::DIRECTIVE_LOCATION);
8080
p.bump(SyntaxKind::QUERY_KW);

crates/apollo-parser/src/parser/grammar/document.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ pub(crate) fn document(p: &mut Parser) {
4242
doc.finish_node();
4343
}
4444

45-
fn select_definition(def: String, p: &mut Parser) {
46-
match def.as_str() {
45+
fn select_definition(def: &str, p: &mut Parser) {
46+
match def {
4747
"directive" => directive::directive_definition(p),
4848
"enum" => enum_::enum_type_definition(p),
4949
"extend" => extensions::extensions(p),

crates/apollo-parser/src/parser/grammar/enum_.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub(crate) fn enum_type_definition(p: &mut Parser) {
1515
description::description(p);
1616
}
1717

18-
if let Some("enum") = p.peek_data().as_deref() {
18+
if let Some("enum") = p.peek_data() {
1919
p.bump(SyntaxKind::enum_KW);
2020
}
2121

crates/apollo-parser/src/parser/grammar/extensions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{
66
pub(crate) fn extensions(p: &mut Parser) {
77
// we already know the next node is 'extend', check for the node after that
88
// to figure out which type system extension to apply.
9-
match p.peek_data_n(2).as_deref() {
9+
match p.peek_data_n(2) {
1010
Some("schema") => schema::schema_extension(p),
1111
Some("scalar") => scalar::scalar_type_extension(p),
1212
Some("type") => object::object_type_extension(p),

crates/apollo-parser/src/parser/grammar/input.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub(crate) fn input_object_type_definition(p: &mut Parser) {
1414
description::description(p);
1515
}
1616

17-
if let Some("input") = p.peek_data().as_deref() {
17+
if let Some("input") = p.peek_data() {
1818
p.bump(SyntaxKind::input_KW);
1919
}
2020

crates/apollo-parser/src/parser/grammar/interface.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub(crate) fn interface_type_definition(p: &mut Parser) {
1313
description::description(p);
1414
}
1515

16-
if let Some("interface") = p.peek_data().as_deref() {
16+
if let Some("interface") = p.peek_data() {
1717
p.bump(SyntaxKind::interface_KW);
1818
}
1919

@@ -22,7 +22,7 @@ pub(crate) fn interface_type_definition(p: &mut Parser) {
2222
_ => p.err("expected a Name"),
2323
}
2424

25-
if let Some("implements") = p.peek_data().as_deref() {
25+
if let Some("implements") = p.peek_data() {
2626
object::implements_interfaces(p);
2727
}
2828

@@ -53,7 +53,7 @@ pub(crate) fn interface_type_extension(p: &mut Parser) {
5353
_ => p.err("expected a Name"),
5454
}
5555

56-
if let Some("implements") = p.peek_data().as_deref() {
56+
if let Some("implements") = p.peek_data() {
5757
meets_requirements = true;
5858
object::implements_interfaces(p);
5959
}

crates/apollo-parser/src/parser/grammar/name.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub(crate) fn name(p: &mut Parser) {
88
match p.peek() {
99
Some(TokenKind::Name) => {
1010
let _g = p.start_node(SyntaxKind::NAME);
11-
validate_name(&p.peek_data().unwrap(), p);
11+
validate_name(p.peek_data().unwrap(), p);
1212
p.bump(SyntaxKind::IDENT);
1313
}
1414
_ => p.err("expected a Name"),

crates/apollo-parser/src/parser/grammar/object.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub(crate) fn object_type_definition(p: &mut Parser) {
1515
description::description(p);
1616
}
1717

18-
if let Some("type") = p.peek_data().as_deref() {
18+
if let Some("type") = p.peek_data() {
1919
p.bump(SyntaxKind::type_KW);
2020
}
2121

@@ -61,7 +61,7 @@ pub(crate) fn object_type_extension(p: &mut Parser) {
6161
_ => p.err("expected a Name"),
6262
}
6363

64-
if let Some("implements") = p.peek_data().as_deref() {
64+
if let Some("implements") = p.peek_data() {
6565
meets_requirements = true;
6666
implements_interfaces(p);
6767
}

crates/apollo-parser/src/parser/grammar/operation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ pub(crate) fn operation_definition(p: &mut Parser) {
8888
pub(crate) fn operation_type(p: &mut Parser) {
8989
if let Some(node) = p.peek_data() {
9090
let _g = p.start_node(SyntaxKind::OPERATION_TYPE);
91-
match node.as_str() {
91+
match node {
9292
"query" => p.bump(SyntaxKind::query_KW),
9393
"subscription" => p.bump(SyntaxKind::subscription_KW),
9494
"mutation" => p.bump(SyntaxKind::mutation_KW),

0 commit comments

Comments
 (0)