From 9793459774fcdc4e0d69c05349a6e9a46edc6de8 Mon Sep 17 00:00:00 2001 From: Vladimir Sitnikov Date: Wed, 14 Jun 2023 13:31:46 +0300 Subject: [PATCH] perf: add PropertyIteratorImpl.EMPTY_ITERATOR when iterating over an empty collection --- .../org/apache/jmeter/testelement/AbstractTestElement.java | 3 +++ .../apache/jmeter/testelement/property/AbstractProperty.java | 3 +++ .../jmeter/testelement/property/PropertyIteratorImpl.java | 2 ++ 3 files changed, 8 insertions(+) diff --git a/src/core/src/main/java/org/apache/jmeter/testelement/AbstractTestElement.java b/src/core/src/main/java/org/apache/jmeter/testelement/AbstractTestElement.java index 6bbf3bad48d..2361b26fead 100644 --- a/src/core/src/main/java/org/apache/jmeter/testelement/AbstractTestElement.java +++ b/src/core/src/main/java/org/apache/jmeter/testelement/AbstractTestElement.java @@ -589,6 +589,9 @@ public PropertyIterator propertyIterator() { // Note: can't use ConcurrentMap here as it would return elements in unpredictable order readLock(); try { + if (propMap.isEmpty()) { + return PropertyIteratorImpl.EMPTY_ITERATOR; + } // TODO: copy the contents of the iterator to avoid ConcurrentModificationException? return new PropertyIteratorImpl(this, propMap.values()); } finally { diff --git a/src/core/src/main/java/org/apache/jmeter/testelement/property/AbstractProperty.java b/src/core/src/main/java/org/apache/jmeter/testelement/property/AbstractProperty.java index 37fc659d817..0b0ff4ef6c4 100644 --- a/src/core/src/main/java/org/apache/jmeter/testelement/property/AbstractProperty.java +++ b/src/core/src/main/java/org/apache/jmeter/testelement/property/AbstractProperty.java @@ -77,6 +77,9 @@ public void setRunningVersion(boolean runningVersion) { } protected PropertyIterator getIterator(Collection values) { + if (values.isEmpty()) { + return PropertyIteratorImpl.EMPTY_ITERATOR; + } return new PropertyIteratorImpl(values); } diff --git a/src/core/src/main/java/org/apache/jmeter/testelement/property/PropertyIteratorImpl.java b/src/core/src/main/java/org/apache/jmeter/testelement/property/PropertyIteratorImpl.java index 6f54e598b9d..ca043f5f9b3 100644 --- a/src/core/src/main/java/org/apache/jmeter/testelement/property/PropertyIteratorImpl.java +++ b/src/core/src/main/java/org/apache/jmeter/testelement/property/PropertyIteratorImpl.java @@ -18,11 +18,13 @@ package org.apache.jmeter.testelement.property; import java.util.Collection; +import java.util.Collections; import java.util.Iterator; import org.apache.jmeter.testelement.TestElement; public class PropertyIteratorImpl implements PropertyIterator { + public static final PropertyIterator EMPTY_ITERATOR = new PropertyIteratorImpl(Collections.emptyList()); private final TestElement owner; private final Iterator iter;