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:
- Detects
BytesArray instances - The most common BytesReference implementation in OpenSearch
- Uses
UnsafeByteOperations.unsafeWrap() - Avoids memory copying for immutable data
- 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
Is your feature request related to a problem? Please describe
Currently, all source data in search hits is converted using:
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:
BytesArrayinstances - The most commonBytesReferenceimplementation in OpenSearchUnsafeByteOperations.unsafeWrap()- Avoids memory copying for immutable dataRelated component
Search:Performance
Describe alternatives you've considered
No response
Additional context
Expected performance benefits include:
BytesArraysources