mirror of
https://github.com/PaperMC/Velocity.git
synced 2026-02-17 14:37:43 +01:00
libdeflate is significantly faster than vanilla zlib, zlib-ng, and Cloudflare zlib. It is also MIT-licensed (so no licensing concerns). In addition, it simplifies a lot of the native code (something that's been tricky to get right). While we're at it, I have also taken the time to fine-time compression in Velocity in general. Thanks to this work, native compression only requires one JNI call, an improvement from the more than 2 (sometimes up to 5) that were possible before. This optimization also extends to the existing Java compressors, though they require potentially two JNI calls.
44 lines
1.3 KiB
C
44 lines
1.3 KiB
C
#include <assert.h>
|
|
#include <jni.h>
|
|
#include <stdbool.h>
|
|
#include <stdlib.h>
|
|
#include <libdeflate.h>
|
|
#include "jni_util.h"
|
|
|
|
JNIEXPORT jlong JNICALL
|
|
Java_com_velocitypowered_natives_compression_NativeZlibDeflate_init(JNIEnv *env,
|
|
jobject obj,
|
|
jint level)
|
|
{
|
|
struct libdeflate_compressor *compressor = libdeflate_alloc_compressor(level);
|
|
if (compressor == NULL) {
|
|
// Out of memory!
|
|
throwException(env, "java/lang/OutOfMemoryError", "libdeflate allocate compressor");
|
|
return 0;
|
|
}
|
|
return (jlong) compressor;
|
|
}
|
|
|
|
JNIEXPORT void JNICALL
|
|
Java_com_velocitypowered_natives_compression_NativeZlibDeflate_free(JNIEnv *env,
|
|
jobject obj,
|
|
jlong ctx)
|
|
{
|
|
libdeflate_free_compressor((struct libdeflate_compressor *) ctx);
|
|
}
|
|
|
|
JNIEXPORT jboolean JNICALL
|
|
Java_com_velocitypowered_natives_compression_NativeZlibDeflate_process(JNIEnv *env,
|
|
jobject obj,
|
|
jlong ctx,
|
|
jlong sourceAddress,
|
|
jint sourceLength,
|
|
jlong destinationAddress,
|
|
jint destinationLength,
|
|
jboolean finish)
|
|
{
|
|
struct libdeflate_compressor *compressor = (struct libdeflate_compressor *) ctx;
|
|
size_t produced = libdeflate_zlib_compress(compressor, (void *) sourceAddress, sourceLength,
|
|
(void *) destinationAddress, destinationLength);
|
|
return (jlong) produced;
|
|
} |