Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use thread local state for high compressor #47

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/java/net/jpountz/lz4/LZ4HCJNICompressor.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,18 @@ final class LZ4HCJNICompressor extends LZ4Compressor {

public static final LZ4Compressor INSTANCE = new LZ4HCJNICompressor();

private static ThreadLocal<byte[]> state = new ThreadLocal() {
@Override
protected byte[] initialValue() {
return new byte[LZ4JNI.LZ4_sizeofStateHC()];
}
};

@Override
public int compress(byte[] src, int srcOff, int srcLen, byte[] dest, int destOff, int maxDestLen) {
checkRange(src, srcOff, srcLen);
checkRange(dest, destOff, maxDestLen);
final int result = LZ4JNI.LZ4_compressHC(src, srcOff, srcLen, dest, destOff, maxDestLen);
final int result = LZ4JNI.LZ4_compressHC_withStateHC(state.get(), src, srcOff, srcLen, dest, destOff, maxDestLen);
if (result <= 0) {
throw new LZ4Exception();
}
Expand Down
3 changes: 2 additions & 1 deletion src/java/net/jpountz/lz4/LZ4JNI.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ enum LZ4JNI {

static native void init();
static native int LZ4_compress_limitedOutput(byte[] src, int srcOff, int srcLen, byte[] dest, int destOff, int maxDestLen);
static native int LZ4_compressHC(byte[] src, int srcOff, int srcLen, byte[] dest, int destOff, int maxDestLen);
static native int LZ4_sizeofStateHC();
static native int LZ4_compressHC_withStateHC(byte[] state, byte[] src, int srcOff, int srcLen, byte[] dest, int destOff, int maxDestLen);
static native int LZ4_decompress_fast(byte[] src, int srcOff, byte[] dest, int destOff, int destLen);
static native int LZ4_decompress_fast_withPrefix64k(byte[] src, int srcOff, byte[] dest, int destOff, int destLen);
static native int LZ4_decompress_safe(byte[] src, int srcOff, int srcLen, byte[] dest, int destOff, int maxDestLen);
Expand Down
31 changes: 25 additions & 6 deletions src/jni/net_jpountz_lz4_LZ4JNI.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/

#include "lz4.h"
#include "lz4hc.h"
#include "net_jpountz_lz4_LZ4JNI.h"

static jclass OutOfMemoryError;
Expand Down Expand Up @@ -64,17 +65,24 @@ JNIEXPORT jint JNICALL Java_net_jpountz_lz4_LZ4JNI_LZ4_1compress_1limitedOutput
}

/*
* Class: net_jpountz_lz4_LZ4
* Method: LZ4_compressHC
* Signature: ([BII[BI)I
* Class: net_jpountz_lz4_LZ4JNI
* Method: LZ4_compressHC_withStateHC
* Signature: ([B[BII[BII)I
*/
JNIEXPORT jint JNICALL Java_net_jpountz_lz4_LZ4JNI_LZ4_1compressHC
(JNIEnv *env, jclass cls, jbyteArray src, jint srcOff, jint srcLen, jbyteArray dest, jint destOff, jint maxDestLen) {
JNIEXPORT jint JNICALL Java_net_jpountz_lz4_LZ4JNI_LZ4_1compressHC_1withStateHC
(JNIEnv *env, jclass cls, jbyteArray state, jbyteArray src, jint srcOff, jint srcLen, jbyteArray dest, jint destOff, jint maxDestLen) {

char* stateHC;
char* in;
char* out;
jint compressed;

stateHC = (char*) (*env)->GetPrimitiveArrayCritical(env, state, 0);
if (stateHC == NULL) {
throw_OOM(env);
return 0;
}

in = (char*) (*env)->GetPrimitiveArrayCritical(env, src, 0);
if (in == NULL) {
throw_OOM(env);
Expand All @@ -86,15 +94,26 @@ JNIEXPORT jint JNICALL Java_net_jpountz_lz4_LZ4JNI_LZ4_1compressHC
return 0;
}

compressed = LZ4_compressHC_limitedOutput(in + srcOff, out + destOff, srcLen, maxDestLen);
compressed = LZ4_compressHC_limitedOutput_withStateHC(stateHC, in + srcOff, out + destOff, srcLen, maxDestLen);

(*env)->ReleasePrimitiveArrayCritical(env, state, stateHC, 0);
(*env)->ReleasePrimitiveArrayCritical(env, src, in, 0);
(*env)->ReleasePrimitiveArrayCritical(env, dest, out, 0);

return compressed;

}

/*
* Class: net_jpountz_lz4_LZ4JNI
* Method: LZ4_sizeofStateHC
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_net_jpountz_lz4_LZ4JNI_LZ4_1sizeofStateHC
(JNIEnv *env, jclass cls) {
return LZ4_sizeofStateHC();
}

/*
* Class: net_jpountz_lz4_LZ4
* Method: LZ4_decompress
Expand Down