On Tue, 24 Mar 2026 11:22:16 GMT, Alan Bateman <[email protected]> wrote:

>> src/java.base/share/classes/java/lang/Thread.java line 1799:
>> 
>>> 1797:         String oldName = this.name;
>>> 1798:         this.name = name;
>>> 1799:         if (!isVirtual() && Thread.currentThread() == this && 
>>> !name.equals(oldName)) {
>> 
>> Surely this slows down normal usage!
>
> I can't imagine this being common. I think part of the issue in this PR is 
> that we are a bit in the dark as to the scenario that throws light on 
> Thread.setName. Hopefully Roman can find out more.

I made an experiment, and found that adding this check (and nothing else) costs 
5-8ns of performance for the normal case (. String comparisons are insanely 
fast nowadays. This cost is drowned in the wins elsewhere in this PR - and 
turns same-name scenario into a factual no-op.

  | Benchmark     | (length) | Baseline (ns/op) | +equals check (ns/op) | 
Change |
  
|---------------|----------|------------------:|----------------------:|-------:|
  | setName       |        1 |    597.4 ±  1.7   |        610.7 ±  7.1   |  
+2.2% |
  | setName       |       16 |    709.4 ±  3.4   |        714.3 ±  7.6   |  
+0.7% |
  | setName       |      200 |    983.2 ±  2.0   |        988.3 ±  2.0   |  
+0.5% |

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/30374#discussion_r2980859636

Reply via email to