8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended

Wed, 10 Jun 2015 09:13:27 +0200

author
jlahoda
date
Wed, 10 Jun 2015 09:13:27 +0200
changeset 2813
d94fe2d29b1e
parent 2812
9ec429ab0e7e
child 2814
380f6c17ea01

8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
Summary: Avoiding Scope listener leak by avoiding cache misses in Types.MembersClosureCache
Reviewed-by: mcimadamore, vromero
Contributed-by: maurizio.cimadamore@oracle.com

src/share/classes/com/sun/tools/javac/code/Types.java file | annotate | diff | comparison | revisions
test/tools/javac/types/ScopeListenerTest.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javac/code/Types.java	Fri May 29 10:15:36 2015 +0530
     1.2 +++ b/src/share/classes/com/sun/tools/javac/code/Types.java	Wed Jun 10 09:13:27 2015 +0200
     1.3 @@ -2694,74 +2694,98 @@
     1.4      // </editor-fold>
     1.5  
     1.6      // <editor-fold defaultstate="collapsed" desc="compute transitive closure of all members in given site">
     1.7 -    class MembersClosureCache extends SimpleVisitor<CompoundScope, Boolean> {
     1.8 -
     1.9 -        private WeakHashMap<TypeSymbol, Entry> _map =
    1.10 -                new WeakHashMap<TypeSymbol, Entry>();
    1.11 -
    1.12 -        class Entry {
    1.13 -            final boolean skipInterfaces;
    1.14 -            final CompoundScope compoundScope;
    1.15 -
    1.16 -            public Entry(boolean skipInterfaces, CompoundScope compoundScope) {
    1.17 -                this.skipInterfaces = skipInterfaces;
    1.18 -                this.compoundScope = compoundScope;
    1.19 +    class MembersClosureCache extends SimpleVisitor<Scope.CompoundScope, Void> {
    1.20 +
    1.21 +        private Map<TypeSymbol, CompoundScope> _map = new HashMap<>();
    1.22 +
    1.23 +        Set<TypeSymbol> seenTypes = new HashSet<>();
    1.24 +
    1.25 +        class MembersScope extends CompoundScope {
    1.26 +
    1.27 +            CompoundScope scope;
    1.28 +
    1.29 +            public MembersScope(CompoundScope scope) {
    1.30 +                super(scope.owner);
    1.31 +                this.scope = scope;
    1.32              }
    1.33  
    1.34 -            boolean matches(boolean skipInterfaces) {
    1.35 -                return this.skipInterfaces == skipInterfaces;
    1.36 +            Filter<Symbol> combine(final Filter<Symbol> sf) {
    1.37 +                return new Filter<Symbol>() {
    1.38 +                    @Override
    1.39 +                    public boolean accepts(Symbol s) {
    1.40 +                        return !s.owner.isInterface() && (sf == null || sf.accepts(s));
    1.41 +                    }
    1.42 +                };
    1.43 +            }
    1.44 +
    1.45 +            @Override
    1.46 +            public Iterable<Symbol> getElements(Filter<Symbol> sf) {
    1.47 +                return scope.getElements(combine(sf));
    1.48 +            }
    1.49 +
    1.50 +            @Override
    1.51 +            public Iterable<Symbol> getElementsByName(Name name, Filter<Symbol> sf) {
    1.52 +                return scope.getElementsByName(name, combine(sf));
    1.53 +            }
    1.54 +
    1.55 +            @Override
    1.56 +            public int getMark() {
    1.57 +                return scope.getMark();
    1.58              }
    1.59          }
    1.60  
    1.61 -        List<TypeSymbol> seenTypes = List.nil();
    1.62 +        CompoundScope nilScope;
    1.63  
    1.64          /** members closure visitor methods **/
    1.65  
    1.66 -        public CompoundScope visitType(Type t, Boolean skipInterface) {
    1.67 -            return null;
    1.68 +        public CompoundScope visitType(Type t, Void _unused) {
    1.69 +            if (nilScope == null) {
    1.70 +                nilScope = new CompoundScope(syms.noSymbol);
    1.71 +            }
    1.72 +            return nilScope;
    1.73          }
    1.74  
    1.75          @Override
    1.76 -        public CompoundScope visitClassType(ClassType t, Boolean skipInterface) {
    1.77 -            if (seenTypes.contains(t.tsym)) {
    1.78 +        public CompoundScope visitClassType(ClassType t, Void _unused) {
    1.79 +            if (!seenTypes.add(t.tsym)) {
    1.80                  //this is possible when an interface is implemented in multiple
    1.81 -                //superclasses, or when a classs hierarchy is circular - in such
    1.82 +                //superclasses, or when a class hierarchy is circular - in such
    1.83                  //cases we don't need to recurse (empty scope is returned)
    1.84                  return new CompoundScope(t.tsym);
    1.85              }
    1.86              try {
    1.87 -                seenTypes = seenTypes.prepend(t.tsym);
    1.88 +                seenTypes.add(t.tsym);
    1.89                  ClassSymbol csym = (ClassSymbol)t.tsym;
    1.90 -                Entry e = _map.get(csym);
    1.91 -                if (e == null || !e.matches(skipInterface)) {
    1.92 -                    CompoundScope membersClosure = new CompoundScope(csym);
    1.93 -                    if (!skipInterface) {
    1.94 -                        for (Type i : interfaces(t)) {
    1.95 -                            membersClosure.addSubScope(visit(i, skipInterface));
    1.96 -                        }
    1.97 +                CompoundScope membersClosure = _map.get(csym);
    1.98 +                if (membersClosure == null) {
    1.99 +                    membersClosure = new CompoundScope(csym);
   1.100 +                    for (Type i : interfaces(t)) {
   1.101 +                        membersClosure.addSubScope(visit(i, null));
   1.102                      }
   1.103 -                    membersClosure.addSubScope(visit(supertype(t), skipInterface));
   1.104 +                    membersClosure.addSubScope(visit(supertype(t), null));
   1.105                      membersClosure.addSubScope(csym.members());
   1.106 -                    e = new Entry(skipInterface, membersClosure);
   1.107 -                    _map.put(csym, e);
   1.108 +                    _map.put(csym, membersClosure);
   1.109                  }
   1.110 -                return e.compoundScope;
   1.111 +                return membersClosure;
   1.112              }
   1.113              finally {
   1.114 -                seenTypes = seenTypes.tail;
   1.115 +                seenTypes.remove(t.tsym);
   1.116              }
   1.117          }
   1.118  
   1.119          @Override
   1.120 -        public CompoundScope visitTypeVar(TypeVar t, Boolean skipInterface) {
   1.121 -            return visit(t.getUpperBound(), skipInterface);
   1.122 +        public CompoundScope visitTypeVar(TypeVar t, Void _unused) {
   1.123 +            return visit(t.getUpperBound(), null);
   1.124          }
   1.125      }
   1.126  
   1.127      private MembersClosureCache membersCache = new MembersClosureCache();
   1.128  
   1.129      public CompoundScope membersClosure(Type site, boolean skipInterface) {
   1.130 -        return membersCache.visit(site, skipInterface);
   1.131 +        CompoundScope cs = membersCache.visit(site, null);
   1.132 +        if (cs == null)
   1.133 +            Assert.error("type " + site);
   1.134 +        return skipInterface ? membersCache.new MembersScope(cs) : cs;
   1.135      }
   1.136      // </editor-fold>
   1.137  
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/tools/javac/types/ScopeListenerTest.java	Wed Jun 10 09:13:27 2015 +0200
     2.3 @@ -0,0 +1,82 @@
     2.4 +/*
     2.5 + * Copyright (c) 2015, 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 8039262
    2.30 + * @summary Ensure that using Types.membersClosure does not increase the number of listeners on the
    2.31 + *          class's members Scope.
    2.32 + */
    2.33 +
    2.34 +import com.sun.tools.javac.code.Scope;
    2.35 +import com.sun.tools.javac.code.Symbol;
    2.36 +import com.sun.tools.javac.code.Symtab;
    2.37 +import com.sun.tools.javac.code.Types;
    2.38 +import com.sun.tools.javac.file.JavacFileManager;
    2.39 +import com.sun.tools.javac.util.Context;
    2.40 +import com.sun.tools.javac.util.Names;
    2.41 +import java.lang.reflect.Field;
    2.42 +import java.util.Collection;
    2.43 +
    2.44 +public class ScopeListenerTest {
    2.45 +
    2.46 +    public static void main(String[] args) throws Exception {
    2.47 +        new ScopeListenerTest().run();
    2.48 +    }
    2.49 +
    2.50 +    void run() throws Exception {
    2.51 +        Context context = new Context();
    2.52 +        JavacFileManager.preRegister(context);
    2.53 +        Types types = Types.instance(context);
    2.54 +        Symtab syms = Symtab.instance(context);
    2.55 +        Names names = Names.instance(context);
    2.56 +        types.membersClosure(syms.stringType, true);
    2.57 +        types.membersClosure(syms.stringType, false);
    2.58 +
    2.59 +        Field listenersField = Scope.class.getDeclaredField("listeners");
    2.60 +
    2.61 +        listenersField.setAccessible(true);
    2.62 +
    2.63 +        int listenerCount =
    2.64 +                ((Collection) listenersField.get(syms.stringType.tsym.members())).size();
    2.65 +
    2.66 +        for (int i = 0; i < 100; i++) {
    2.67 +            types.membersClosure(syms.stringType, true);
    2.68 +            types.membersClosure(syms.stringType, false);
    2.69 +        }
    2.70 +
    2.71 +        int newListenerCount
    2.72 +                = ((Collection) listenersField.get(syms.stringType.tsym.members())).size();
    2.73 +
    2.74 +        if (listenerCount != newListenerCount) {
    2.75 +            throw new AssertionError("Orig listener count: " + listenerCount +
    2.76 +                                     "; new listener count: " + newListenerCount);
    2.77 +        }
    2.78 +
    2.79 +        for (Symbol s : types.membersClosure(syms.stringType, true).getElements())
    2.80 +            ;
    2.81 +        for (Symbol s : types.membersClosure(syms.stringType, false).getElementsByName(names.fromString("substring")))
    2.82 +            ;
    2.83 +    }
    2.84 +
    2.85 +}

mercurial