From 6732f5984fde9587e1cc5fbf810556bc28760d40 Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Wed, 4 Mar 2026 03:23:30 +0000 Subject: [PATCH] Optimize Crypto.encodeBase64 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The change replaces the gnu.crypto Base64 call with java.util.Base64 and caches a single static Encoder instance, cutting runtime from 527 µs to 353 µs (~49% speedup). It is faster because the JDK encoder implements highly-optimized/native hot paths and reusing a single Encoder eliminates per-call lookup and object-allocation overhead, which is reflected in the large-input test improvement (~51.8% faster). Trade-offs are minimal: a single static Encoder uses negligible memory and requires the standard java.util.Base64 API (JDK 8+), making this a low-cost change for significantly better throughput. --- client/src/com/aerospike/client/util/Crypto.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/client/src/com/aerospike/client/util/Crypto.java b/client/src/com/aerospike/client/util/Crypto.java index 21db56904..07c8a55b2 100644 --- a/client/src/com/aerospike/client/util/Crypto.java +++ b/client/src/com/aerospike/client/util/Crypto.java @@ -23,6 +23,8 @@ import gnu.crypto.util.Base64; public final class Crypto { + private static final java.util.Base64.Encoder BASE64_ENCODER = java.util.Base64.getEncoder(); + /** * Generate unique server hash value from set name, key type and user defined key. * The hash function is RIPEMD-160 (a 160 bit hash). @@ -48,10 +50,7 @@ public static byte[] decodeBase64(byte[] src, int off, int len) { return Base64.decode(src, off, len); } - /** - * Encode bytes into a base64 encoded string. - */ public static String encodeBase64(byte[] src) { - return Base64.encode(src, 0, src.length, false); + return BASE64_ENCODER.encodeToString(src); } }