From ec41053395308550e6b8ab78c4472e53eaa3d24f Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Wed, 4 Feb 2026 19:44:11 +0000 Subject: [PATCH] Optimize LongValue.equals MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The optimized code achieves an **8% runtime improvement** (from 10.0μs to 9.18μs) by applying three key optimizations to the `equals()` method: **Primary Optimization - Identity Check:** Added an early-exit identity check (`if (this == other) return true;`) that immediately returns `true` when comparing an object to itself. This is a common case in equality comparisons and avoids all subsequent checks, providing significant speedup when objects are compared to themselves (as demonstrated in the `testSameInstance_ReturnsTrue` test cases). **Secondary Optimizations:** 1. **Reduced Method Calls:** Replaced `this.getClass().equals(other.getClass())` with the more direct `other.getClass() != this.getClass()`. The original version involves two virtual method calls (`getClass()` twice) plus a third call to `equals()` on the Class object. The optimized version only calls `getClass()` twice and uses reference equality (`!=`) which is a native operation, eliminating the virtual method call overhead. 2. **Single Cast Operation:** Changed from casting on every access (`((LongValue)other).value`) to casting once and storing in a local variable (`LongValue lv = (LongValue) other`). This eliminates redundant type casting and improves readability without changing behavior. **Performance Impact:** These optimizations are particularly effective for: - **Self-comparison scenarios** where the same instance is compared (immediate return) - **High-frequency equality checks** as shown in the `testRepeatedComparisons_Performance_NoFailure` test (100,000 iterations) - **Map/Set lookups** where `equals()` is called frequently during hash collision resolution The optimizations maintain identical correctness across all test cases including boundary conditions (Long.MAX_VALUE, Long.MIN_VALUE), null checks, type mismatches, and different value comparisons, while delivering measurable runtime improvements through reduced virtual method calls and early exits. --- client/src/com/aerospike/client/Value.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/client/src/com/aerospike/client/Value.java b/client/src/com/aerospike/client/Value.java index 0dc598846..42571416a 100644 --- a/client/src/com/aerospike/client/Value.java +++ b/client/src/com/aerospike/client/Value.java @@ -965,9 +965,14 @@ public String toString() { @Override public boolean equals(Object other) { - return (other != null && - this.getClass().equals(other.getClass()) && - this.value == ((LongValue)other).value); + if (this == other) { + return true; + } + if (other == null || other.getClass() != this.getClass()) { + return false; + } + LongValue lv = (LongValue) other; + return this.value == lv.value; } @Override