Class TreePathCacher
- All Implemented Interfaces:
TreeVisitor<TreePath,Tree>
This class replicates some logic from com.sun.source.util.TreePath.getPath but caches
results so that lookups with overlapping paths reuse each other's work.
The search is lazy about allocation: scan(com.sun.source.tree.Tree, com.sun.source.tree.Tree) only pushes and pops the trees it
visits on currentStack and allocates no TreePath during traversal. Only when the
target is reached does buildPathForStack() materialize (and cache) the TreePath
nodes along the root-to-target path. Off-path trees visited during a search are therefore not
cached, which keeps foundPaths far smaller than caching every visited node would. See
the "TreePathCacher" entries in docs/developer/performance-notes.md for the allocation
measurements that motivated this design.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddArtificialPath(Tree tree, TreePath path) Adds the path of an artificial tree to the cache and records that the tree is artificial.voidAdds the given key and value to the cache.voidclear()Clear thefoundPathsand re-initialize with a newIdentityHashMap, and drop the subtree index and the set of artificial trees.getCachedPath(Tree target) Returns the cached path for the given tree, without scanning.getPath(CompilationUnitTree root, Tree target) Returns the TreePath for a Tree.Return the TreePath fortarget, searching the compilation unit thatrootPathbelongs to, but starting the search atrootPath's leaf and expanding outward.booleanisArtificial(Tree tree) Returns true iftreeis an artificial tree, whose path was registered withaddArtificialPath(com.sun.source.tree.Tree, com.sun.source.util.TreePath)for the current compilation unit.booleanReturns true if the tree is cached.Visit one tree as part of a search.voiduseUnit(CompilationUnitTree newUnit) Prepares the cache for the given compilation unit: clears it, unless it is already for that unit.Methods inherited from class com.sun.source.util.TreeScanner
reduce, scan, visitAnnotatedType, visitAnnotation, visitAnyPattern, visitArrayAccess, visitArrayType, visitAssert, visitAssignment, visitBinary, visitBindingPattern, visitBlock, visitBreak, visitCase, visitCatch, visitClass, visitCompilationUnit, visitCompoundAssignment, visitConditionalExpression, visitConstantCaseLabel, visitContinue, visitDeconstructionPattern, visitDefaultCaseLabel, visitDoWhileLoop, visitEmptyStatement, visitEnhancedForLoop, visitErroneous, visitExports, visitExpressionStatement, visitForLoop, visitIdentifier, visitIf, visitImport, visitInstanceOf, visitIntersectionType, visitLabeledStatement, visitLambdaExpression, visitLiteral, visitMemberReference, visitMemberSelect, visitMethod, visitMethodInvocation, visitModifiers, visitModule, visitNewArray, visitNewClass, visitOpens, visitOther, visitPackage, visitParameterizedType, visitParenthesized, visitPatternCaseLabel, visitPrimitiveType, visitProvides, visitRequires, visitReturn, visitStringTemplate, visitSwitch, visitSwitchExpression, visitSynchronized, visitThrow, visitTry, visitTypeCast, visitTypeParameter, visitUnary, visitUnionType, visitUses, visitVariable, visitWhileLoop, visitWildcard, visitYield
-
Constructor Details
-
TreePathCacher
public TreePathCacher()Construct a TreePathCacher.
-
-
Method Details
-
useUnit
Prepares the cache for the given compilation unit: clears it, unless it is already for that unit. The checkers of a compound checker share one cache, and each of them calls this when it starts a compilation unit.Clearing the cache when a later checker starts the same unit would lose the paths of the artificial trees that a CFG builder registered. A scan cannot find an artificial tree, so the registered path is the only source of its path.
- Parameters:
newUnit- the compilation unit that is about to be checked
-
isCached
Returns true if the tree is cached.- Parameters:
target- the tree to search for- Returns:
- true if the tree is cached
-
getCachedPath
Returns the cached path for the given tree, without scanning. Performs a single map lookup.Returns null in two distinct cases that this method does not distinguish: (a)
targetis not cached at all, or (b)targetwas previously searched for viagetPath(com.sun.source.tree.CompilationUnitTree, com.sun.source.tree.Tree)and not found in the compilation unit (in which casegetPath(com.sun.source.tree.CompilationUnitTree, com.sun.source.tree.Tree)cachednullto avoid re-scanning). Callers that need to distinguish these cases must follow up withisCached(com.sun.source.tree.Tree).- Parameters:
target- the tree to look up- Returns:
- the cached path for
target, or null iftargetis not cached or is cached as not-in-unit
-
addPath
Adds the given key and value to the cache.- Parameters:
target- the tree to addpath- the path to cache
-
addArtificialPath
Adds the path of an artificial tree to the cache and records that the tree is artificial.An artificial tree is one that the CFG builder created, such as the binary operation of a compound assignment. It is not in the compilation unit, so a scan cannot find it, and the path registered here is the only source of its path.
- Parameters:
tree- the artificial treepath- the path oftree
-
isArtificial
Returns true iftreeis an artificial tree, whose path was registered withaddArtificialPath(com.sun.source.tree.Tree, com.sun.source.util.TreePath)for the current compilation unit.- Parameters:
tree- a tree- Returns:
- true if
treeis an artificial tree
-
getPath
Returns the TreePath for a Tree.- Parameters:
root- the compilation unit to search intarget- the target tree to look for- Returns:
- the TreePath corresponding to target, or null if target is not found in the compilation root
-
getPath
Return the TreePath fortarget, searching the compilation unit thatrootPathbelongs to, but starting the search atrootPath's leaf and expanding outward.This searches the whole compilation unit, not just
rootPath's subtree (despite taking a path argument, it does not have the subtree-only semantics ofcom.sun.source.util.TreePath.getPath(TreePath, Tree)). The reason to passrootPathrather than just the compilation unit is locality: callers pass the path of the tree they are currently visiting, and the target is almost always inside that subtree, so starting the scan there finds it without rescanning the whole unit. On a class with very many top-level members this turns an O(members) rescan per lookup into a local one.- Parameters:
rootPath- a path within the compilation unit to search; its leaf's subtree is searched first, and its node chain seeds the path prefix (see the body)target- the target tree to look for- Returns:
- the TreePath corresponding to target, or null if target is not in the compilation unit
-
clear
public void clear()Clear thefoundPathsand re-initialize with a newIdentityHashMap, and drop the subtree index and the set of artificial trees. The cache is then for no compilation unit, so the nextuseUnit(com.sun.source.tree.CompilationUnitTree)clears it again. -
scan
Visit one tree as part of a search. PushestreeontocurrentStackfor the duration of the visit and allocates noTreePathhere; iftreeis the target,buildPathForStack()materializes the path and it is thrown out viaTreePathCacher.Result. This is the lazy counterpart of eagerly extending aTreePathfield on every node.- Overrides:
scanin classTreeScanner<TreePath,Tree>
-