Skip to content

Commit

Permalink
fix dfs iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
ssddOnTop committed Oct 6, 2024
1 parent 8b9616b commit ab67518
Show file tree
Hide file tree
Showing 19 changed files with 393 additions and 454 deletions.
4 changes: 1 addition & 3 deletions src/core/ir/resolver_context_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,7 @@ impl<'a> From<&'a crate::core::jit::Field<ConstValue>> for SelectionField {
}

impl SelectionField {
fn from_jit_field(
field: &crate::core::jit::Field<ConstValue>,
) -> SelectionField {
fn from_jit_field(field: &crate::core::jit::Field<ConstValue>) -> SelectionField {
let name = field.output_name.to_string();
let type_name = field.type_of.name();
let selection_set = field
Expand Down
8 changes: 3 additions & 5 deletions src/core/jit/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,8 @@ impl Builder {
.map(|(k, v)| (k.node.to_string(), v.node.clone()))
.collect::<Vec<_>>();

directives.push(JitDirective {
name: directive.name.to_string(),
arguments,
});
directives
.push(JitDirective { name: directive.name.to_string(), arguments });
}

let (include, skip) = conditions.into_variable_tuple();
Expand Down Expand Up @@ -248,7 +246,7 @@ impl Builder {
include,
args: Vec::new(),
pos: selection.pos.into(),
selection: vec![], // __typename has no child selection
selection: vec![], // __typename has no child selection
directives,
};

Expand Down
6 changes: 1 addition & 5 deletions src/core/jit/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ impl<'a, Input: Clone, Output> Context<'a, Input, Output> {
}
}

pub fn with_value_and_field(
&self,
value: &'a Output,
field: &'a Field<Input>,
) -> Self {
pub fn with_value_and_field(&self, value: &'a Output, field: &'a Field<Input>) -> Self {
Self {
request: self.request,
args: Self::build_args(field),
Expand Down
56 changes: 32 additions & 24 deletions src/core/jit/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl<Value> Variables<Value> {
}

impl<V> FromIterator<(String, V)> for Variables<V> {
fn from_iter<T: IntoIterator<Item=(String, V)>>(iter: T) -> Self {
fn from_iter<T: IntoIterator<Item = (String, V)>>(iter: T) -> Self {
Self(iter.into_iter().collect())
}
}
Expand All @@ -73,8 +73,8 @@ impl<Input> Field<Input> {

/// Returns the __typename of the value related to this field
pub fn value_type<'a, Output>(&'a self, value: &'a Output) -> &'a str
where
Output: TypedValue<'a>,
where
Output: TypedValue<'a>,
{
value.get_type_name().unwrap_or(self.type_of.name())
}
Expand Down Expand Up @@ -183,16 +183,15 @@ impl<'a, Input> Iterator for DFS<'a, Input> {
type Item = &'a Field<Input>;

fn next(&mut self) -> Option<Self::Item> {
match self.stack.pop() {
None => None,
Some(mut i) => match i.next() {
None => self.next(),
Some(field) => {
self.stack.push(field.selection.iter());
Some(field)
}
},
while let Some(iter) = self.stack.last_mut() {
if let Some(field) = iter.next() {
self.stack.push(field.selection.iter());
return Some(field);
} else {
self.stack.pop();
}
}
None
}
}

Expand Down Expand Up @@ -267,17 +266,15 @@ impl<Input: Clone> Field<Input> {
}
}
}
Self {
selection: children,
..self
}
Self { selection: children, ..self }
}
}

impl<Input: Debug> Debug for Field<Input> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let mut debug_struct = f.debug_struct("Field");
debug_struct.field("id", &self.id);
debug_struct.field("parent_id", &self.parent_id);
debug_struct.field("name", &self.name);
debug_struct.field("output_name", &self.output_name);
if self.ir.is_some() {
Expand Down Expand Up @@ -364,8 +361,8 @@ impl<Input> OperationPlan<Input> {
index: Arc<Index>,
is_introspection_query: bool,
) -> Self
where
Input: Clone,
where
Input: Clone,
{
Self {
root_name: root_name.to_string(),
Expand All @@ -379,7 +376,10 @@ impl<Input> OperationPlan<Input> {
}

/// Remove fields which are skipped
pub fn filter_skipped<Var: for<'b> JsonLike<'b> + Clone>(mut self, variables: &Variables<Var>) -> Self {
pub fn filter_skipped<Var: for<'b> JsonLike<'b> + Clone>(
mut self,
variables: &Variables<Var>,
) -> Self {
filter_skipped_fields(&mut self.selection, variables);

self
Expand All @@ -405,6 +405,11 @@ impl<Input> OperationPlan<Input> {
&self.selection
}

/// Returns a nested [Field] representation
pub fn into_nested(self) -> Vec<Field<Input>> {
self.selection
}

/// Returns a flat [Field] representation
pub fn as_flat(&self) -> DFS<Input> {
DFS { stack: vec![self.selection.iter()] }
Expand Down Expand Up @@ -456,8 +461,8 @@ impl<Input> OperationPlan<Input> {
field: &'a Field<Input>,
value: &'a Output,
) -> bool
where
Output: TypedValue<'a>,
where
Output: TypedValue<'a>,
{
match &field.type_condition {
Some(type_condition) => match value.get_type_name() {
Expand All @@ -474,7 +479,10 @@ impl<Input> OperationPlan<Input> {
}

// TODO: review and rename
fn filter_skipped_fields<Input, Var: for<'b> JsonLike<'b> + Clone>(fields: &mut Vec<Field<Input>>, vars: &Variables<Var>) {
fn filter_skipped_fields<Input, Var: for<'b> JsonLike<'b> + Clone>(
fields: &mut Vec<Field<Input>>,
vars: &Variables<Var>,
) {
fields.retain(|f| !f.skip(vars));
for field in fields {
filter_skipped_fields(&mut field.selection, vars);
Expand Down Expand Up @@ -589,8 +597,8 @@ impl<Value> Positioned<Value> {
}

impl<Value> Positioned<Value>
where
Value: Clone,
where
Value: Clone,
{
pub fn with_path(&mut self, path: Vec<PathSegment>) -> Self {
Self { value: self.value.clone(), pos: self.pos, path }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,31 @@ expression: plan.into_nested()
type_condition: Some(
"Query",
),
extensions: Some(
Nested(
[
selection: [
Field {
id: 1,
name: "user",
output_name: "author",
ir: "Some(..)",
type_of: User,
type_condition: Some(
"Post",
),
selection: [
Field {
id: 1,
name: "user",
output_name: "author",
ir: "Some(..)",
type_of: User,
id: 2,
name: "id",
output_name: "identifier",
type_of: ID!,
type_condition: Some(
"Post",
),
extensions: Some(
Nested(
[
Field {
id: 2,
name: "id",
output_name: "identifier",
type_of: ID!,
type_condition: Some(
"User",
),
directives: [],
},
],
),
"User",
),
directives: [],
},
],
),
),
directives: [],
},
],
directives: [],
},
]
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,18 @@ expression: plan.into_nested()
default_value: None,
},
],
extensions: Some(
Nested(
[
Field {
id: 1,
name: "id",
output_name: "id",
type_of: ID!,
type_condition: Some(
"Post",
),
directives: [],
},
],
),
),
selection: [
Field {
id: 1,
name: "id",
output_name: "id",
type_of: ID!,
type_condition: Some(
"Post",
),
directives: [],
},
],
directives: [],
},
]
Original file line number Diff line number Diff line change
Expand Up @@ -12,51 +12,47 @@ expression: plan.into_nested()
type_condition: Some(
"Query",
),
extensions: Some(
Nested(
[
Field {
id: 1,
name: "id",
output_name: "id",
type_of: ID!,
type_condition: Some(
"User",
),
directives: [
Directive {
name: "options",
arguments: [
(
"paging",
Variable(
Name(
"includeName",
),
),
selection: [
Field {
id: 1,
name: "id",
output_name: "id",
type_of: ID!,
type_condition: Some(
"User",
),
directives: [
Directive {
name: "options",
arguments: [
(
"paging",
Variable(
Name(
"includeName",
),
],
},
],
},
Field {
id: 2,
name: "name",
output_name: "name",
type_of: String!,
type_condition: Some(
"User",
),
include: Some(
Variable(
"includeName",
),
),
),
directives: [],
],
},
],
),
),
},
Field {
id: 2,
name: "name",
output_name: "name",
type_of: String!,
type_condition: Some(
"User",
),
include: Some(
Variable(
"includeName",
),
),
directives: [],
},
],
directives: [],
},
]
Loading

1 comment on commit ab67518

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running 30s test @ http://localhost:8000/graphql

4 threads and 100 connections

Thread Stats Avg Stdev Max +/- Stdev
Latency 4.94ms 2.16ms 34.62ms 75.83%
Req/Sec 5.14k 563.85 5.85k 94.33%

614415 requests in 30.04s, 775.89MB read

Requests/sec: 20453.28

Transfer/sec: 25.83MB

Please sign in to comment.