Skip to content

Use Standard Iterator for TurboFilterList #983

@belugabehr

Description

@belugabehr

Object[] tfa = toArray();
final int len = tfa.length;
for (int i = 0; i < len; i++) {
// for (TurboFilter tf : this) {
final TurboFilter tf = (TurboFilter) tfa[i];
final FilterReply r = tf.decide(marker, logger, level, format, params, t);
if (r == FilterReply.DENY || r == FilterReply.ACCEPT) {
return r;
}
}

At one point (more than 17 years ago!) this used to use the iterator, the current implementation looks suspect in the current world of more advanced JDK JIT. Can you clean this up? If there are larger lists, this also adds additional overheard to the heap as it makes a copy of the array (v.s. one new Iterator that may be optimized away anyway).

I put together a simple JMH benchmark for the two approaches and got the following results:

Benchmark                                    (listSize)  Mode  Cnt   Score   Error  Units
CopyOnWriteIterationBenchmark.usingIterator           5  avgt    9   2.823 ± 0.129  ns/op
CopyOnWriteIterationBenchmark.usingIterator          10  avgt    9   4.390 ± 0.121  ns/op
CopyOnWriteIterationBenchmark.usingIterator          50  avgt    9  15.658 ± 0.749  ns/op
CopyOnWriteIterationBenchmark.usingIterator         100  avgt    9  31.177 ± 0.967  ns/op
CopyOnWriteIterationBenchmark.usingToArray            5  avgt    9   4.278 ± 0.071  ns/op
CopyOnWriteIterationBenchmark.usingToArray           10  avgt    9   5.203 ± 0.312  ns/op
CopyOnWriteIterationBenchmark.usingToArray           50  avgt    9  16.350 ± 0.536  ns/op
CopyOnWriteIterationBenchmark.usingToArray          100  avgt    9  32.647 ± 1.565  ns/op

The listSize is the number of elements in the list.

Note

When I restrict the memory to 128MB, the "usingToArray" test fails with OutOfMemory, thus confirming that the JDK can in-line the iterator and not actually created one on the heap.

# JMH version: 1.37
# VM version: JDK 17.0.16, OpenJDK 64-Bit Server VM, 17.0.16+8-Ubuntu-0ubuntu124.04.1
# VM invoker: /usr/lib/jvm/java-17-openjdk-amd64/bin/java
# VM options: -Xms128M -Xmx128M -XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions