7142086: performance problem in Check.checkOverrideClashes(...)

Tue, 14 Feb 2012 15:43:52 -0800

author
mcimadamore
date
Tue, 14 Feb 2012 15:43:52 -0800
changeset 1198
84b61130cbed
parent 1197
237198ef45f5
child 1199
e127334a64fe

7142086: performance problem in Check.checkOverrideClashes(...)
Summary: Code in Check.checkOverrideClashes() causes too many calls to MethodSymbol.overrides
Reviewed-by: jjg
Contributed-by: jan.lahoda@oracle.com

src/share/classes/com/sun/tools/javac/comp/Check.java file | annotate | diff | comparison | revisions
test/tools/javac/7142086/T7142086.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javac/comp/Check.java	Mon Feb 13 16:01:43 2012 -0800
     1.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Check.java	Tue Feb 14 15:43:52 2012 -0800
     1.3 @@ -1,5 +1,5 @@
     1.4  /*
     1.5 - * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
     1.7   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.8   *
     1.9   * This code is free software; you can redistribute it and/or modify it
    1.10 @@ -2114,25 +2114,26 @@
    1.11       */
    1.12      void checkOverrideClashes(DiagnosticPosition pos, Type site, MethodSymbol sym) {
    1.13           ClashFilter cf = new ClashFilter(site);
    1.14 -         //for each method m1 that is a member of 'site'...
    1.15 -         for (Symbol s1 : types.membersClosure(site, false).getElementsByName(sym.name, cf)) {
    1.16 -            //...find another method m2 that is overridden (directly or indirectly)
    1.17 -            //by method 'sym' in 'site'
    1.18 -            for (Symbol s2 : types.membersClosure(site, false).getElementsByName(sym.name, cf)) {
    1.19 -                if (s1 == s2 || !sym.overrides(s2, site.tsym, types, false)) continue;
    1.20 +        //for each method m1 that is overridden (directly or indirectly)
    1.21 +        //by method 'sym' in 'site'...
    1.22 +        for (Symbol m1 : types.membersClosure(site, false).getElementsByName(sym.name, cf)) {
    1.23 +            if (!sym.overrides(m1, site.tsym, types, false)) continue;
    1.24 +             //...check each method m2 that is a member of 'site'
    1.25 +             for (Symbol m2 : types.membersClosure(site, false).getElementsByName(sym.name, cf)) {
    1.26 +                if (m2 == m1) continue;
    1.27                  //if (i) the signature of 'sym' is not a subsignature of m1 (seen as
    1.28                  //a member of 'site') and (ii) m1 has the same erasure as m2, issue an error
    1.29 -                if (!types.isSubSignature(sym.type, types.memberType(site, s1), false) &&
    1.30 -                        types.hasSameArgs(s1.erasure(types), s2.erasure(types))) {
    1.31 +                if (!types.isSubSignature(sym.type, types.memberType(site, m2), false) &&
    1.32 +                        types.hasSameArgs(m2.erasure(types), m1.erasure(types))) {
    1.33                      sym.flags_field |= CLASH;
    1.34 -                    String key = s2 == sym ?
    1.35 +                    String key = m1 == sym ?
    1.36                              "name.clash.same.erasure.no.override" :
    1.37                              "name.clash.same.erasure.no.override.1";
    1.38                      log.error(pos,
    1.39                              key,
    1.40                              sym, sym.location(),
    1.41 -                            s1, s1.location(),
    1.42 -                            s2, s2.location());
    1.43 +                            m2, m2.location(),
    1.44 +                            m1, m1.location());
    1.45                      return;
    1.46                  }
    1.47              }
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/tools/javac/7142086/T7142086.java	Tue Feb 14 15:43:52 2012 -0800
     2.3 @@ -0,0 +1,115 @@
     2.4 +/*
     2.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
     2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     2.7 + *
     2.8 + * This code is free software; you can redistribute it and/or modify it
     2.9 + * under the terms of the GNU General Public License version 2 only, as
    2.10 + * published by the Free Software Foundation.
    2.11 + *
    2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    2.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    2.15 + * version 2 for more details (a copy is included in the LICENSE file that
    2.16 + * accompanied this code).
    2.17 + *
    2.18 + * You should have received a copy of the GNU General Public License version
    2.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    2.21 + *
    2.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    2.23 + * or visit www.oracle.com if you need additional information or have any
    2.24 + * questions.
    2.25 + */
    2.26 +
    2.27 +/*
    2.28 + * @test
    2.29 + * @bug 7142086
    2.30 + * @summary performance problem in Check.checkOverrideClashes(...)
    2.31 + * @run main/timeout=10 T7142086
    2.32 + */
    2.33 +
    2.34 +import com.sun.source.util.JavacTask;
    2.35 +import java.net.URI;
    2.36 +import java.util.List;
    2.37 +import java.util.ArrayList;
    2.38 +import java.util.Locale;
    2.39 +import javax.tools.Diagnostic;
    2.40 +import javax.tools.JavaCompiler;
    2.41 +import javax.tools.JavaFileObject;
    2.42 +import javax.tools.SimpleJavaFileObject;
    2.43 +import javax.tools.StandardJavaFileManager;
    2.44 +import javax.tools.ToolProvider;
    2.45 +
    2.46 +public class T7142086 {
    2.47 +
    2.48 +    final static int N_METHODS = 1000;
    2.49 +
    2.50 +    static class TestClass extends SimpleJavaFileObject {
    2.51 +
    2.52 +        String methTemplate = "abstract void m(A#N p);";
    2.53 +        String classTemplate = "abstract class Test { #M }";
    2.54 +
    2.55 +        String source;
    2.56 +
    2.57 +        public TestClass() {
    2.58 +            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
    2.59 +            StringBuilder buf = new StringBuilder();
    2.60 +            for (int i = 0 ; i < N_METHODS ; i++) {
    2.61 +                buf.append(methTemplate.replace("#N", String.valueOf(i)));
    2.62 +                buf.append("\n");
    2.63 +            }
    2.64 +            source = classTemplate.replace("#M", buf.toString());
    2.65 +        }
    2.66 +
    2.67 +        @Override
    2.68 +        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
    2.69 +            return source;
    2.70 +        }
    2.71 +    }
    2.72 +
    2.73 +    static class AnSource extends SimpleJavaFileObject {
    2.74 +
    2.75 +        String classTemplate = "abstract class A#N { }";
    2.76 +
    2.77 +        String source;
    2.78 +
    2.79 +        public AnSource(int n) {
    2.80 +            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
    2.81 +            source = classTemplate.replace("#N", String.valueOf(n));
    2.82 +        }
    2.83 +
    2.84 +        @Override
    2.85 +        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
    2.86 +            return source;
    2.87 +        }
    2.88 +    }
    2.89 +
    2.90 +    public static void main(String... args) throws Exception {
    2.91 +        ArrayList<JavaFileObject> sources = new ArrayList<>();
    2.92 +        for (int i = 0 ; i < N_METHODS ; i++) {
    2.93 +            sources.add(new AnSource(i));
    2.94 +        }
    2.95 +        sources.add(new TestClass());
    2.96 +        new T7142086().run(sources);
    2.97 +    }
    2.98 +
    2.99 +    void run(List<JavaFileObject> sources) throws Exception {
   2.100 +        DiagnosticChecker dc = new DiagnosticChecker();
   2.101 +        JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
   2.102 +        StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
   2.103 +        JavacTask ct = (JavacTask)comp.getTask(null, fm, dc,
   2.104 +                null, null, sources);
   2.105 +        ct.analyze();
   2.106 +    }
   2.107 +
   2.108 +    static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
   2.109 +
   2.110 +        boolean errorFound;
   2.111 +
   2.112 +        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   2.113 +            if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
   2.114 +                throw new AssertionError("unexpected diagnostic: " + diagnostic.getMessage(Locale.getDefault()));
   2.115 +            }
   2.116 +        }
   2.117 +    }
   2.118 +}

mercurial