This test runs popular checksum and digest algorithms that have corresponding compiler intrinsics in HotSpot JVM. These intrinsics do not maintain common frame layout and thus are tricky for stack unwinding.
| 21 | * and thus are tricky for stack unwinding. |
| 22 | */ |
| 23 | public class CodingIntrinsics { |
| 24 | static volatile long sink; |
| 25 | |
| 26 | public static void main(String[] args) { |
| 27 | byte[][] arrays = new byte[1025][]; |
| 28 | |
| 29 | Random random = new Random(123); |
| 30 | for (int i = 0; i < arrays.length; i++) { |
| 31 | arrays[i] = new byte[i]; |
| 32 | random.nextBytes(arrays[i]); |
| 33 | } |
| 34 | |
| 35 | for (int i = 0; i < arrays.length; i++) { |
| 36 | runTest(arrays[i]); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | static void runTest(byte[] input) { |
| 41 | for (Codec codec : CODECS) { |
| 42 | sink = runTestWithCodec(codec, input, 10_000); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | static long runTestWithCodec(Codec codec, byte[] input, int count) { |
| 47 | long n = 0; |
| 48 | for (int i = 0; i < count; i++) { |
| 49 | byte[] output = codec.encode(input); |
| 50 | n += output.length; |
| 51 | if (output.length <= 16) { |
| 52 | n += Arrays.hashCode(output); |
| 53 | } |
| 54 | } |
| 55 | return n; |
| 56 | } |
| 57 | |
| 58 | interface Codec { |
| 59 | byte[] encode(byte[] input); |
| 60 | } |
| 61 | |
| 62 | static final Codec[] CODECS = new Codec[]{ |
| 63 | input -> Base64.getEncoder().encode(input), |
| 64 | input -> checksum(input, new CRC32()), |
| 65 | input -> checksum(input, new Adler32()), |
| 66 | input -> digest(input, "MD5"), |
| 67 | input -> digest(input, "SHA-1"), |
| 68 | // async-profiler cannot easily unwind sha256_implCompress intrinsic |
| 69 | // on x86 machines that support AVX2 but not SHA instruction set. |
| 70 | isArm64() ? input -> digest(input, "SHA-256") : input -> input |
| 71 | }; |
| 72 | |
| 73 | static boolean isArm64() { |
| 74 | String arch = System.getProperty("os.arch").toLowerCase(); |
| 75 | return arch.equals("aarch64") || arch.contains("arm64"); |
| 76 | } |
| 77 | |
| 78 | static byte[] checksum(byte[] input, Checksum checksum) { |
| 79 | checksum.update(input, 0, input.length); |
| 80 | long value = checksum.getValue(); |