Details
Description
I believe this code should print "something" but doesn't work:
@CompileStatic class NotWorkingExample { static void main(String[] args) { Collection<String> values = null for (i in 0..<200_000) { printSomethingIfNotEmpty(values) } //never printed but it should values = ['A'] printSomethingIfNotEmpty(values) } static printSomethingIfNotEmpty(Collection<String> values) { if(values) { println 'something' } } }
This one does print "something" because we pass an empty collection [] instead of null:
@CompileStatic class ItWorks { static void main(String[] args) { Collection<String> values = [] for (i in 0..<200_000) { printSomethingIfNotEmpty(values) } //it works because [] was passed in the previous 200k calls values = ['A'] printSomethingIfNotEmpty(values) } static printSomethingIfNotEmpty(Collection<String> values) { if(values) { println 'something' } } }
Some optimization is done differently when the condition is skipped too many times. Both classes should output "something" on the last method call.
Thank you for the support.
Attachments
Issue Links
- is caused by
-
GROOVY-8298 Slow Performance Caused by Invoke Dynamic
- Closed
- relates to
-
GROOVY-10596 IF-condition evaluation on the non-null object sometimes doesn't return true
- Closed
-
GROOVY-10618 SC: further optimize cast to bool
- Closed
- links to