RFR: 8356993: ArrayDeque should use Arrays.fill() instead of for() loops

Archie Cobbs acobbs at openjdk.org
Thu May 15 17:48:50 UTC 2025


On Wed, 14 May 2025 19:37:37 GMT, Archie Cobbs <acobbs at openjdk.org> wrote:

> Please review this small performance tweak `ArrayDeque`.
> 
> `ArrayDeque` has an invariant in which any unused elements in the array must be null. In a couple of places, the code is setting contiguous ranges of elements to null using `for()` loops. This can be both simplified and sped up by using `Arrays.fill()` instead.

I added a benchmark to the PR (hopefully I did that right).

It shows a _decrease_ in performance. I have no idea why. I did this on my laptop so who knows, but if the effect is real then it kind of raises a lot of larger questions.

jdk-25+22-94-g0318e49500e (master):
Benchmark                                       Mode  Cnt   Score   Error  Units
ArrayDeque.ClearBenchmarkTestJMH.fillAndClear  thrpt   50  37.064 ± 0.225  ops/s

jdk-25+22-95-g84fb0903be0 (JDK-8356993):
Benchmark                                       Mode  Cnt   Score   Error  Units
ArrayDeque.ClearBenchmarkTestJMH.fillAndClear  thrpt   50  35.528 ± 0.180  ops/s

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

PR Comment: https://git.openjdk.org/jdk/pull/25237#issuecomment-2884609647


More information about the core-libs-dev mailing list