Skip to content

Commit 7f30957

Browse files
committed
Work around compiler bug
This tweaks the Agora context type we use in order to avoid a recurring Rust compiler bug (rust-lang/rust#71723) which makes it suddenly impossible to carry the context across an await. This comes at the cost of increasing String allocations. Though the use of snmalloc mitigates this cost somewhat.
1 parent 4aca9fa commit 7f30957

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
lines changed

graph-gateway/src/block_constraints.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ pub fn block_constraints<'c>(context: &'c Context<'c>) -> Option<BTreeSet<BlockC
4242
let defaults = query
4343
.variable_definitions
4444
.iter()
45-
.filter(|d| !vars.0.contains_key(d.name))
46-
.filter_map(|d| Some((d.name, d.default_value.as_ref()?.to_graphql())))
47-
.collect::<BTreeMap<&str, StaticValue>>();
45+
.filter(|d| !vars.0.contains_key(&d.name))
46+
.filter_map(|d| Some((d.name.clone(), d.default_value.as_ref()?.to_graphql())))
47+
.collect::<BTreeMap<String, StaticValue>>();
4848
(&query.selection_set, defaults)
4949
}
5050
OperationDefinition::Query(_)
@@ -88,9 +88,9 @@ pub fn make_query_deterministic(
8888
let defaults = query
8989
.variable_definitions
9090
.iter()
91-
.filter(|d| !vars.0.contains_key(d.name))
92-
.filter_map(|d| Some((d.name, d.default_value.as_ref()?.to_graphql())))
93-
.collect::<BTreeMap<&str, StaticValue>>();
91+
.filter(|d| !vars.0.contains_key(&d.name))
92+
.filter_map(|d| Some((d.name.clone(), d.default_value.as_ref()?.to_graphql())))
93+
.collect::<BTreeMap<String, StaticValue>>();
9494
(&mut query.selection_set, defaults)
9595
}
9696
OperationDefinition::Query(_)
@@ -123,7 +123,7 @@ pub fn make_query_deterministic(
123123
None => {
124124
selection_field
125125
.arguments
126-
.push(("block", deterministic_block(&latest.hash)));
126+
.push(("block".to_string(), deterministic_block(&latest.hash)));
127127
}
128128
};
129129
}
@@ -146,21 +146,21 @@ pub fn make_query_deterministic(
146146
.ok()
147147
}
148148

149-
fn deterministic_block<'c>(hash: &Bytes32) -> Value<'c, &'c str> {
149+
fn deterministic_block<'c>(hash: &Bytes32) -> Value<'c, String> {
150150
Value::Object(BTreeMap::from_iter([(
151-
"hash",
151+
"hash".to_string(),
152152
Value::String(hash.to_string()),
153153
)]))
154154
}
155155

156-
fn field_constraint<'c>(
156+
fn field_constraint(
157157
vars: &QueryVariables,
158-
defaults: &BTreeMap<&str, StaticValue>,
159-
field: &Value<'c, &'c str>,
158+
defaults: &BTreeMap<String, StaticValue>,
159+
field: &Value<'_, String>,
160160
) -> Option<BlockConstraint> {
161161
match field {
162162
Value::Object(fields) => parse_constraint(vars, defaults, fields),
163-
Value::Variable(name) => match vars.get(*name)? {
163+
Value::Variable(name) => match vars.get(name)? {
164164
Value::Object(fields) => parse_constraint(vars, defaults, fields),
165165
_ => None,
166166
},
@@ -170,7 +170,7 @@ fn field_constraint<'c>(
170170

171171
fn parse_constraint<'c, T: Text<'c>>(
172172
vars: &QueryVariables,
173-
defaults: &BTreeMap<&str, StaticValue>,
173+
defaults: &BTreeMap<String, StaticValue>,
174174
fields: &BTreeMap<T::Value, Value<'c, T>>,
175175
) -> Option<BlockConstraint> {
176176
let field = fields.iter().at_most_one().ok()?;
@@ -190,7 +190,7 @@ fn parse_constraint<'c, T: Text<'c>>(
190190
fn parse_hash<'c, T: Text<'c>>(
191191
hash: &Value<'c, T>,
192192
variables: &QueryVariables,
193-
defaults: &BTreeMap<&str, StaticValue>,
193+
defaults: &BTreeMap<String, StaticValue>,
194194
) -> Option<Bytes32> {
195195
match hash {
196196
Value::String(hash) => hash.parse().ok(),
@@ -208,7 +208,7 @@ fn parse_hash<'c, T: Text<'c>>(
208208
fn parse_number<'c, T: Text<'c>>(
209209
number: &Value<'c, T>,
210210
variables: &QueryVariables,
211-
defaults: &BTreeMap<&str, StaticValue>,
211+
defaults: &BTreeMap<String, StaticValue>,
212212
) -> Option<u64> {
213213
let n = match number {
214214
Value::Int(n) => n,

graph-gateway/src/client_query.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,6 @@ fn count_top_level_selection_sets(ctx: &AgoraContext) -> anyhow::Result<usize> {
732732
OperationDefinition::Mutation(_) => bail!("Mutations not yet supported"),
733733
OperationDefinition::Subscription(_) => bail!("Subscriptions not yet supported"),
734734
})
735-
.collect::<anyhow::Result<Vec<&SelectionSet<&str>>>>()?;
735+
.collect::<anyhow::Result<Vec<&SelectionSet<String>>>>()?;
736736
Ok(selection_sets.into_iter().map(|set| set.items.len()).sum())
737737
}

indexer-selection/src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ use std::{
3838
sync::Arc,
3939
};
4040

41-
pub type Context<'c> = cost_model::Context<'c, &'c str>;
41+
// We have to use `String` instead of `&'c str` here because of compiler bug triggered when holding
42+
// a context across an await. See https://github.com/rust-lang/rust/issues/71723
43+
pub type Context<'c> = cost_model::Context<'c, String>;
4244

4345
#[derive(Clone, Debug)]
4446
pub struct Selection {

0 commit comments

Comments
 (0)