Skip to content

Commit

Permalink
Save CRC32C Netty allocation to trigger ADLER32 and CRC32 reflective …
Browse files Browse the repository at this point in the history
…initialization (#13836)

Motivation:

Allocating a new CRC32C instance cause ByteBufChecksum static init to
kick-in, performing reflective lookups of Adler32 and Crc32 OpenJDK
methods, although not needed

Modification:

Place each of the reflective methods in their own class holders, to let
them kick-in only if directly accessed

Result:

Recheability native image analysis and JIT CRC32C usage won't trigger
initializing unused classes or native libraries
  • Loading branch information
franz1981 committed Feb 10, 2024
1 parent b135e37 commit 442165e
Showing 1 changed file with 18 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,20 @@
* byte array ({@link ByteBuf#hasArray()} is {@code true}) or not.
*/
abstract class ByteBufChecksum implements Checksum {
private static final Method ADLER32_UPDATE_METHOD;
private static final Method CRC32_UPDATE_METHOD;

static {
// See if we can use fast-path when using ByteBuf that is not heap based as Adler32 and CRC32 added support
// for update(ByteBuffer) in JDK8.
ADLER32_UPDATE_METHOD = updateByteBuffer(new Adler32());
CRC32_UPDATE_METHOD = updateByteBuffer(new CRC32());

/**
* on OpenJDK Adler32 and CRC32 both calls ZipUtils.loadLibrary on class init.
*/
private static class ZlibChecksumMethods {
private static final Method ADLER32_UPDATE_METHOD;
private static final Method CRC32_UPDATE_METHOD;

static {
// See if we can use fast-path when using ByteBuf that is not heap based as Adler32 and CRC32 added support
// for update(ByteBuffer) in JDK8.
ADLER32_UPDATE_METHOD = updateByteBuffer(new Adler32());
CRC32_UPDATE_METHOD = updateByteBuffer(new CRC32());
}
}

private final ByteProcessor updateProcessor = new ByteProcessor() {
Expand Down Expand Up @@ -69,11 +75,11 @@ static ByteBufChecksum wrapChecksum(Checksum checksum) {
if (checksum instanceof ByteBufChecksum) {
return (ByteBufChecksum) checksum;
}
if (checksum instanceof Adler32 && ADLER32_UPDATE_METHOD != null) {
return new ReflectiveByteBufChecksum(checksum, ADLER32_UPDATE_METHOD);
if (checksum instanceof Adler32 && ZlibChecksumMethods.ADLER32_UPDATE_METHOD != null) {
return new ReflectiveByteBufChecksum(checksum, ZlibChecksumMethods.ADLER32_UPDATE_METHOD);
}
if (checksum instanceof CRC32 && CRC32_UPDATE_METHOD != null) {
return new ReflectiveByteBufChecksum(checksum, CRC32_UPDATE_METHOD);
if (checksum instanceof CRC32 && ZlibChecksumMethods.CRC32_UPDATE_METHOD != null) {
return new ReflectiveByteBufChecksum(checksum, ZlibChecksumMethods.CRC32_UPDATE_METHOD);
}
return new SlowByteBufChecksum(checksum);
}
Expand Down

0 comments on commit 442165e

Please sign in to comment.