Skip to content

Conversation

@kamilkisiela
Copy link
Contributor

@kamilkisiela kamilkisiela commented Jan 5, 2026

This PR significantly improves JSON response serialization (response projection) performance (50% faster) by replacing the existing character-by-character string escaping logic with a SIMD-accelerated implementation adapted from sonic-rs.

I also improved estimate_final_response_size() with 20% buffer for JSON overhead, that according to the tests I did and responses in the federation audit, is a good estimate.

This code is complex and challenging to maintain, due to the low-level SIMD operations, unsafe pointer manipulation, and platform-specific optimizations. However, the previous implementation was also difficult to maintain in its own way.

The key advantage here is that we're leveraging battle-tested code from sonic-rs, a library we already depend on indirectly.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @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 overhauls the internal mechanism for writing and escaping JSON strings, adopting a high-performance, SIMD-optimized solution. The goal is to boost the efficiency of JSON serialization within the executor. Additionally, it refines the estimation of final response sizes to better reflect the actual memory footprint after JSON formatting.

Highlights

  • Refactored JSON String Escaping: The core logic for writing and escaping JSON strings has been completely rewritten, moving from a custom implementation to a highly optimized, SIMD-accelerated approach inspired by the cloudwego/sonic-rs project. This change is expected to significantly improve performance for JSON serialization.
  • New Dependency: sonic-simd: The sonic-simd crate has been introduced and integrated to provide the underlying SIMD capabilities for the new string escaping logic, enhancing efficiency by processing multiple bytes simultaneously.
  • Improved Response Size Estimation: The estimate_final_response_size function now includes a 20% buffer in its calculation. This buffer accounts for the additional overhead introduced by JSON syntax, character escaping, and other formatting, leading to more accurate size predictions.
  • Enhanced Test Coverage: New test cases have been added for the format_string function, thoroughly validating the correctness of the updated JSON string quoting and escaping logic, including scenarios with special characters and long strings.

🧠 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 Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a significant performance improvement by replacing the existing JSON string escaping logic with a SIMD-accelerated implementation from sonic-rs. While the performance gain is valuable, the new code introduces a large amount of unsafe Rust. My review focuses on ensuring this new code adheres to the project's style guide, particularly regarding safety, and improving the maintainability of the new tests and heuristics.

@github-actions
Copy link

github-actions bot commented Jan 5, 2026

k6-benchmark results

     ✓ response code was 200
     ✓ no graphql errors
     ✓ valid response structure

     █ setup

     checks.........................: 100.00% ✓ 221373      ✗ 0    
     data_received..................: 6.5 GB  215 MB/s
     data_sent......................: 87 MB   2.9 MB/s
     http_req_blocked...............: avg=3.54µs   min=631ns   med=1.71µs  max=3.77ms   p(90)=2.47µs  p(95)=2.87µs 
     http_req_connecting............: avg=947ns    min=0s      med=0s      max=2.63ms   p(90)=0s      p(95)=0s     
     http_req_duration..............: avg=19.86ms  min=2.15ms  med=18.85ms max=186.5ms  p(90)=27.25ms p(95)=30.43ms
       { expected_response:true }...: avg=19.86ms  min=2.15ms  med=18.85ms max=186.5ms  p(90)=27.25ms p(95)=30.43ms
     http_req_failed................: 0.00%   ✓ 0           ✗ 73811
     http_req_receiving.............: avg=149.33µs min=26.51µs med=40.7µs  max=142.46ms p(90)=92.6µs  p(95)=398.9µs
     http_req_sending...............: avg=22.76µs  min=5.54µs  med=10.87µs max=18.56ms  p(90)=16.59µs p(95)=28.24µs
     http_req_tls_handshaking.......: avg=0s       min=0s      med=0s      max=0s       p(90)=0s      p(95)=0s     
     http_req_waiting...............: avg=19.69ms  min=2.08ms  med=18.72ms max=89.62ms  p(90)=27ms    p(95)=30.13ms
     http_reqs......................: 73811   2455.073426/s
     iteration_duration.............: avg=20.32ms  min=6.72ms  med=19.21ms max=255.12ms p(90)=27.74ms p(95)=30.92ms
     iterations.....................: 73791   2454.408193/s
     vus............................: 50      min=50        max=50 
     vus_max........................: 50      min=50        max=50 

@github-actions
Copy link

github-actions bot commented Jan 5, 2026

🐋 This PR was built and pushed to the following Docker images:

Image Names: ghcr.io/graphql-hive/router

Platforms: linux/amd64,linux/arm64

Image Tags: ghcr.io/graphql-hive/router:pr-638 ghcr.io/graphql-hive/router:sha-cc9b7e7

Docker metadata
{
"buildx.build.ref": "builder-72488c6f-a481-4923-abba-9d619aca6464/builder-72488c6f-a481-4923-abba-9d619aca64640/sgvymujd983npxn317rlvp061",
"containerimage.descriptor": {
  "mediaType": "application/vnd.oci.image.index.v1+json",
  "digest": "sha256:9fb1d1e6f84daa1a53aec39a9168ee3c29c2b07640d6981294eb0c4b7adbf439",
  "size": 1609
},
"containerimage.digest": "sha256:9fb1d1e6f84daa1a53aec39a9168ee3c29c2b07640d6981294eb0c4b7adbf439",
"image.name": "ghcr.io/graphql-hive/router:pr-638,ghcr.io/graphql-hive/router:sha-cc9b7e7"
}

Added a buffer to the final response size estimate.
Updated the version of the sonic-simd package from 0.1.1 to 0.1.2 and added new dependencies.
Updated documentation link in json_writer.rs
@kamilkisiela kamilkisiela marked this pull request as ready for review January 7, 2026 13:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant