| It is definitely the copy of the corresponding Set. The unit test below shows the results:
| WeldTest.java |
import java.util.Collections; |
import java.util.Set; |
|
import org.apache.commons.lang3.RandomStringUtils; |
import org.jboss.weld.bean.builtin.BeanManagerProxy; |
import org.jboss.weld.environment.se.Weld; |
import org.jboss.weld.environment.se.WeldContainer; |
import org.jboss.weld.util.collections.ImmutableHashSet; |
import org.jboss.weld.util.collections.ImmutableSet; |
import org.junit.Before; |
import org.junit.Test; |
|
import com.google.common.base.Stopwatch; |
|
import javassist.CannotCompileException; |
import javassist.ClassPool; |
|
public class WeldTest { |
|
private static final int CLASS_COUNT = 2000; |
private Class<?>[] classes = new Class<?>[CLASS_COUNT]; |
|
@Before |
public void before() throws CannotCompileException, RuntimeException { |
final ClassPool pool = ClassPool.getDefault(); |
for (int i = 0; i < CLASS_COUNT; i++) { |
classes[i] = pool.makeClass(RandomStringUtils.randomAlphabetic(10)).toClass(); |
} |
} |
|
@Test |
public void testImmutableSet() { |
for (int j = 0; j < 10; j++) { |
final Set<Class<?>> immutableSet = ImmutableHashSet.of(classes); |
final Stopwatch stopwatch = Stopwatch.createStarted(); |
for (int i = 0; i < 1000; i++) { |
ImmutableSet.copyOf(immutableSet); |
} |
System.out.println("immutableSet " + stopwatch); |
stopwatch.reset(); |
final Set<Class<?>> unmodifiableSet = Collections.unmodifiableSet(immutableSet); |
stopwatch.start(); |
for (int i = 0; i < 1000; i++) { |
ImmutableSet.copyOf(unmodifiableSet); |
} |
System.out.println("unmodifiableSet " + stopwatch); |
} |
} |
}
|
Results: immutableSet 200.0 μs unmodifiableSet 230.3 ms immutableSet 33.70 μs unmodifiableSet 153.8 ms immutableSet 36.34 μs unmodifiableSet 147.8 ms immutableSet 27.80 μs unmodifiableSet 140.9 ms immutableSet 27.86 μs unmodifiableSet 150.0 ms immutableSet 28.12 μs unmodifiableSet 188.2 ms immutableSet 28.67 μs unmodifiableSet 134.9 ms immutableSet 44.41 μs unmodifiableSet 140.4 ms immutableSet 27.62 μs unmodifiableSet 136.0 ms immutableSet 36.56 μs unmodifiableSet 138.1 ms Additionally the org.jboss.weld.util.collections.ImmutableSet#copyOf(Collection ...) is doing a fallback to the builder.addAll iterable. This prevents the implementation to instantiate the new copied set with a size parameter and therefore multiple resize operations are needed during adding. |