Skip to content

Commit 289fc85

Browse files
committed
*: Address review on cache improvements
1 parent 38dbe4a commit 289fc85

File tree

5 files changed

+34
-28
lines changed

5 files changed

+34
-28
lines changed

graph/src/data/query/result.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ impl QueryResult {
5757
}
5858

5959
pub fn append(&mut self, mut other: QueryResult) {
60+
// Currently we don't used extensions, the desired behaviour for merging them is tbd.
61+
assert!(self.extensions.is_none());
62+
assert!(other.extensions.is_none());
63+
6064
match (&mut self.data, &mut other.data) {
6165
(Some(q::Value::Object(ours)), Some(q::Value::Object(other))) => ours.append(other),
6266

@@ -73,10 +77,6 @@ impl QueryResult {
7377
// Only one side has errors, use that.
7478
_ => self.errors = self.errors.take().or(other.errors),
7579
}
76-
77-
// Currently we don't used extensions, the desired behaviour for merging them is tbd.
78-
assert!(self.extensions.is_none());
79-
assert!(other.extensions.is_none());
8080
}
8181
}
8282

graph/src/data/store/mod.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::data::subgraph::SubgraphDeploymentId;
2-
use crate::prelude::{format_err, EntityKey, QueryExecutionError};
2+
use crate::prelude::{format_err, CacheWeight, EntityKey, QueryExecutionError};
33
use failure::Error;
44
use graphql_parser::query;
55
use graphql_parser::schema;
@@ -580,6 +580,12 @@ impl<'a> From<Vec<(&'a str, Value)>> for Entity {
580580
}
581581
}
582582

583+
impl CacheWeight for Entity {
584+
fn indirect_weight(&self) -> usize {
585+
self.0.indirect_weight()
586+
}
587+
}
588+
583589
/// A value that can (maybe) be converted to an `Entity`.
584590
pub trait TryIntoEntity {
585591
fn try_into_entity(self) -> Result<Entity, Error>;

graph/src/util/cache_weight.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
use crate::prelude::{BigDecimal, BigInt, Entity, Value};
1+
use crate::prelude::{BigDecimal, BigInt, Value};
2+
use std::mem;
23

34
/// Estimate of how much memory a value consumes.
45
/// Useful for measuring the size of caches.
56
pub trait CacheWeight {
67
/// Total weight of the value.
78
fn weight(&self) -> usize {
8-
std::mem::size_of_val(&self) + self.indirect_weight()
9+
mem::size_of_val(&self) + self.indirect_weight()
910
}
1011

1112
/// The weight of values pointed to by this value but logically owned by it, which is not
@@ -24,7 +25,8 @@ impl<T: CacheWeight> CacheWeight for Option<T> {
2425

2526
impl<T: CacheWeight> CacheWeight for Vec<T> {
2627
fn indirect_weight(&self) -> usize {
27-
self.iter().map(CacheWeight::indirect_weight).sum()
28+
self.iter().map(CacheWeight::indirect_weight).sum::<usize>()
29+
+ self.capacity() * mem::size_of::<T>()
2830
}
2931
}
3032

@@ -36,15 +38,18 @@ impl<T: CacheWeight, U: CacheWeight> CacheWeight for std::collections::BTreeMap<
3638
}
3739
}
3840

39-
impl CacheWeight for &'_ [u8] {
41+
impl<T: CacheWeight, U: CacheWeight> CacheWeight for std::collections::HashMap<T, U> {
4042
fn indirect_weight(&self) -> usize {
41-
self.len()
43+
self.iter()
44+
.map(|(key, value)| key.indirect_weight() + value.indirect_weight())
45+
.sum::<usize>()
46+
+ self.capacity() * mem::size_of::<T>()
4247
}
4348
}
4449

4550
impl CacheWeight for String {
4651
fn indirect_weight(&self) -> usize {
47-
self.len()
52+
self.capacity()
4853
}
4954
}
5055

@@ -60,27 +65,25 @@ impl CacheWeight for BigInt {
6065
}
6166
}
6267

68+
impl CacheWeight for crate::data::store::scalar::Bytes {
69+
fn indirect_weight(&self) -> usize {
70+
self.as_slice().len()
71+
}
72+
}
73+
6374
impl CacheWeight for Value {
6475
fn indirect_weight(&self) -> usize {
6576
match self {
6677
Value::String(s) => s.indirect_weight(),
6778
Value::BigDecimal(d) => d.indirect_weight(),
6879
Value::List(values) => values.indirect_weight(),
69-
Value::Bytes(bytes) => bytes.as_slice().indirect_weight(),
80+
Value::Bytes(bytes) => bytes.indirect_weight(),
7081
Value::BigInt(n) => n.indirect_weight(),
7182
Value::Int(_) | Value::Bool(_) | Value::Null => 0,
7283
}
7384
}
7485
}
7586

76-
impl CacheWeight for Entity {
77-
fn indirect_weight(&self) -> usize {
78-
self.iter()
79-
.map(|(key, value)| key.weight() + value.weight())
80-
.sum()
81-
}
82-
}
83-
8487
impl CacheWeight for graphql_parser::query::Value {
8588
fn indirect_weight(&self) -> usize {
8689
use graphql_parser::query as q;

graphql/src/execution/cache.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@ use std::sync::{Arc, Condvar, Mutex};
99
type Hash = <SetHasher as StableHasher>::Out;
1010

1111
/// The 'true' cache entry that lives inside the Arc.
12-
/// When the last Arc is dropped, this is dropped,
13-
/// and the cache is removed.
12+
/// When the last Arc is dropped, this is dropped, and the cache is removed.
1413
#[derive(Debug)]
1514
struct CacheEntryInner<R> {
16-
hash: Hash,
1715
// Considered using once_cell::sync::Lazy,
1816
// but that quickly becomes a mess of generics
1917
// or runs into the issue that Box<dyn FnOnce> can't be
@@ -26,9 +24,8 @@ struct CacheEntryInner<R> {
2624
}
2725

2826
impl<R> CacheEntryInner<R> {
29-
fn new(hash: Hash) -> Arc<Self> {
27+
fn new() -> Arc<Self> {
3028
Arc::new(Self {
31-
hash,
3229
result: OnceCell::new(),
3330
condvar: Condvar::new(),
3431
lock: Mutex::new(false),
@@ -134,7 +131,7 @@ impl<R: CheapClone> QueryCache<R> {
134131
return entry.wait().cheap_clone();
135132
}
136133
Entry::Vacant(entry) => {
137-
let uncached = CacheEntryInner::new(hash);
134+
let uncached = CacheEntryInner::new();
138135
entry.insert(uncached.clone());
139136
uncached
140137
}

server/http/src/response.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ mod tests {
100100
#[test]
101101
fn generates_401_for_query_errors() {
102102
let parse_error = graphql_parser::parse_query("<>?><").unwrap_err();
103-
let query_error = QueryError::ParseError(parse_error.into());
103+
let query_error = QueryError::ParseError(Arc::new(parse_error.compat().into()));
104104
let future = GraphQLResponse::new(Err(GraphQLServerError::from(query_error)));
105105
let response = future.wait().expect("Should generate a response");
106106
test_utils::assert_error_response(response, StatusCode::BAD_REQUEST);
@@ -170,7 +170,7 @@ mod tests {
170170
fn generates_valid_json_for_query_error() {
171171
let parse_error =
172172
graphql_parser::parse_query("<><?").expect_err("Should fail parsing an invalid query");
173-
let query_error = QueryError::ParseError(parse_error.into());
173+
let query_error = QueryError::ParseError(Arc::new(parse_error.compat().into()));
174174
let err = GraphQLServerError::QueryError(query_error);
175175
let future = GraphQLResponse::new(Err(err));
176176
let response = future.wait().expect("Should generate a response");

0 commit comments

Comments
 (0)