-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add models for JIT (#2213)
- Loading branch information
1 parent
5af4bcc
commit d5c0a22
Showing
19 changed files
with
1,547 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,38 @@ | ||
use std::cell::Cell; | ||
use std::sync::Mutex; | ||
|
||
pub trait Count { | ||
type Item; | ||
fn next(&self) -> Self::Item; | ||
} | ||
|
||
#[allow(unused)] | ||
#[derive(Default)] | ||
pub struct Counter(Cell<usize>); | ||
impl Counter { | ||
pub fn new(start: usize) -> Self { | ||
pub struct Counter<A>(Cell<A>); | ||
impl<A> Counter<A> { | ||
pub fn new(start: A) -> Self { | ||
Self(Cell::new(start)) | ||
} | ||
pub fn next(&self) -> usize { | ||
} | ||
|
||
impl<A: Copy + num::Num> Count for Counter<A> { | ||
type Item = A; | ||
|
||
fn next(&self) -> A { | ||
let curr = self.0.get(); | ||
self.0.set(curr + 1); | ||
self.0.set(curr + A::one()); | ||
curr | ||
} | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct AtomicCounter<A>(Mutex<A>); | ||
|
||
impl<A: Copy + num::Num> Count for AtomicCounter<A> { | ||
type Item = A; | ||
|
||
fn next(&self) -> A { | ||
let mut x = self.0.lock().unwrap(); | ||
*x = *x + A::one(); | ||
*x | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
use std::collections::HashMap; | ||
|
||
use crate::core::blueprint::{ | ||
Blueprint, Definition, FieldDefinition, InputFieldDefinition, SchemaDefinition, | ||
}; | ||
|
||
/// | ||
/// A read optimized index of all the fields in the Blueprint. Provide O(1) | ||
/// access to getting any field information. | ||
#[allow(unused)] | ||
pub struct BlueprintIndex { | ||
map: HashMap<String, (Definition, HashMap<String, QueryField>)>, | ||
schema: SchemaDefinition, | ||
} | ||
|
||
#[allow(unused)] | ||
#[derive(Debug)] | ||
pub enum QueryField { | ||
Field((FieldDefinition, HashMap<String, InputFieldDefinition>)), | ||
InputField(InputFieldDefinition), | ||
} | ||
|
||
#[allow(unused)] | ||
impl QueryField { | ||
pub fn get_arg(&self, arg_name: &str) -> Option<&InputFieldDefinition> { | ||
match self { | ||
QueryField::Field((_, args)) => args.get(arg_name), | ||
QueryField::InputField(_) => None, | ||
} | ||
} | ||
} | ||
|
||
impl BlueprintIndex { | ||
pub fn init(blueprint: &Blueprint) -> Self { | ||
let mut map = HashMap::new(); | ||
|
||
for definition in blueprint.definitions.iter() { | ||
match definition { | ||
Definition::Object(object_def) => { | ||
let type_name = object_def.name.clone(); | ||
let mut fields_map = HashMap::new(); | ||
|
||
for field in &object_def.fields { | ||
let args_map = HashMap::from_iter( | ||
field | ||
.args | ||
.iter() | ||
.map(|v| (v.name.clone(), v.clone())) | ||
.collect::<Vec<_>>(), | ||
); | ||
fields_map.insert( | ||
field.name.clone(), | ||
QueryField::Field((field.clone(), args_map)), | ||
); | ||
} | ||
|
||
map.insert( | ||
type_name, | ||
(Definition::Object(object_def.to_owned()), fields_map), | ||
); | ||
} | ||
Definition::Interface(interface_def) => { | ||
let type_name = interface_def.name.clone(); | ||
let mut fields_map = HashMap::new(); | ||
|
||
for field in interface_def.fields.clone() { | ||
let args_map = HashMap::from_iter( | ||
field | ||
.args | ||
.iter() | ||
.map(|v| (v.name.clone(), v.clone())) | ||
.collect::<Vec<_>>(), | ||
); | ||
fields_map.insert(field.name.clone(), QueryField::Field((field, args_map))); | ||
} | ||
|
||
map.insert( | ||
type_name, | ||
(Definition::Interface(interface_def.to_owned()), fields_map), | ||
); | ||
} | ||
Definition::InputObject(input_object_def) => { | ||
let type_name = input_object_def.name.clone(); | ||
let mut fields_map = HashMap::new(); | ||
|
||
for field in input_object_def.fields.clone() { | ||
fields_map.insert(field.name.clone(), QueryField::InputField(field)); | ||
} | ||
|
||
map.insert( | ||
type_name, | ||
( | ||
Definition::InputObject(input_object_def.to_owned()), | ||
fields_map, | ||
), | ||
); | ||
} | ||
Definition::Scalar(scalar_def) => { | ||
let type_name = scalar_def.name.clone(); | ||
map.insert( | ||
type_name.clone(), | ||
(Definition::Scalar(scalar_def.to_owned()), HashMap::new()), | ||
); | ||
} | ||
Definition::Enum(enum_def) => { | ||
let type_name = enum_def.name.clone(); | ||
map.insert( | ||
type_name.clone(), | ||
(Definition::Enum(enum_def.to_owned()), HashMap::new()), | ||
); | ||
} | ||
Definition::Union(union_def) => { | ||
let type_name = union_def.name.clone(); | ||
map.insert( | ||
type_name.clone(), | ||
(Definition::Union(union_def.to_owned()), HashMap::new()), | ||
); | ||
} | ||
} | ||
} | ||
|
||
Self { map, schema: blueprint.schema.to_owned() } | ||
} | ||
|
||
#[allow(unused)] | ||
pub fn get_field(&self, type_name: &str, field_name: &str) -> Option<&QueryField> { | ||
self.map | ||
.get(type_name) | ||
.and_then(|(_, fields_map)| fields_map.get(field_name)) | ||
} | ||
|
||
pub fn get_query(&self) -> &String { | ||
&self.schema.query | ||
} | ||
|
||
#[allow(unused)] | ||
pub fn get_mutation(&self) -> Option<&str> { | ||
self.schema.mutation.as_deref() | ||
} | ||
} |
Oops, something went wrong.
d5c0a22
There was a problem hiding this comment.
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
449417 requests in 30.01s, 2.25GB read
Requests/sec: 14973.13
Transfer/sec: 76.85MB