From e2025fb5ad7e9bd66a11b12e43f8ff9bfbfeef68 Mon Sep 17 00:00:00 2001 From: Michael Bien Date: Sun, 13 Apr 2025 02:15:45 +0200 Subject: [PATCH] Remove source level query from javadoc hint. plus small inner loop optimization in TreeUtilities. The javadoc hint is typically in the top three list of slowest hints in files with many elements (fields, methods etc), even without the presence of javadoc sections. Due to its high per-file invocation count, a source level or preferences query can end up being quite expensive. Luckily the source level query wasn't even used, Preferences will be addressed separately. --- .../api/java/source/TreeUtilities.java | 38 +++++++++---------- .../modules/javadoc/hints/Analyzer.java | 20 ++++------ 2 files changed, 24 insertions(+), 34 deletions(-) diff --git a/java/java.source.base/src/org/netbeans/api/java/source/TreeUtilities.java b/java/java.source.base/src/org/netbeans/api/java/source/TreeUtilities.java index d8c6a4ea0db3..c3ac869d93a4 100644 --- a/java/java.source.base/src/org/netbeans/api/java/source/TreeUtilities.java +++ b/java/java.source.base/src/org/netbeans/api/java/source/TreeUtilities.java @@ -198,29 +198,25 @@ public boolean isSynthetic(TreePath path) throws NullPointerException { while (path != null) { if (isSynthetic(path.getCompilationUnit(), path.getLeaf())) return true; - if (path.getParentPath() != null && - path.getParentPath().getParentPath() != null && - path.getParentPath().getParentPath().getLeaf().getKind() == Kind.NEW_CLASS) { - NewClassTree nct = (NewClassTree) path.getParentPath().getParentPath().getLeaf(); - ClassTree body = nct.getClassBody(); - - if (body != null && - (body.getExtendsClause() == path.getLeaf() || - body.getImplementsClause().contains(path.getLeaf()))) { - return true; - } - } - if (path.getLeaf().getKind() == Kind.VARIABLE && - path.getParentPath() != null && - path.getParentPath().getLeaf().getKind() == Kind.METHOD && - path.getParentPath().getParentPath() != null && - path.getParentPath().getParentPath().getLeaf().getKind() == Kind.RECORD) { - JCMethodDecl m = (JCMethodDecl) path.getParentPath().getLeaf(); - if ((m.mods.flags & Flags.COMPACT_RECORD_CONSTRUCTOR) != 0 && m.getParameters().contains(path.getLeaf())) { - return true; + if (path.getParentPath() != null && path.getParentPath().getParentPath() != null) { + TreePath grandpa = path.getParentPath().getParentPath(); + if (grandpa.getLeaf().getKind() == Kind.NEW_CLASS) { + NewClassTree nct = (NewClassTree) grandpa.getLeaf(); + ClassTree body = nct.getClassBody(); + if (body != null && + (body.getExtendsClause() == path.getLeaf() || + body.getImplementsClause().contains(path.getLeaf()))) { + return true; + } + } else if (grandpa.getLeaf().getKind() == Kind.RECORD && + path.getLeaf().getKind() == Kind.VARIABLE && + path.getParentPath().getLeaf().getKind() == Kind.METHOD) { + JCMethodDecl m = (JCMethodDecl) path.getParentPath().getLeaf(); + if ((m.mods.flags & Flags.COMPACT_RECORD_CONSTRUCTOR) != 0 && m.getParameters().contains(path.getLeaf())) { + return true; + } } } - path = path.getParentPath(); } diff --git a/java/javadoc/src/org/netbeans/modules/javadoc/hints/Analyzer.java b/java/javadoc/src/org/netbeans/modules/javadoc/hints/Analyzer.java index 1a753062262a..548b65ae1563 100644 --- a/java/javadoc/src/org/netbeans/modules/javadoc/hints/Analyzer.java +++ b/java/javadoc/src/org/netbeans/modules/javadoc/hints/Analyzer.java @@ -51,7 +51,6 @@ import com.sun.source.tree.ClassTree; import com.sun.source.tree.ExpressionTree; import com.sun.source.tree.MethodTree; -import com.sun.source.tree.ThrowTree; import com.sun.source.tree.Tree; import com.sun.source.util.DocSourcePositions; import com.sun.source.util.DocTreePath; @@ -59,7 +58,6 @@ import com.sun.source.util.TreePath; import java.io.IOException; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.Deque; import java.util.HashSet; @@ -69,7 +67,6 @@ import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; -import javax.lang.model.SourceVersion; import javax.lang.model.element.Element; import javax.lang.model.element.ElementKind; import javax.lang.model.element.ExecutableElement; @@ -77,7 +74,6 @@ import javax.lang.model.element.TypeElement; import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeMirror; -import javax.lang.model.type.TypeVariable; import javax.lang.model.util.Elements; import javax.lang.model.util.Types; import org.netbeans.api.java.source.CompilationInfo; @@ -93,7 +89,6 @@ import org.netbeans.modules.html.editor.lib.api.model.HtmlTag; import org.netbeans.modules.html.editor.lib.api.model.HtmlTagType; import static org.netbeans.modules.javadoc.hints.Bundle.*; -import static org.netbeans.modules.javadoc.hints.JavadocUtilities.resolveSourceVersion; import org.netbeans.spi.editor.hints.ErrorDescription; import org.netbeans.spi.java.hints.ErrorDescriptionFactory; import org.netbeans.spi.java.hints.HintContext; @@ -118,8 +113,9 @@ * @author Jan Pokorsky * @author Ralph Benjamin Ruijs */ - @NbBundle.Messages({"MISSING_RETURN_DESC=Missing @return tag.", - "# {0} - @param name", "MISSING_PARAM_DESC=Missing @param tag for {0}"}) +@NbBundle.Messages({"MISSING_RETURN_DESC=Missing @return tag.", + "# {0} - @param name", + "MISSING_PARAM_DESC=Missing @param tag for {0}"}) final class Analyzer extends DocTreePathScanner> { private static final HtmlModel model = HtmlModelFactory.getModel(HtmlVersion.XHTML5); @@ -127,12 +123,11 @@ final class Analyzer extends DocTreePathScanner> { private final CompilationInfo javac; private final FileObject file; private final TreePath currentPath; - private final SourceVersion sourceVersion; private final Access access; - private Deque tagStack = new LinkedList<>(); - private Set foundParams = new HashSet<>(); - private Set foundThrows = new HashSet<>(); + private final Deque tagStack = new LinkedList<>(); + private final Set foundParams = new HashSet<>(); + private final Set foundThrows = new HashSet<>(); private TypeMirror returnType = null; private boolean returnTypeFound = false; private boolean foundInheritDoc = false; @@ -143,7 +138,6 @@ final class Analyzer extends DocTreePathScanner> { this.javac = javac; this.file = javac.getFileObject(); this.currentPath = currentPath; - this.sourceVersion = resolveSourceVersion(javac.getFileObject()); this.access = access; this.ctx = ctx; } @@ -814,7 +808,7 @@ private static HtmlTag getTag(Name tagName) { return t.getTagClass() == HtmlTagType.HTML ? t : null; } - private static final Set NON_VOID_TAGS = new HashSet<>(Arrays.asList("menuitem", "noscript", "script", "style")); + private static final Set NON_VOID_TAGS = Set.of("menuitem", "noscript", "script", "style"); private boolean isVoid(HtmlTag tag) { return tag.isEmpty() && !NON_VOID_TAGS.contains(tag.getName());