Avoid clones in impl From<PlanExecutionError> for GraphQLError#666
Avoid clones in impl From<PlanExecutionError> for GraphQLError#666ardatan merged 2 commits intographql_errorfrom
Conversation
Summary of ChangesHello @kamilkisiela, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a targeted optimization to the error handling mechanism, specifically focusing on the conversion from Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request effectively addresses a performance issue by eliminating unnecessary string cloning during error conversion, which aligns well with the repository's focus on performance. The approach of making struct fields public to enable moving values is a valid solution. However, I've provided a couple of suggestions for an alternative implementation that achieves the same performance benefits while preserving encapsulation. By using destructuring within the From implementation, we can avoid modifying the public API of PlanExecutionErrorContext, leading to a more robust and maintainable design.
✅
|
|
🐋 This PR was built and pushed to the following Docker images: Image Names: Platforms: Image Tags: Docker metadata{
"buildx.build.ref": "builder-5f5594e8-a468-49b4-8499-3617dd79c881/builder-5f5594e8-a468-49b4-8499-3617dd79c8810/ekhdd4fp3anuo22mvps5tc4lq",
"containerimage.descriptor": {
"mediaType": "application/vnd.oci.image.index.v1+json",
"digest": "sha256:e8c70900041204ec7f32fe31c4c82b1f53315f15e848938465afb2b7ab0b2edd",
"size": 1609
},
"containerimage.digest": "sha256:e8c70900041204ec7f32fe31c4c82b1f53315f15e848938465afb2b7ab0b2edd",
"image.name": "ghcr.io/graphql-hive/router:pr-666,ghcr.io/graphql-hive/router:sha-6c4e1e7"
} |
**Before**
```rust
impl From<PlanExecutionError> for GraphQLError {
fn from(val: PlanExecutionError) -> Self {
let mut error = GraphQLError::from_message_and_code(val.to_string(), val.error_code());
if let Some(subgraph_name) = val.subgraph_name() { // Returns &Option<String>
error = error.add_subgraph_name(subgraph_name); // Clones via Into<String>
}
if let Some(affected_path) = val.affected_path() { // Returns &Option<String>
error = error.add_affected_path(affected_path); // Clones via Into<String>
}
error
}
}
```
What happens:
1. `val.subgraph_name()` returns `&Option<String>`
2. `add_subgraph_name<TStr: Into<String>>()` expects an `Into<String>`
3. When Rust sees `&String` -> `Into<String>`, it uses the impl that
clones the String
4. Same issue for `affected_path`
We end up with 2 unnecessary heap allocations per error conversion.
**After**
I moved owned values out of the structure, to the `add_subgraph_name`
and `add_affected_path` methods.
When Rust sees `Stirng` -> `Into<String>`, it will just use the owned
String, so no clones.
**Before**
```rust
impl From<PlanExecutionError> for GraphQLError {
fn from(val: PlanExecutionError) -> Self {
let mut error = GraphQLError::from_message_and_code(val.to_string(), val.error_code());
if let Some(subgraph_name) = val.subgraph_name() { // Returns &Option<String>
error = error.add_subgraph_name(subgraph_name); // Clones via Into<String>
}
if let Some(affected_path) = val.affected_path() { // Returns &Option<String>
error = error.add_affected_path(affected_path); // Clones via Into<String>
}
error
}
}
```
What happens:
1. `val.subgraph_name()` returns `&Option<String>`
2. `add_subgraph_name<TStr: Into<String>>()` expects an `Into<String>`
3. When Rust sees `&String` -> `Into<String>`, it uses the impl that
clones the String
4. Same issue for `affected_path`
We end up with 2 unnecessary heap allocations per error conversion.
**After**
I moved owned values out of the structure, to the `add_subgraph_name`
and `add_affected_path` methods.
When Rust sees `Stirng` -> `Into<String>`, it will just use the owned
String, so no clones.
**Before**
```rust
impl From<PlanExecutionError> for GraphQLError {
fn from(val: PlanExecutionError) -> Self {
let mut error = GraphQLError::from_message_and_code(val.to_string(), val.error_code());
if let Some(subgraph_name) = val.subgraph_name() { // Returns &Option<String>
error = error.add_subgraph_name(subgraph_name); // Clones via Into<String>
}
if let Some(affected_path) = val.affected_path() { // Returns &Option<String>
error = error.add_affected_path(affected_path); // Clones via Into<String>
}
error
}
}
```
What happens:
1. `val.subgraph_name()` returns `&Option<String>`
2. `add_subgraph_name<TStr: Into<String>>()` expects an `Into<String>`
3. When Rust sees `&String` -> `Into<String>`, it uses the impl that
clones the String
4. Same issue for `affected_path`
We end up with 2 unnecessary heap allocations per error conversion.
**After**
I moved owned values out of the structure, to the `add_subgraph_name`
and `add_affected_path` methods.
When Rust sees `Stirng` -> `Into<String>`, it will just use the owned
String, so no clones.
Before
What happens:
val.subgraph_name()returns&Option<String>add_subgraph_name<TStr: Into<String>>()expects anInto<String>&String->Into<String>, it uses the impl that clones the Stringaffected_pathWe end up with 2 unnecessary heap allocations per error conversion.
After
I moved owned values out of the structure, to the
add_subgraph_nameandadd_affected_pathmethods.When Rust sees
Stirng->Into<String>, it will just use the owned String, so no clones.