lz4 / lz4-java

LZ4 compression for Java
Apache License 2.0
1.11k stars 252 forks source link

Idea to improve performance by eliminating one copying step #69

Open mtopolnik opened 9 years ago

mtopolnik commented 9 years ago

I'm going over your code while considering to use it for a Hazelcast enterprise feature. In the same effort I have done some I/O coding similar to the write(byte[], int, int) method of LZ4BlockOutputStream. The way I wrote it is not too complex, but avoids repeatedly copying into the internal buffer whenever the supplied buffer is larger than it. Basically,

void write(byte[] b, int off, int len) {
    while (len >= BUFFER_SIZE) {
        flushLocalBuffer();
        compress(b, off, BUFFER_SIZE);
        off += BUFFER_SIZE;
        len -= BUFFER_SIZE;
    }
    while (len > 0) {
        final int transferredCount = Math.min(BUFFER_SIZE - position, len);
        System.arraycopy(b, off, buf, position, transferredCount);
        off += transferredCount;
        len -= transferredCount;
        position += transferredCount;
        ensureBufHasRoom();
    }
}

Since performance is a high concern in this project, perhaps it is of value to bring up this detail.

odaira commented 7 years ago

Thanks, I think this is a neat idea. As I would like to release the next version as soon as possible, let me revisit this after the next release. I would like to see if it will cause any visible performance difference.