diff --git a/src/java.base/share/classes/java/util/ArrayDeque.java b/src/java.base/share/classes/java/util/ArrayDeque.java index 60bb3ed4a4572..113e361ec10c6 100644 --- a/src/java.base/share/classes/java/util/ArrayDeque.java +++ b/src/java.base/share/classes/java/util/ArrayDeque.java @@ -151,8 +151,7 @@ private void grow(int needed) { System.arraycopy(es, head, es, head + newSpace, oldCapacity - head); - for (int i = head, to = (head += newSpace); i < to; i++) - es[i] = null; + Arrays.fill(es, head, head += newSpace, null); } } @@ -1046,7 +1045,7 @@ private static void circularClear(Object[] es, int i, int end) { // assert 0 <= end && end < es.length; for (int to = (i <= end) ? end : es.length; ; i = 0, to = end) { - for (; i < to; i++) es[i] = null; + Arrays.fill(es, i, to, null); if (to == end) break; } } diff --git a/test/jdk/java/util/ArrayDeque/ClearBenchmarkTestJMH.java b/test/jdk/java/util/ArrayDeque/ClearBenchmarkTestJMH.java new file mode 100644 index 0000000000000..c02b976b1361d --- /dev/null +++ b/test/jdk/java/util/ArrayDeque/ClearBenchmarkTestJMH.java @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package org.openjdk.bench.java.util.ArrayDeque; + +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.TearDown; +import org.openjdk.jmh.annotations.Warmup; +import org.openjdk.jmh.infra.Blackhole; + +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Collections; +import java.util.concurrent.TimeUnit; + +@BenchmarkMode(Mode.Throughput) +@OutputTimeUnit(TimeUnit.SECONDS) +@State(Scope.Thread) +public class ClearBenchmarkTestJMH { + + private static final int LIST_SIZE = 10_000_000; + + private Object[] array; + + @Setup + public void setUp() { + array = new Object[LIST_SIZE]; + for (int i = 0; i < LIST_SIZE; i++) + array[i] = new Object(); + } + + @Benchmark + @Measurement(iterations = 10) + @Warmup(iterations = 3) + public void fillAndClear(Blackhole bh) { + ArrayDeque a = new ArrayDeque<>(LIST_SIZE); + Collections.addAll(a, array); + a.clear(); + bh.consume(a); + } +}