Prefer instanceOf instead of getClass() in AsciiString#9366
Prefer instanceOf instead of getClass() in AsciiString#9366normanmaurer merged 1 commit intonetty:4.1from doom369:prefer_instanceOF
Conversation
|
Can one of the admins verify this patch? |
|
AFAIK https://gist.github.com/apangin/7a9b7062a4bd0cd41fcc getClass is already a JVM intrinsic: can you please check why it is supposed to be faster? |
|
@franz1981 as AsciiString is Final the semantics are the Same |
|
@normanmaurer haven't considered it, you're right...that make me more curious to know why getClass is kinda slower...probably it is really due to null check.. |
|
So it is probably not only null check but also class compare check. |
|
Only ASM check could tell us the truth: as long as the methods got hot, I believe they will be replaced by their ASM replacements (4 the intrinsics) and the C2 compiled version for the rest of the code, so I won't be worried too much by the bytecode instructions... |
|
@franz1981 they will and they are. You can check that by warmup phase that is 30% slower. However, still |
|
@netty-bot test this please |
|
@doom369 looks good generally but how come you used
|
@njhill no changes. |
|
@njhill @franz1981 look like this is null check after all. |
|
@njhill @franz1981 so what you think? Even if it not shows a perf win I kind of think we should pull it in just for the sake of consistency |
|
@normanmaurer I will push this but will avoid on it to refer to any perf improvement to save next PRs to rely on the same (inaccurate) assumption |
|
@franz1981 sounds good |
|
@doom369 can you adjust the commit message ? |
|
@normanmaurer you mean edit text in PR text? |
|
@doom369 yes please (and also the commit message by amend it). |
|
I fixed the PR message. Regarding commit message you can change it during PR merge. (I still don't understand what should I change in it anyway) |
|
@doom369 thanks a lot! |
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 ```
Motivation:
instanceOfdoesn't perform null check likegetClass()does. SoinstanceOfmay be faster. However, it not true for all cases, as C2 could eliminate these null checks forgetClass().Modification:
Replaced
string.getClass() == AsciiString.classwithstring instanceof AsciiString.Proof: