in compiler/crates/relay-transforms/src/skip_redundant_nodes.rs [146:265]
fn transform_selection(
&self,
selection: &Selection,
selection_map: &mut SelectionMap,
) -> Transformed<Selection> {
// This will optimize a traversal of the same subselections.
// If it's the same node, and selection_map is empty
// result of transform_selection has to be the same.
let is_empty = selection_map.0.is_empty();
let identifier = NodeIdentifier::from_selection(&self.schema, selection);
match selection {
Selection::ScalarField(_) | Selection::FragmentSpread(_) => {
if selection_map.0.contains_key(&identifier) {
Transformed::Delete
} else {
selection_map.0.insert(identifier, None);
Transformed::Keep
}
}
Selection::LinkedField(selection) => {
let should_cache = is_empty && Arc::strong_count(selection) > 2;
if should_cache {
let key = PointerAddress::new(selection);
if let Some(cached) = self.cache.get(&key) {
let (cached_result, cached_selection_map) = cached.clone();
*selection_map = cached_selection_map;
return cached_result;
}
}
let result = if let Some(Some(linked_selection_map)) =
selection_map.0.get_mut(&identifier)
{
self.transform_linked_field(selection, linked_selection_map)
.map(Selection::LinkedField)
} else {
let mut linked_selection_map = Default::default();
let result = self
.transform_linked_field(selection, &mut linked_selection_map)
.map(Selection::LinkedField);
if !matches!(result, Transformed::Delete) {
selection_map
.0
.insert(identifier, Some(linked_selection_map));
}
result
};
if should_cache {
let key = PointerAddress::new(selection);
self.cache
.insert(key, (result.clone(), selection_map.clone()));
}
result
}
Selection::Condition(selection) => {
if let Some(Some(existing_selection_map)) = selection_map.0.get_mut(&identifier) {
self.transform_condition(selection, existing_selection_map)
.map(Selection::Condition)
} else {
// Fork the selection map to prevent conditional selections from
// affecting the outer "guaranteed" selections.
let mut next_selection_map = selection_map.clone();
let result = self
.transform_condition(selection, &mut next_selection_map)
.map(Selection::Condition);
if !matches!(result, Transformed::Delete) {
selection_map.0.insert(identifier, Some(next_selection_map));
}
result
}
}
Selection::InlineFragment(selection) => {
let should_cache = is_empty && Arc::strong_count(selection) > 2;
if should_cache {
let key = PointerAddress::new(selection);
if let Some(cached) = self.cache.get(&key) {
let (cached_result, cached_selection_map) = cached.clone();
*selection_map = cached_selection_map;
return cached_result;
}
}
let result = if let Some(Some(existing_selection_map)) =
selection_map.0.get_mut(&identifier)
{
self.transform_inline_fragment(selection, existing_selection_map)
.map(Selection::InlineFragment)
} else if selection
.directives
.iter()
.any(is_relay_custom_inline_fragment_directive)
{
let mut linked_selection_map = Default::default();
let result = self
.transform_inline_fragment(selection, &mut linked_selection_map)
.map(Selection::InlineFragment);
if !matches!(result, Transformed::Delete) {
selection_map
.0
.insert(identifier, Some(linked_selection_map));
}
result
} else {
// Fork for inline fragments for the same reason
let mut next_selection_map = selection_map.clone();
let result = self
.transform_inline_fragment(selection, &mut next_selection_map)
.map(Selection::InlineFragment);
if !matches!(result, Transformed::Delete) {
selection_map.0.insert(identifier, Some(next_selection_map));
}
result
};
if should_cache {
let key = PointerAddress::new(selection);
self.cache
.insert(key, (result.clone(), selection_map.clone()));
}
result
}
}
}