Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(federation): borrow selection keys #6074

Merged
merged 18 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ dependencies = [
"apollo-compiler",
"derive_more",
"either",
"hashbrown 0.15.0",
"hex",
"indexmap 2.2.6",
"insta",
Expand Down Expand Up @@ -2577,6 +2578,12 @@ version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"

[[package]]
name = "foldhash"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f81ec6369c545a7d40e4589b5597581fa1c441fe1cce96dd1de43159910a36a2"

[[package]]
name = "forbid-anonymous-operations"
version = "0.1.0"
Expand Down Expand Up @@ -3085,6 +3092,17 @@ dependencies = [
"allocator-api2",
]

[[package]]
name = "hashbrown"
version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e087f84d4f86bf4b218b927129862374b72199ae7d8657835f1e89000eea4fb"
dependencies = [
"allocator-api2",
"equivalent",
"foldhash",
]

[[package]]
name = "hdrhistogram"
version = "7.5.4"
Expand Down
3 changes: 2 additions & 1 deletion about.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ accepted = [
"LicenseRef-ring",
"MIT",
"MPL-2.0",
"Unicode-DFS-2016"
"Unicode-DFS-2016",
"Zlib"
]

# See https://github.com/EmbarkStudios/cargo-about/pull/216
Expand Down
1 change: 1 addition & 0 deletions apollo-federation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ time = { version = "0.3.34", default-features = false, features = [
"local-offset",
] }
derive_more = "0.99.17"
hashbrown = "0.15.0"
indexmap = { version = "2.2.6", features = ["serde"] }
itertools = "0.13.0"
lazy_static = "1.4.0"
Expand Down
6 changes: 3 additions & 3 deletions apollo-federation/src/operation/contains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,15 @@ impl SelectionSet {
let mut is_equal = true;
let mut did_ignore_typename = false;

for (key, other_selection) in other.selections.iter() {
if key.is_typename_field() && options.ignore_missing_typename {
for other_selection in other.selections.values() {
if other_selection.is_typename_field() && options.ignore_missing_typename {
if !self.has_top_level_typename_field() {
did_ignore_typename = true;
}
continue;
}

let Some(self_selection) = self.selections.get(key) else {
let Some(self_selection) = self.selections.get(other_selection.key()) else {
return Containment::NotContained;
};

Expand Down
17 changes: 9 additions & 8 deletions apollo-federation/src/operation/merging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ impl SelectionSet {
let target = Arc::make_mut(&mut self.selections);
for other_selection in others {
let other_key = other_selection.key();
match target.entry(other_key.clone()) {
match target.entry(other_key) {
selection_map::Entry::Occupied(existing) => match existing.get() {
Selection::Field(self_field_selection) => {
let Selection::Field(other_field_selection) = other_selection else {
Expand All @@ -193,7 +193,7 @@ impl SelectionSet {
);
};
fields
.entry(other_key)
.entry(other_key.to_owned_key())
Copy link
Member Author

@goto-bus-stop goto-bus-stop Sep 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not easy to rewrite to borrowed keys, because it creates intermediate maps grouping selections that have the same key. We can probably rewrite merging without the intermediate maps entirely later. Note the previous version was also cloning keys here, so it's not a regression.

.or_insert_with(Vec::new)
.push(other_field_selection);
}
Expand All @@ -207,7 +207,7 @@ impl SelectionSet {
);
};
fragment_spreads
.entry(other_key)
.entry(other_key.to_owned_key())
.or_insert_with(Vec::new)
.push(other_fragment_spread_selection);
}
Expand All @@ -226,7 +226,7 @@ impl SelectionSet {
);
};
inline_fragments
.entry(other_key)
.entry(other_key.to_owned_key())
.or_insert_with(Vec::new)
.push(other_inline_fragment_selection);
}
Expand All @@ -237,18 +237,19 @@ impl SelectionSet {
}
}

for (key, self_selection) in target.iter_mut() {
for self_selection in target.values_mut() {
let key = self_selection.key().to_owned_key();
match self_selection {
SelectionValue::Field(mut self_field_selection) => {
if let Some(other_field_selections) = fields.shift_remove(key) {
if let Some(other_field_selections) = fields.shift_remove(&key) {
self_field_selection.merge_into(
other_field_selections.iter().map(|selection| &***selection),
)?;
}
}
SelectionValue::FragmentSpread(mut self_fragment_spread_selection) => {
if let Some(other_fragment_spread_selections) =
fragment_spreads.shift_remove(key)
fragment_spreads.shift_remove(&key)
{
self_fragment_spread_selection.merge_into(
other_fragment_spread_selections
Expand All @@ -259,7 +260,7 @@ impl SelectionSet {
}
SelectionValue::InlineFragment(mut self_inline_fragment_selection) => {
if let Some(other_inline_fragment_selections) =
inline_fragments.shift_remove(key)
inline_fragments.shift_remove(&key)
{
self_inline_fragment_selection.merge_into(
other_inline_fragment_selections
Expand Down
Loading
Loading