huginn created HBASE-30391:
------------------------------

             Summary: Reduce array copying when expanding ByteArrayOutputStream
                 Key: HBASE-30391
                 URL: https://issues.apache.org/jira/browse/HBASE-30391
             Project: HBase
          Issue Type: Improvement
          Components: HFile
    Affects Versions: 2.4.11
            Reporter: huginn


## What happens

The custom ByteArrayOutputStream copies the entire backing array when it 
expands, including unused capacity.

## When it happens

This occurs when a write exceeds the current buffer capacity and the stream 
allocates a larger backing array.

## Impact

The unnecessary copy increases CPU work and temporary heap memory traffic, 
especially for buffers whose logical content is much smaller than their 
capacity.

## Root cause

In ByteArrayOutputStream.checkSizeAndGrow, the expansion path copies buf.length 
bytes even though only pos bytes contain valid stream data.

## Proposed fix

Copy only the bytes already written to the stream (pos) when expanding the 
backing array. This preserves the stream contents while avoiding copies of 
unused capacity.

## Reproduction

The behavior can be observed by writing data in increments that trigger buffer 
expansion and profiling the resulting array-copy work. Testing evidence will be 
added by the reporter.{color:red}着色文本{color}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to