Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MINOR: Unnecessary changeCapacity in ImplicitLinkedHashCollection #17893

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ public final boolean add(E newElement) {
if (newElement.prev() != INVALID_INDEX || newElement.next() != INVALID_INDEX) {
return false;
}
if ((size + 1) >= elements.length / 2) {
if (size >= elements.length / 2) {
changeCapacity(calculateCapacity(elements.length));
}
int slot = addInternal(newElement, elements);
Expand Down Expand Up @@ -443,9 +443,7 @@ private void changeCapacity(int newCapacity) {
Element[] newElements = new Element[newCapacity];
HeadElement newHead = new HeadElement();
int oldSize = size;
for (Iterator<E> iter = iterator(); iter.hasNext(); ) {
Element element = iter.next();
iter.remove();
for (Element element : this) {
int newSlot = addInternal(element, newElements);
addToListTail(newHead, newElements, newSlot);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,13 +475,17 @@ public void testCollisions() {

@Test
public void testEnlargement() {
ImplicitLinkedHashCollection<TestElement> coll = new ImplicitLinkedHashCollection<>(5);
int expectedNumElements = 5;
ImplicitLinkedHashCollection<TestElement> coll = new ImplicitLinkedHashCollection<>(expectedNumElements);
assertEquals(11, coll.numSlots());
for (int i = 0; i < 6; i++) {
for (int i = 0; i < expectedNumElements; i++) {
assertTrue(coll.add(new TestElement(i)));
}
assertEquals(11, coll.numSlots());
assertEquals(expectedNumElements, coll.size());
assertTrue(coll.add(new TestElement(expectedNumElements)));
assertEquals(23, coll.numSlots());
assertEquals(6, coll.size());
assertEquals(expectedNumElements + 1, coll.size());
expectTraversal(coll.iterator(), 0, 1, 2, 3, 4, 5);
for (int i = 0; i < 6; i++) {
assertTrue(coll.contains(new TestElement(i)), "Failed to find element " + i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.TimeUnit;

@State(Scope.Benchmark)
Expand Down Expand Up @@ -102,12 +105,15 @@ public int compare(TestElement a, TestElement b) {
private int size;

private ImplicitLinkedHashCollection<TestElement> coll;
private List<TestElement> elements;

@Setup(Level.Trial)
public void setup() {
coll = new ImplicitLinkedHashCollection<>();
elements = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
coll.add(new TestElement(Uuid.randomUuid().toString()));
elements.add(new TestElement("TestElement-" + i));
}
}

Expand All @@ -119,4 +125,26 @@ public ImplicitLinkedHashCollection<TestElement> testCollectionSort() {
coll.sort(TestElementComparator.INSTANCE);
return coll;
}

@Benchmark
public void testCreateFromExpectedNumElements(Blackhole blackhole) {
ImplicitLinkedHashCollection<TestElement> sets = new ImplicitLinkedHashCollection<>(elements.size());
for (TestElement element : elements) {
element.setPrev(ImplicitLinkedHashCollection.INVALID_INDEX);
element.setNext(ImplicitLinkedHashCollection.INVALID_INDEX);
sets.mustAdd(element);
}
blackhole.consume(sets);
}

@Benchmark
public void testCreateFromEmpty(Blackhole blackhole) {
ImplicitLinkedHashCollection<TestElement> sets = new ImplicitLinkedHashCollection<>();
for (TestElement element : elements) {
element.setPrev(ImplicitLinkedHashCollection.INVALID_INDEX);
element.setNext(ImplicitLinkedHashCollection.INVALID_INDEX);
sets.mustAdd(element);
}
blackhole.consume(sets);
}
}