Skip to content

Commit

Permalink
Improve performance by inlining functions
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Jun 27, 2024
1 parent f399487 commit f5663c2
Show file tree
Hide file tree
Showing 25 changed files with 82 additions and 1 deletion.
1 change: 1 addition & 0 deletions bluejay-parser/src/ast/argument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub type ConstArgument<'a> = Argument<'a, true>;
pub type VariableArgument<'a> = Argument<'a, false>;

impl<'a, const CONST: bool> FromTokens<'a> for Argument<'a, CONST> {
#[inline]
fn from_tokens(tokens: &mut impl Tokens<'a>) -> Result<Self, ParseError> {
let name = tokens.expect_name()?;
tokens.expect_punctuator(PunctuatorType::Colon)?;
Expand Down
2 changes: 2 additions & 0 deletions bluejay-parser/src/ast/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub struct Arguments<'a, const CONST: bool> {
pub type VariableArguments<'a> = Arguments<'a, false>;

impl<'a, const CONST: bool> FromTokens<'a> for Arguments<'a, CONST> {
#[inline]
fn from_tokens(tokens: &mut impl Tokens<'a>) -> Result<Self, ParseError> {
let open_span = tokens.expect_punctuator(PunctuatorType::OpenRoundBracket)?;
let mut arguments: Vec<Argument<CONST>> = Vec::new();
Expand All @@ -27,6 +28,7 @@ impl<'a, const CONST: bool> FromTokens<'a> for Arguments<'a, CONST> {
}

impl<'a, const CONST: bool> IsMatch<'a> for Arguments<'a, CONST> {
#[inline]
fn is_match(tokens: &mut impl Tokens<'a>) -> bool {
tokens.peek_punctuator_matches(0, PunctuatorType::OpenRoundBracket)
}
Expand Down
2 changes: 2 additions & 0 deletions bluejay-parser/src/ast/directive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ pub type ConstDirective<'a> = Directive<'a, true>;
pub type VariableDirective<'a> = Directive<'a, false>;

impl<'a, const CONST: bool> IsMatch<'a> for Directive<'a, CONST> {
#[inline]
fn is_match(tokens: &mut impl Tokens<'a>) -> bool {
tokens.peek_punctuator_matches(0, PunctuatorType::At)
}
}

impl<'a, const CONST: bool> FromTokens<'a> for Directive<'a, CONST> {
#[inline]
fn from_tokens(tokens: &mut impl Tokens<'a>) -> Result<Self, ParseError> {
let at_span = tokens.expect_punctuator(PunctuatorType::At)?;
let name = tokens.expect_name()?;
Expand Down
2 changes: 2 additions & 0 deletions bluejay-parser/src/ast/directives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub type ConstDirectives<'a> = Directives<'a, true>;
pub type VariableDirectives<'a> = Directives<'a, false>;

impl<'a, const CONST: bool> FromTokens<'a> for Directives<'a, CONST> {
#[inline]
fn from_tokens(tokens: &mut impl Tokens<'a>) -> Result<Self, ParseError> {
let mut directives: Vec<Directive<'a, CONST>> = Vec::new();
while let Some(directive) = Directive::try_from_tokens(tokens) {
Expand All @@ -29,6 +30,7 @@ impl<'a, const CONST: bool> FromTokens<'a> for Directives<'a, CONST> {
}

impl<'a, const CONST: bool> IsMatch<'a> for Directives<'a, CONST> {
#[inline]
fn is_match(tokens: &mut impl Tokens<'a>) -> bool {
Directive::<'a, CONST>::is_match(tokens)
}
Expand Down
2 changes: 2 additions & 0 deletions bluejay-parser/src/ast/executable/executable_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub enum ExecutableDefinition<'a> {
}

impl<'a> FromTokens<'a> for ExecutableDefinition<'a> {
#[inline]
fn from_tokens(tokens: &mut impl Tokens<'a>) -> Result<Self, ParseError> {
if OperationDefinition::is_match(tokens) {
OperationDefinition::from_tokens(tokens).map(Self::Operation)
Expand All @@ -20,6 +21,7 @@ impl<'a> FromTokens<'a> for ExecutableDefinition<'a> {
}

impl<'a> IsMatch<'a> for ExecutableDefinition<'a> {
#[inline]
fn is_match(tokens: &mut impl Tokens<'a>) -> bool {
OperationDefinition::is_match(tokens) || FragmentDefinition::is_match(tokens)
}
Expand Down
2 changes: 2 additions & 0 deletions bluejay-parser/src/ast/executable/executable_document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ impl<'a> ExecutableDocument<'a> {
&self.fragment_definitions
}

#[inline]
fn is_empty(&self) -> bool {
self.operation_definitions.is_empty() && self.fragment_definitions.is_empty()
}
}

impl<'a> Parse<'a> for ExecutableDocument<'a> {
#[inline]
fn parse_from_tokens(mut tokens: impl Tokens<'a>) -> Result<Self, Vec<Error>> {
let mut instance: Self = Self::new(Vec::new(), Vec::new());
let mut errors = Vec::new();
Expand Down
2 changes: 2 additions & 0 deletions bluejay-parser/src/ast/executable/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub struct Field<'a> {
}

impl<'a> FromTokens<'a> for Field<'a> {
#[inline]
fn from_tokens(tokens: &mut impl Tokens<'a>) -> Result<Self, ParseError> {
let has_alias = tokens.peek_punctuator_matches(1, PunctuatorType::Colon);
let (alias, name) = if has_alias {
Expand Down Expand Up @@ -55,6 +56,7 @@ impl<'a> FromTokens<'a> for Field<'a> {
}

impl<'a> IsMatch<'a> for Field<'a> {
#[inline]
fn is_match(tokens: &mut impl Tokens<'a>) -> bool {
tokens.peek_name(0).is_some()
}
Expand Down
2 changes: 2 additions & 0 deletions bluejay-parser/src/ast/executable/fragment_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ pub struct FragmentDefinition<'a> {
}

impl<'a> IsMatch<'a> for FragmentDefinition<'a> {
#[inline]
fn is_match(tokens: &mut impl Tokens<'a>) -> bool {
tokens.peek_name_matches(0, "fragment")
}
}

impl<'a> FromTokens<'a> for FragmentDefinition<'a> {
#[inline]
fn from_tokens(tokens: &mut impl Tokens<'a>) -> Result<Self, ParseError> {
let fragment_identifier_span = tokens.expect_name_value("fragment")?;
let name = tokens.expect_name()?;
Expand Down
2 changes: 2 additions & 0 deletions bluejay-parser/src/ast/executable/fragment_spread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub struct FragmentSpread<'a> {
}

impl<'a> FromTokens<'a> for FragmentSpread<'a> {
#[inline]
fn from_tokens(tokens: &mut impl Tokens<'a>) -> Result<Self, ParseError> {
let ellipse_span = tokens.expect_punctuator(PunctuatorType::Ellipse)?;
let name = tokens.expect_name()?;
Expand All @@ -27,6 +28,7 @@ impl<'a> FromTokens<'a> for FragmentSpread<'a> {
}

impl<'a> IsMatch<'a> for FragmentSpread<'a> {
#[inline]
fn is_match(tokens: &mut impl Tokens<'a>) -> bool {
tokens.peek_punctuator_matches(0, PunctuatorType::Ellipse)
&& tokens
Expand Down
2 changes: 2 additions & 0 deletions bluejay-parser/src/ast/executable/inline_fragment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub struct InlineFragment<'a> {
}

impl<'a> FromTokens<'a> for InlineFragment<'a> {
#[inline]
fn from_tokens(tokens: &mut impl Tokens<'a>) -> Result<Self, ParseError> {
let ellipse_span = tokens.expect_punctuator(PunctuatorType::Ellipse)?;
let type_condition = TypeCondition::try_from_tokens(tokens).transpose()?;
Expand All @@ -28,6 +29,7 @@ impl<'a> FromTokens<'a> for InlineFragment<'a> {
}

impl<'a> IsMatch<'a> for InlineFragment<'a> {
#[inline]
fn is_match(tokens: &mut impl Tokens<'a>) -> bool {
tokens.peek_punctuator_matches(0, PunctuatorType::Ellipse)
&& tokens
Expand Down
2 changes: 2 additions & 0 deletions bluejay-parser/src/ast/executable/operation_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ impl<'a> CoreOperationDefinition for OperationDefinition<'a> {
}

impl<'a> FromTokens<'a> for OperationDefinition<'a> {
#[inline]
fn from_tokens(tokens: &mut impl Tokens<'a>) -> Result<Self, ParseError> {
if let Some(operation_type) = OperationType::try_from_tokens(tokens).transpose()? {
let name = tokens.next_if_name();
Expand All @@ -71,6 +72,7 @@ impl<'a> FromTokens<'a> for OperationDefinition<'a> {
}

impl<'a> IsMatch<'a> for OperationDefinition<'a> {
#[inline]
fn is_match(tokens: &mut impl Tokens<'a>) -> bool {
OperationType::is_match(tokens) || SelectionSet::is_match(tokens)
}
Expand Down
2 changes: 2 additions & 0 deletions bluejay-parser/src/ast/executable/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ impl<'a> CoreSelection for Selection<'a> {
}

impl<'a> FromTokens<'a> for Selection<'a> {
#[inline]
fn from_tokens(tokens: &mut impl Tokens<'a>) -> Result<Self, ParseError> {
if Field::is_match(tokens) {
Field::from_tokens(tokens).map(Self::Field)
Expand All @@ -39,6 +40,7 @@ impl<'a> FromTokens<'a> for Selection<'a> {
}

impl<'a> IsMatch<'a> for Selection<'a> {
#[inline]
fn is_match(tokens: &mut impl Tokens<'a>) -> bool {
Field::is_match(tokens) || tokens.peek_punctuator_matches(0, PunctuatorType::Ellipse)
}
Expand Down
2 changes: 2 additions & 0 deletions bluejay-parser/src/ast/executable/selection_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct SelectionSet<'a> {
}

impl<'a> FromTokens<'a> for SelectionSet<'a> {
#[inline]
fn from_tokens(tokens: &mut impl Tokens<'a>) -> Result<Self, ParseError> {
let open_span = tokens.expect_punctuator(PunctuatorType::OpenBrace)?;
let mut selections: Vec<Selection> = Vec::new();
Expand All @@ -26,6 +27,7 @@ impl<'a> FromTokens<'a> for SelectionSet<'a> {
}

impl<'a> IsMatch<'a> for SelectionSet<'a> {
#[inline]
fn is_match(tokens: &mut impl Tokens<'a>) -> bool {
tokens.peek_punctuator_matches(0, PunctuatorType::OpenBrace)
}
Expand Down
2 changes: 2 additions & 0 deletions bluejay-parser/src/ast/executable/type_condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub struct TypeCondition<'a> {
}

impl<'a> FromTokens<'a> for TypeCondition<'a> {
#[inline]
fn from_tokens(tokens: &mut impl Tokens<'a>) -> Result<Self, ParseError> {
tokens.expect_name_value(Self::ON)?;
let named_type = tokens.expect_name()?;
Expand All @@ -15,6 +16,7 @@ impl<'a> FromTokens<'a> for TypeCondition<'a> {
}

impl<'a> IsMatch<'a> for TypeCondition<'a> {
#[inline]
fn is_match(tokens: &mut impl Tokens<'a>) -> bool {
tokens.peek_name_matches(0, Self::ON)
}
Expand Down
1 change: 1 addition & 0 deletions bluejay-parser/src/ast/executable/variable_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub struct VariableDefinition<'a> {
}

impl<'a> FromTokens<'a> for VariableDefinition<'a> {
#[inline]
fn from_tokens(tokens: &mut impl Tokens<'a>) -> Result<Self, ParseError> {
let variable = Variable::from_tokens(tokens)?;
tokens.expect_punctuator(PunctuatorType::Colon)?;
Expand Down
2 changes: 2 additions & 0 deletions bluejay-parser/src/ast/executable/variable_definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct VariableDefinitions<'a> {
}

impl<'a> FromTokens<'a> for VariableDefinitions<'a> {
#[inline]
fn from_tokens(tokens: &mut impl Tokens<'a>) -> Result<Self, ParseError> {
let open_span = tokens.expect_punctuator(PunctuatorType::OpenRoundBracket)?;
let mut variable_definitions: Vec<VariableDefinition> = Vec::new();
Expand All @@ -29,6 +30,7 @@ impl<'a> FromTokens<'a> for VariableDefinitions<'a> {
}

impl<'a> IsMatch<'a> for VariableDefinitions<'a> {
#[inline]
fn is_match(tokens: &mut impl Tokens<'a>) -> bool {
tokens.peek_punctuator_matches(0, PunctuatorType::OpenRoundBracket)
}
Expand Down
1 change: 1 addition & 0 deletions bluejay-parser/src/ast/executable/variable_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ impl<'a> CoreVariableType for VariableType<'a> {
}

impl<'a> FromTokens<'a> for VariableType<'a> {
#[inline]
fn from_tokens(tokens: &mut impl Tokens<'a>) -> Result<Self, ParseError> {
if let Some(open_span) = tokens.next_if_punctuator(PunctuatorType::OpenSquareBracket) {
let inner = Box::new(VariableType::from_tokens(tokens)?);
Expand Down
2 changes: 2 additions & 0 deletions bluejay-parser/src/ast/operation_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ impl From<&OperationType> for bluejay_core::OperationType {
}

impl<'a> FromTokens<'a> for OperationType {
#[inline]
fn from_tokens(tokens: &mut impl Tokens<'a>) -> Result<Self, ParseError> {
tokens.expect_name().and_then(|name| {
match bluejay_core::OperationType::try_from(name.as_str()) {
Expand All @@ -37,6 +38,7 @@ impl<'a> FromTokens<'a> for OperationType {
}

impl<'a> IsMatch<'a> for OperationType {
#[inline]
fn is_match(tokens: &mut impl Tokens<'a>) -> bool {
bluejay_core::OperationType::POSSIBLE_VALUES
.iter()
Expand Down
3 changes: 3 additions & 0 deletions bluejay-parser/src/ast/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ pub struct ParseOptions {
}

pub trait Parse<'a>: Sized {
#[inline]
fn parse(s: &'a str) -> Result<Self, Vec<Error>> {
Self::parse_with_options(s, Default::default())
}

#[inline]
fn parse_with_options(s: &'a str, options: ParseOptions) -> Result<Self, Vec<Error>> {
let lexer =
LogosLexer::new(s).with_graphql_ruby_compatibility(options.graphql_ruby_compatibility);
Expand All @@ -24,6 +26,7 @@ pub trait Parse<'a>: Sized {
}

impl<'a, T: FromTokens<'a>> Parse<'a> for T {
#[inline]
fn parse_from_tokens(mut tokens: impl Tokens<'a>) -> Result<Self, Vec<Error>> {
let result = T::from_tokens(&mut tokens);

Expand Down
Loading

0 comments on commit f5663c2

Please sign in to comment.