paimon icon indicating copy to clipboard operation
paimon copied to clipboard

[enhancement ] Reusing the lengths array in ArrowFieldWrites$ArrayWriter's doWrite method

Open haohao0103 opened this issue 9 months ago • 0 comments

Search before asking

  • [x] I searched in the issues and found nothing similar.

Motivation

ArrowFieldWriters$ArrayWriter#doWrite

Image

Solution

Implementation Steps

  1. Reusable Buffer Declaration
    Define a reusable buffer field in ArrayWriter class:

    private int[] reusedLengthsBuffer;  // Reusable array buffer for length storage
    
  2. Lazy Initialization & Dynamic Expansion
    In doWrite method, initialize or expand buffer based on runtime lenSize requirements:

    if (reusedLengthsBuffer == null || reusedLengthsBuffer.length < lenSize) {
        int newCapacity = lenSize;
        // Apply 1.5x growth factor if reallocating from existing buffer
        if (reusedLengthsBuffer != null) {
            newCapacity = Math.max(lenSize, (int)(reusedLengthsBuffer.length * 1.5)); 
        }
        reusedLengthsBuffer = new int[newCapacity];  // Allocate or expand buffer
    }
    
    // Reset buffer segment before reuse
    Arrays.fill(reusedLengthsBuffer, 0, lenSize, 0);  
    

Let me know if you have any questions. thanks

Anything else?

No response

Are you willing to submit a PR?

  • [x] I'm willing to submit a PR!

haohao0103 avatar Apr 03 '25 08:04 haohao0103