-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Replace AtomicInteger with int in TestTemplateTestDescriptor.execute() #725
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will merge the branch issues/14-jupiter-params
into master
soon. In commit d933781, I've changed the implementation of this method so I'd rather not merge this pull request at this time.
👍 I will revisit this once |
#728 has now been merged to master. |
An atomic integer is not really required to count the invocationIndex. A local int variable should shuffice. Rewrite using java5 for-each and while loop instead of lambdas to remove the need for an effectively final mutable number.
6951513
to
c11c2d2
Compare
Redid this on top of the new master. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To me, the old implementation looks simpler and more elegant. Usage of AtomicInteger
being a minor hindrance.
@junit-team/junit-lambda What do you think?
++invocationIndex); | ||
execute(dynamicTestExecutor, invocationTestDescriptor); | ||
} | ||
invocationContextStream.close(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The stream should be used within a try-with-resources statement to ensure it's closed in case odf an exception.
I'm with @marcphilipp here. If I converted the lambda-chain to a loop-construct, I'd used a plain for-i loop here emphasizing on the importance of the index variable. |
Out of curiosity I did a micro-benchmark: import static org.openjdk.jmh.annotations.Scope.Benchmark;
import java.util.concurrent.atomic.AtomicInteger;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.State;
public class MyBenchmark {
@State(Benchmark)
public static class MyState {
private int primitiveCounter = 0;
private AtomicInteger atomicCounter = new AtomicInteger();
private MutableInteger mutableCounter = new MutableInteger();
}
@Benchmark
public int primitiveCounter(MyState state) {
++state.primitiveCounter;
return state.primitiveCounter;
}
@Benchmark
public int atomicCounter(MyState state) {
state.atomicCounter.incrementAndGet();
return state.atomicCounter.get();
}
@Benchmark
public int mutableCounter(MyState state) {
state.mutableCounter.incrementAndGet();
return state.mutableCounter.get();
}
private static class MutableInteger {
private int value;
int incrementAndGet() {
return ++value;
}
int get() {
return value;
}
}
} Unsurprisingly, using a primitive counter is the fastest, but the
91 million ops/s is still fast enough, though. Thus, it's certainly not a performance bottleneck. |
I agree, and I prefer the use of the stream over the for-loop. If we change anything there, it should be a switch from |
My motivation for this was AtomicInteger is a class to be used when an application is trying to achieve atomic semantics in a concurrency context. Otherwise it can be misleading as it happened with @LiamClark in #723 (comment). I was definitely not trying to optimize. On the other hand I fell victim to Esclation of commitment for this. On the previous version of master my change was not that bad in readability but @marcphilipp's new lambda is pretty neat and very readable(:+1:) so my change is much worse now. Closing this. |
@gaganis It was probably me who set the precedent of using So basically, you can blame/tease me about it. 😜 |
@jbduncan I am not fond of blaming so you need not worry about that :) |
@marcphilipp [jmh micro benchmark] (little off-topic to OP) In fairness, the atomic counter method can use |
Updated benchmark: import static org.openjdk.jmh.annotations.Scope.Benchmark;
import java.util.concurrent.atomic.AtomicInteger;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.State;
public class MyBenchmark {
@State(Benchmark)
public static class MyState {
private int primitiveCounter = 0;
private AtomicInteger atomicCounter = new AtomicInteger();
private MutableInteger mutableCounter = new MutableInteger();
}
@Benchmark
public int primitiveCounter(MyState state) {
return ++state.primitiveCounter;
}
@Benchmark
public int atomicCounter(MyState state) {
return state.atomicCounter.incrementAndGet();
}
@Benchmark
public int mutableCounter(MyState state) {
return state.mutableCounter.incrementAndGet();
}
private static class MutableInteger {
private int value;
int incrementAndGet() {
return ++value;
}
}
} Results:
|
An atomic integer is not really required to count the invocationIndex.
A local int variable should shuffice. Rewrite using java5 for-each and
while loop instead of lambdas to remove the need for an effectively
final mutable number.
I hereby agree to the terms of the JUnit Contributor License Agreement.
Definition of Done
@API
annotations