Details
-
Type:
Enhancement
-
Status: Open
-
Priority:
P4
-
Resolution: Unresolved
-
Affects Version/s: None
-
Fix Version/s: None
-
Component/s: core-libs
-
Labels:
-
Subcomponent:
Description
Currently these methods are implemented with short-circuit operators as
public static boolean logicalAnd(boolean a, boolean b) {
return a && b;
}
Normally, short-circuit operators may avoid evaluation of the second operand. However, in this case both arguments are already evaluated.
Using eager operators & and | first, produce more compact bytecode, and second show some measurable speedup in the benchmarks.
Here _1 stands for short-circuit and and _2 -- for eager and:
Benchmark (a) (b) Mode Cnt Score Error Units
Booleans.testLogicalAnd_1 false true avgt 20 2.725 ± 0.042 ns/op
Booleans.testLogicalAnd_1 false false avgt 20 2.713 ± 0.016 ns/op
Booleans.testLogicalAnd_1 true true avgt 20 2.781 ± 0.226 ns/op
Booleans.testLogicalAnd_1 true false avgt 20 2.698 ± 0.007 ns/op
Booleans.testLogicalAnd_2 false true avgt 20 2.601 ± 0.010 ns/op
Booleans.testLogicalAnd_2 false false avgt 20 2.604 ± 0.010 ns/op
Booleans.testLogicalAnd_2 true true avgt 20 2.609 ± 0.021 ns/op
Booleans.testLogicalAnd_2 true false avgt 20 2.598 ± 0.010 ns/op
public static boolean logicalAnd(boolean a, boolean b) {
return a && b;
}
Normally, short-circuit operators may avoid evaluation of the second operand. However, in this case both arguments are already evaluated.
Using eager operators & and | first, produce more compact bytecode, and second show some measurable speedup in the benchmarks.
Here _1 stands for short-circuit and and _2 -- for eager and:
Benchmark (a) (b) Mode Cnt Score Error Units
Booleans.testLogicalAnd_1 false true avgt 20 2.725 ± 0.042 ns/op
Booleans.testLogicalAnd_1 false false avgt 20 2.713 ± 0.016 ns/op
Booleans.testLogicalAnd_1 true true avgt 20 2.781 ± 0.226 ns/op
Booleans.testLogicalAnd_1 true false avgt 20 2.698 ± 0.007 ns/op
Booleans.testLogicalAnd_2 false true avgt 20 2.601 ± 0.010 ns/op
Booleans.testLogicalAnd_2 false false avgt 20 2.604 ± 0.010 ns/op
Booleans.testLogicalAnd_2 true true avgt 20 2.609 ± 0.021 ns/op
Booleans.testLogicalAnd_2 true false avgt 20 2.598 ± 0.010 ns/op