You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Jan Kanis (JIRA)" <ji...@apache.org> on 2016/11/21 10:19:58 UTC

[jira] [Created] (GROOVY-8004) CompileStatic ImmutableList.size() calls DefaultGroovyMethods.size(Iterable)

Jan Kanis created GROOVY-8004:
---------------------------------

             Summary: CompileStatic ImmutableList.size() calls DefaultGroovyMethods.size(Iterable)
                 Key: GROOVY-8004
                 URL: https://issues.apache.org/jira/browse/GROOVY-8004
             Project: Groovy
          Issue Type: Bug
          Components: Compiler
    Affects Versions: 2.4.7
            Reporter: Jan Kanis


Calling {{size()}} on a Guava ImmutableList (which implements {{List}}) under {{@CompileStatic}} calls DefaultGroovyMethods.size(Iterable) instead ImmutableList.size(). If the variable is of type {{List}} and the compiler cannot deduce that it actually contains an {{ImmutableList}} the {{List.size()}} method is called as expected.

The same bug exists for general Guava collections. 

Obviously this is bad especially for large collections, where a {{size()}} ends up taking linear time instead of constant time.

Example:
{code}
import com.google.common.collect.ImmutableList
import groovy.transform.CompileStatic

@CompileStatic
class TestClass {
    public static void main(String[] args) {
        ImmutableList<String> l = ImmutableList.copyOf(args)
        List<String> ls = l as List

        println(l.size()) // Calls DefaultGroovyMethods.size(Iterable)
        println(ls.size()) // Calls List.size()
    }
}
{code}

View the generated code with {{javap -p -c TestClass.class}} to see the calls that were emitted.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)