Skip to content

Commit

Permalink
perf: small improvements in Synth (#3051)
Browse files Browse the repository at this point in the history
Co-authored-by: Tushar Mathur <[email protected]>
  • Loading branch information
meskill and tusharmath authored Oct 23, 2024
1 parent 3946b44 commit 9292577
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 15 deletions.
6 changes: 3 additions & 3 deletions ci-benchmark/benchmark.graphql
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
schema @server(port: 8000, hostname: "0.0.0.0") @upstream(baseURL: "http://jsonplaceholder.typicode.com") {
schema @server(port: 8000, hostname: "0.0.0.0") {
query: Query
}

type Query @cache(maxAge: 30000) {
posts: [Post] @http(path: "/posts")
posts: [Post] @http(url: "http://jsonplaceholder.typicode.com/posts")
}

type User {
Expand All @@ -20,5 +20,5 @@ type Post {
userId: Int!
title: String!
body: String!
user: User @http(path: "/users/{{.value.userId}}")
user: User @http(url: "http://jsonplaceholder.typicode.com/users/{{.value.userId}}")
}
11 changes: 3 additions & 8 deletions ci-benchmark/nginx-benchmark.graphql
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
schema
@server(hostname: "0.0.0.0", port: 8000)
@upstream(
baseURL: "http://jsonplaceholder.typicode.com"
poolMaxIdlePerHost: 200
tcpKeepAlive: 60
proxy: {url: "http://127.0.0.1:3000"}
) {
@upstream(poolMaxIdlePerHost: 200, tcpKeepAlive: 60, proxy: {url: "http://127.0.0.1:3000"}) {
query: Query
}

type Query {
posts: [Post] @http(path: "/posts")
posts: [Post] @http(url: "http://jsonplaceholder.typicode.com/posts")
}

type User {
Expand All @@ -27,5 +22,5 @@ type Post {
userId: Int!
title: String!
body: String!
user: User @http(path: "/users/{{value.userId}}")
user: User @http(url: "http://jsonplaceholder.typicode.com/users/{{value.userId}}")
}
4 changes: 4 additions & 0 deletions src/core/jit/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ impl Builder {
.map(|a| a.node.to_string())
.unwrap_or(field_name.to_owned()),
ir,
is_scalar: self.index.type_is_scalar(type_of.name()),
is_enum: self.index.type_is_enum(type_of.name()),
type_of,
type_condition: Some(type_condition.to_string()),
skip,
Expand All @@ -241,6 +243,8 @@ impl Builder {
pos: selection.pos.into(),
selection: vec![], // __typename has no child selection
directives,
is_scalar: true,
is_enum: false,
};

fields.push(typename_field);
Expand Down
4 changes: 4 additions & 0 deletions src/core/jit/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ pub struct Field<Input> {
pub selection: Vec<Field<Input>>,
pub pos: Pos,
pub directives: Vec<Directive<Input>>,
pub is_scalar: bool,
pub is_enum: bool,
}

pub struct DFS<'a, Input> {
Expand Down Expand Up @@ -229,6 +231,8 @@ impl<Input> Field<Input> {
.into_iter()
.map(|directive| directive.try_map(map))
.collect::<Result<_, _>>()?,
is_scalar: self.is_scalar,
is_enum: self.is_enum,
})
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/core/jit/synth/synth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ where
} else {
Err(ValidationError::ValueRequired.into())
}
} else if self.plan.field_is_scalar(node) {
} else if node.is_scalar {
let scalar =
scalar::Scalar::find(node.type_of.name()).unwrap_or(&scalar::Scalar::Empty);

Expand All @@ -154,7 +154,7 @@ where
.into(),
)
}
} else if self.plan.field_is_enum(node) {
} else if node.is_enum {
let check_valid_enum = |value: &Value| -> bool {
value
.as_str()
Expand All @@ -179,7 +179,7 @@ where
} else {
match (value.as_array(), value.as_object()) {
(_, Some(obj)) => {
let mut ans = Value::JsonObject::new();
let mut ans = Value::JsonObject::with_capacity(node.selection.len());

for child in node
.iter()
Expand All @@ -201,7 +201,7 @@ where
Ok(Value::object(ans))
}
(Some(arr), _) => {
let mut ans = vec![];
let mut ans = Vec::with_capacity(arr.len());
for (i, val) in arr.iter().enumerate() {
path.push(PathSegment::Index(i));
let val =
Expand Down
5 changes: 5 additions & 0 deletions src/core/json/borrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ impl<'ctx> JsonObjectLike<'ctx> for ObjectAsVec<'ctx> {
ObjectAsVec::default()
}

fn with_capacity(_n: usize) -> Self {
// TODO: no way to define capacity on ObjectAsVec
ObjectAsVec::new()
}

fn get_key(&self, key: &str) -> Option<&Self::Value> {
self.get(key)
}
Expand Down
4 changes: 4 additions & 0 deletions src/core/json/graphql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ impl<'obj, Value: JsonLike<'obj> + Clone> JsonObjectLike<'obj> for IndexMap<Name
IndexMap::new()
}

fn with_capacity(n: usize) -> Self {
IndexMap::with_capacity(n)
}

fn get_key(&self, key: &str) -> Option<&Self::Value> {
self.get(key)
}
Expand Down
1 change: 1 addition & 0 deletions src/core/json/json_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub trait JsonLike<'json>: Sized {
pub trait JsonObjectLike<'obj>: Sized {
type Value;
fn new() -> Self;
fn with_capacity(n: usize) -> Self;
fn get_key(&self, key: &str) -> Option<&Self::Value>;
fn insert_key(&mut self, key: &'obj str, value: Self::Value);
}
Expand Down
4 changes: 4 additions & 0 deletions src/core/json/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ impl<'obj> JsonObjectLike<'obj> for serde_json::Map<String, serde_json::Value> {
serde_json::Map::new()
}

fn with_capacity(n: usize) -> Self {
serde_json::Map::with_capacity(n)
}

fn get_key(&self, key: &str) -> Option<&serde_json::Value> {
self.get(key)
}
Expand Down

1 comment on commit 9292577

@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 8.70ms 3.97ms 149.44ms 91.74%
Req/Sec 2.93k 374.99 4.01k 79.67%

350126 requests in 30.01s, 1.75GB read

Requests/sec: 11666.32

Transfer/sec: 59.88MB

Please sign in to comment.