Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
prefer instanceOf instead of getClass() (netty#9366)
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