Add fast path in ByteBufUtil.compare and ByteBufUtil.equals methods (#11296)
Motivation: When object-references are both same, the method should return 0 directly with no necessary go loop&compare the content of the ByteBuf. Modification: Added short circuit when both object-references are the same for equals and compare methods. Result: Added short circuit code.
This commit is contained in:
parent
8aa371bd45
commit
078cdd2597
@ -1274,7 +1274,7 @@ public abstract class AbstractByteBuf extends ByteBuf {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
return this == o || (o instanceof ByteBuf && ByteBufUtil.equals(this, (ByteBuf) o));
|
return o instanceof ByteBuf && ByteBufUtil.equals(this, (ByteBuf) o);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -298,6 +298,9 @@ public final class ByteBufUtil {
|
|||||||
* This method is useful when implementing a new buffer type.
|
* This method is useful when implementing a new buffer type.
|
||||||
*/
|
*/
|
||||||
public static boolean equals(ByteBuf bufferA, ByteBuf bufferB) {
|
public static boolean equals(ByteBuf bufferA, ByteBuf bufferB) {
|
||||||
|
if (bufferA == bufferB) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
final int aLen = bufferA.readableBytes();
|
final int aLen = bufferA.readableBytes();
|
||||||
if (aLen != bufferB.readableBytes()) {
|
if (aLen != bufferB.readableBytes()) {
|
||||||
return false;
|
return false;
|
||||||
@ -310,6 +313,9 @@ public final class ByteBufUtil {
|
|||||||
* This method is useful when implementing a new buffer type.
|
* This method is useful when implementing a new buffer type.
|
||||||
*/
|
*/
|
||||||
public static int compare(ByteBuf bufferA, ByteBuf bufferB) {
|
public static int compare(ByteBuf bufferA, ByteBuf bufferB) {
|
||||||
|
if (bufferA == bufferB) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
final int aLen = bufferA.readableBytes();
|
final int aLen = bufferA.readableBytes();
|
||||||
final int bLen = bufferB.readableBytes();
|
final int bLen = bufferB.readableBytes();
|
||||||
final int minLength = Math.min(aLen, bLen);
|
final int minLength = Math.min(aLen, bLen);
|
||||||
|
@ -1027,9 +1027,6 @@ public class SwappedByteBuf extends ByteBuf {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
if (this == obj) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (obj instanceof ByteBuf) {
|
if (obj instanceof ByteBuf) {
|
||||||
return ByteBufUtil.equals(this, (ByteBuf) obj);
|
return ByteBufUtil.equals(this, (ByteBuf) obj);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user