Skip to content

[Feature Request] GRPC Search Response bytes optimization #19310

@karenyrx

Description

@karenyrx

Is your feature request related to a problem? Please describe

Currently, all source data in search hits is converted using:

hitBuilder.setSource(ByteString.copyFrom(
    bytesRef.bytes, bytesRef.offset, bytesRef.length));

This approach creates a defensive copy of the underlying byte array for every search hit, regardless of the source type.

Describe the solution you'd like

Implement a zero-copy optimization that:

  1. Detects BytesArray instances - The most common BytesReference implementation in OpenSearch
  2. Uses UnsafeByteOperations.unsafeWrap() - Avoids memory copying for immutable data
  3. Maintains backward compatibility - Falls back to defensive copying for other implementations

Related component

Search:Performance

Describe alternatives you've considered

No response

Additional context

Expected performance benefits include:

  • Lower serialization latency: especially for large documents
  • Improved throughput: Better performance under high search loads
  • Memory allocation reduction: reduction from BytesArray sources
  • Reduced garbage collection pressure: Fewer temporary objects created

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Status

✅ Done

Status

Done/Won't Do

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions