Skip to content

Commit

Permalink
prefer instanceOf instead of getClass() (netty#9366)
Browse files Browse the repository at this point in the history
Motivation:

`instanceOf` doesn't perform null check like `getClass()` does. So `instanceOf` may be faster. However, it not true for all cases, as C2 could eliminate these null checks for `getClass()`.

Modification:

Replaced `string.getClass() == AsciiString.class` with `string instanceof AsciiString`.

Proof:

```
@BenchmarkMode(Mode.Throughput)
@fork(value = 1)
@State(Scope.Thread)
@WarmUp(iterations = 5, time = 1, batchSize = 1000)
@measurement(iterations = 10, time = 1, batchSize = 1000)
public class GetClassInstanceOf {

    Object key;

    @setup
    public void setup() {
        key = "123";
    }

    @benchmark
    public boolean getClassEquals() {
        return key.getClass() == String.class;
    }

    @benchmark
    public boolean instanceOf() {
        return key instanceof String;
    }

}
```

```
Benchmark                           Mode  Cnt       Score      Error  Units
GetClassInstanceOf.getClassEquals  thrpt   10  401863.130 ± 3092.568  ops/s
GetClassInstanceOf.instanceOf      thrpt   10  421386.176 ± 4317.328  ops/s
```
  • Loading branch information
doom369 authored and normanmaurer committed Jul 16, 2019
1 parent d07d7e2 commit a82d62a
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions common/src/main/java/io/netty/util/AsciiString.java
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ public AsciiString concat(CharSequence string) {
return this;
}

if (string.getClass() == AsciiString.class) {
if (string instanceof AsciiString) {
AsciiString that = (AsciiString) string;
if (isEmpty()) {
return that;
Expand Down Expand Up @@ -530,7 +530,7 @@ public boolean contentEqualsIgnoreCase(CharSequence string) {
return false;
}

if (string.getClass() == AsciiString.class) {
if (string instanceof AsciiString) {
AsciiString rhs = (AsciiString) string;
for (int i = arrayOffset(), j = rhs.arrayOffset(); i < length(); ++i, ++j) {
if (!equalsIgnoreCase(value[i], rhs.value[j])) {
Expand Down Expand Up @@ -989,7 +989,7 @@ public AsciiString toUpperCase() {
* @return a new string with characters {@code <= \\u0020} removed from the beginning and the end.
*/
public static CharSequence trim(CharSequence c) {
if (c.getClass() == AsciiString.class) {
if (c instanceof AsciiString) {
return ((AsciiString) c).trim();
}
if (c instanceof String) {
Expand Down Expand Up @@ -1044,7 +1044,7 @@ public boolean contentEquals(CharSequence a) {
if (a == null || a.length() != length()) {
return false;
}
if (a.getClass() == AsciiString.class) {
if (a instanceof AsciiString) {
return equals(a);
}

Expand Down Expand Up @@ -1393,7 +1393,7 @@ public boolean equals(CharSequence a, CharSequence b) {
* {@link AsciiString}, just returns the same instance.
*/
public static AsciiString of(CharSequence string) {
return string.getClass() == AsciiString.class ? (AsciiString) string : new AsciiString(string);
return string instanceof AsciiString ? (AsciiString) string : new AsciiString(string);
}

/**
Expand All @@ -1417,7 +1417,7 @@ public static int hashCode(CharSequence value) {
if (value == null) {
return 0;
}
if (value.getClass() == AsciiString.class) {
if (value instanceof AsciiString) {
return value.hashCode();
}

Expand Down Expand Up @@ -1447,10 +1447,10 @@ public static boolean contentEqualsIgnoreCase(CharSequence a, CharSequence b) {
return a == b;
}

if (a.getClass() == AsciiString.class) {
if (a instanceof AsciiString) {
return ((AsciiString) a).contentEqualsIgnoreCase(b);
}
if (b.getClass() == AsciiString.class) {
if (b instanceof AsciiString) {
return ((AsciiString) b).contentEqualsIgnoreCase(a);
}

Expand Down Expand Up @@ -1509,11 +1509,11 @@ public static boolean contentEquals(CharSequence a, CharSequence b) {
return a == b;
}

if (a.getClass() == AsciiString.class) {
if (a instanceof AsciiString) {
return ((AsciiString) a).contentEquals(b);
}

if (b.getClass() == AsciiString.class) {
if (b instanceof AsciiString) {
return ((AsciiString) b).contentEquals(a);
}

Expand Down

0 comments on commit a82d62a

Please sign in to comment.