Skip to content

Conversation

@karenyrx
Copy link
Contributor

@karenyrx karenyrx commented Sep 12, 2025

Description

Current Behavior
The current implementation of gRPC search hit source conversion in SearchHitProtoUtils.processSource() always uses ByteString.copyFrom() to create Protocol Buffer byte strings, which always creates a defensive copy of the underlying byte array for every search hit, regardless of the source type. This results in unnecessary memory copying and allocation overhead.

Proposed Enhancement
This PR implements a zero-copy optimization for BytesArray sources, which:
a) Detects when the source is a BytesArray instance and using UnsafeByteOperations.unsafeWrap() for BytesArray sources
b) Maintains backward compatibility by falling back to ByteString.copyFrom() for other BytesReference implementations

Expected benefits
This should improve:

  • serialization latency for large documents
  • better memory efficiency - by eliminating unecessary memory allocation for BytesArray sources
  • improved CPU performance - by removing memory copy operations

Related Issues

Resolves #19310

Check List

  • Functionality includes testing.
  • API changes companion pull request created, if applicable.
  • Public documentation issue/PR created, if applicable.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

@github-actions
Copy link
Contributor

❌ Gradle check result for f87cce9: FAILURE

Please examine the workflow log, locate, and copy-paste the failure(s) below, then iterate to green. Is the failure a flaky test unrelated to your change?

… BytesRef

Signed-off-by: karenx <karenx@uber.com>
@github-actions
Copy link
Contributor

✅ Gradle check result for b13b325: SUCCESS

@codecov
Copy link

codecov bot commented Sep 13, 2025

Codecov Report

❌ Patch coverage is 85.71429% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 72.82%. Comparing base (6c4f3d8) to head (47e55dc).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
...rpc/proto/response/search/SearchHitProtoUtils.java 85.71% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main   #19280      +/-   ##
============================================
- Coverage     72.88%   72.82%   -0.07%     
+ Complexity    69841    69798      -43     
============================================
  Files          5673     5673              
  Lines        320756   320756              
  Branches      46370    46370              
============================================
- Hits         233796   233590     -206     
- Misses        68102    68272     +170     
- Partials      18858    18894      +36     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Signed-off-by: karenx <karenx@uber.com>
@github-actions github-actions bot added enhancement Enhancement or improvement to existing feature or request Search:Performance labels Sep 16, 2025
@github-actions
Copy link
Contributor

✅ Gradle check result for edae7f3: SUCCESS

@karenyrx karenyrx marked this pull request as ready for review September 16, 2025 16:54
@karenyrx karenyrx requested a review from a team as a code owner September 16, 2025 16:54
Signed-off-by: Karen X <karenxyr@gmail.com>
@github-actions
Copy link
Contributor

❌ Gradle check result for dfa762f: FAILURE

Please examine the workflow log, locate, and copy-paste the failure(s) below, then iterate to green. Is the failure a flaky test unrelated to your change?

@github-actions
Copy link
Contributor

❌ Gradle check result for 8b9e86a: FAILURE

Please examine the workflow log, locate, and copy-paste the failure(s) below, then iterate to green. Is the failure a flaky test unrelated to your change?

Signed-off-by: karenx <karenx@uber.com>
@github-actions
Copy link
Contributor

❌ Gradle check result for caf9cab: null

Please examine the workflow log, locate, and copy-paste the failure(s) below, then iterate to green. Is the failure a flaky test unrelated to your change?

Signed-off-by: Karen X <karenxyr@gmail.com>
@github-actions
Copy link
Contributor

❌ Gradle check result for eec47c3: FAILURE

Please examine the workflow log, locate, and copy-paste the failure(s) below, then iterate to green. Is the failure a flaky test unrelated to your change?

Signed-off-by: Karen X <karenxyr@gmail.com>
@github-actions
Copy link
Contributor

✅ Gradle check result for 47e55dc: SUCCESS

@andrross andrross merged commit 5babdca into opensearch-project:main Sep 17, 2025
31 checks passed
jainankitk pushed a commit to jainankitk/OpenSearch that referenced this pull request Sep 22, 2025
… BytesRef (opensearch-project#19280)

* [GRPC] Optimize source conversion in gRPC search hits using zero-copy BytesRef

Signed-off-by: karenx <karenx@uber.com>

* code cov

Signed-off-by: karenx <karenx@uber.com>

* resolve merge conflicts

Signed-off-by: karenx <karenx@uber.com>

---------

Signed-off-by: karenx <karenx@uber.com>
Signed-off-by: Karen X <karenxyr@gmail.com>
Co-authored-by: karenx <karenx@uber.com>
jainankitk pushed a commit to jainankitk/OpenSearch that referenced this pull request Sep 22, 2025
… BytesRef (opensearch-project#19280)

* [GRPC] Optimize source conversion in gRPC search hits using zero-copy BytesRef

Signed-off-by: karenx <karenx@uber.com>

* code cov

Signed-off-by: karenx <karenx@uber.com>

* resolve merge conflicts

Signed-off-by: karenx <karenx@uber.com>

---------

Signed-off-by: karenx <karenx@uber.com>
Signed-off-by: Karen X <karenxyr@gmail.com>
Co-authored-by: karenx <karenx@uber.com>
Signed-off-by: Ankit Jain <jainankitk@apache.org>
jainankitk pushed a commit to jainankitk/OpenSearch that referenced this pull request Sep 22, 2025
… BytesRef (opensearch-project#19280)

* [GRPC] Optimize source conversion in gRPC search hits using zero-copy BytesRef

Signed-off-by: karenx <karenx@uber.com>

* code cov

Signed-off-by: karenx <karenx@uber.com>

* resolve merge conflicts

Signed-off-by: karenx <karenx@uber.com>

---------

Signed-off-by: karenx <karenx@uber.com>
Signed-off-by: Karen X <karenxyr@gmail.com>
Co-authored-by: karenx <karenx@uber.com>
Signed-off-by: Ankit Jain <jainankitk@apache.org>
asimmahmood1 pushed a commit to jainankitk/OpenSearch that referenced this pull request Sep 23, 2025
… BytesRef (opensearch-project#19280)

* [GRPC] Optimize source conversion in gRPC search hits using zero-copy BytesRef

Signed-off-by: karenx <karenx@uber.com>

* code cov

Signed-off-by: karenx <karenx@uber.com>

* resolve merge conflicts

Signed-off-by: karenx <karenx@uber.com>

---------

Signed-off-by: karenx <karenx@uber.com>
Signed-off-by: Karen X <karenxyr@gmail.com>
Co-authored-by: karenx <karenx@uber.com>
pranikum pushed a commit to pranikum/OpenSearch that referenced this pull request Sep 23, 2025
… BytesRef (opensearch-project#19280)

* [GRPC] Optimize source conversion in gRPC search hits using zero-copy BytesRef

Signed-off-by: karenx <karenx@uber.com>

* code cov

Signed-off-by: karenx <karenx@uber.com>

* resolve merge conflicts

Signed-off-by: karenx <karenx@uber.com>

---------

Signed-off-by: karenx <karenx@uber.com>
Signed-off-by: Karen X <karenxyr@gmail.com>
Co-authored-by: karenx <karenx@uber.com>
vinaykpud pushed a commit to vinaykpud/OpenSearch that referenced this pull request Sep 26, 2025
… BytesRef (opensearch-project#19280)

* [GRPC] Optimize source conversion in gRPC search hits using zero-copy BytesRef

Signed-off-by: karenx <karenx@uber.com>

* code cov

Signed-off-by: karenx <karenx@uber.com>

* resolve merge conflicts

Signed-off-by: karenx <karenx@uber.com>

---------

Signed-off-by: karenx <karenx@uber.com>
Signed-off-by: Karen X <karenxyr@gmail.com>
Co-authored-by: karenx <karenx@uber.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Enhancement or improvement to existing feature or request Search:Performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request] GRPC Search Response bytes optimization

3 participants