Repository navigation
Description
In JSpecify mode, NullAway takes exponential time to check an assignment of a wildcard-parameterized type when the intersection bounds of the type parameters form a cycle through the middle of the bound graph. With 5 levels of bounds, the test below takes 20.7 s on master at ce85293, which is #1972, and 1.2 s on the commit before it, 90edd0b. Most of the 1.2 s is the warm-up of the first test in the JVM. With 6 levels, the check does not finish in 60 s.
Versions: NullAway master at ce85293 (0.14.4-SNAPSHOT), the first bad commit; last good 90edd0b. The latest release, v0.14.3, does not contain #1972. JDK 21.0.9, macOS.
Reproducer
package com.uber.nullaway.jspecify;
import com.uber.nullaway.NullAwayTestsBase;
import com.uber.nullaway.generics.JSpecifyJavacConfig;
import java.util.Collections;
import org.junit.Test;
public class IntersectionBackEdgeTimeoutTest extends NullAwayTestsBase {
@Test(timeout = 120_000)
public void assignmentWithBackEdgeInIntersectionBounds() {
makeTestHelperWithArgs(
JSpecifyJavacConfig.withJSpecifyModeArgs(
Collections.singletonList("-XepOpt:NullAway:OnlyNullMarked=true")))
.addSourceLines(
"Test.java",
"""
interface A0<T extends A5<?> & B5<?>> {}
interface B0<T extends B5<?>> {}
interface A1<T extends A0<?> & B0<?>> {}
interface B1<T extends A0<?> & B0<?>> {}
interface A2<T extends A1<?> & B1<?>> {}
interface B2<T extends A1<?> & B1<?>> {}
interface A3<T extends A2<?> & B2<?>> {}
interface B3<T extends A2<?> & B2<?>> {}
interface A4<T extends A3<?> & B3<?>> {}
interface B4<T extends A3<?> & B3<?>> {}
interface A5<T extends A4<?> & B4<?>> {}
interface B5<T extends A4<?> & B4<?>> {}
@org.jspecify.annotations.NullMarked
abstract class Test {
abstract A5<?> get();
void trigger(Test r) {
A5<?> x = r.get();
}
}
""")
.doTest();
}
}./gradlew :nullaway:test --tests 'com.uber.nullaway.jspecify.IntersectionBackEdgeTimeoutTest'The test passes, so NullAway reports nothing; only the time is wrong.
Time by depth
The same shape generated for depths 2 to 8, one run per depth. The depths ran in one JUnit class, so only the first test of each run pays for the warm-up. This generated variant also declares an unused abstract B<n-1><?> get2();.
| Levels | ce85293 | 90edd0b |
|---|---|---|
| 2 | 0.18 s | |
| 3 | 0.40 s | |
| 4 | 1.9 s | |
| 5 | 19.4 s | 1.0 s (first test, includes warm-up) |
| 6 | timed out at 60 s | 0.09 s |
| 7 | timed out at 60 s | 0.08 s |
| 8 | timed out at 60 s | 0.08 s |
Two variants of the graph stay fast on ce85293. With only a cycle at the leaf, interface A0<T extends A0<?>>, the shape SharedIntersectionTimeoutTests covers, 30 levels compile in 1.5 s. With the assignment replaced by a bare call r.get();, depths 6 to 14 compile in under 0.5 s each, apart from the first test of each run, which took about 1 s with the warm-up. So the slow path needs both the back edge and the assignment check.
Where the time goes
jstack of the 6-level run shows CheckIdenticalNullabilityVisitor.wildcardContains 22 times in one stack, each level followed by a full annotation restoration under TypeSubstitutionUtils.asSuper. An excerpt with one repetition and the bottom of the stack (trimmed):
com.uber.nullaway.generics.TypeSubstitutionUtils$RestoreNullnessAnnotationsVisitor.restoreImplicitWildcardBound(TypeSubstitutionUtils.java:562)
com.uber.nullaway.generics.TypeSubstitutionUtils$RestoreNullnessAnnotationsVisitor.visitWildcardType(TypeSubstitutionUtils.java:685)
...
com.uber.nullaway.generics.TypeSubstitutionUtils.restoreExplicitNullabilityAnnotations(TypeSubstitutionUtils.java:132)
com.uber.nullaway.generics.TypeSubstitutionUtils.asSuper(TypeSubstitutionUtils.java:83)
com.uber.nullaway.generics.CheckIdenticalNullabilityVisitor.visitClassType(CheckIdenticalNullabilityVisitor.java:90)
com.uber.nullaway.generics.CheckIdenticalNullabilityVisitor.visitClassType(CheckIdenticalNullabilityVisitor.java:25)
com.sun.tools.javac.code.Type$ClassType.accept(jdk.compiler@21.0.9/Type.java:1050)
com.uber.nullaway.generics.CheckIdenticalNullabilityVisitor.lambda$handleIntersectionType$0(CheckIdenticalNullabilityVisitor.java:139)
...
com.uber.nullaway.generics.CheckIdenticalNullabilityVisitor.handleIntersectionType(CheckIdenticalNullabilityVisitor.java:139)
com.uber.nullaway.generics.CheckIdenticalNullabilityVisitor.visitClassType(CheckIdenticalNullabilityVisitor.java:84)
com.uber.nullaway.generics.CheckIdenticalNullabilityVisitor.visitClassType(CheckIdenticalNullabilityVisitor.java:25)
com.sun.tools.javac.code.Type$ClassType.accept(jdk.compiler@21.0.9/Type.java:1050)
com.uber.nullaway.generics.GenericsChecks.identicalTypeParameterNullability(GenericsChecks.java:2491)
com.uber.nullaway.generics.GenericsChecks.subtypeParameterNullability(GenericsChecks.java:2547)
com.uber.nullaway.generics.CheckIdenticalNullabilityVisitor.typeArgumentSubtype(CheckIdenticalNullabilityVisitor.java:298)
com.uber.nullaway.generics.CheckIdenticalNullabilityVisitor.wildcardContains(CheckIdenticalNullabilityVisitor.java:251)
com.uber.nullaway.generics.CheckIdenticalNullabilityVisitor.typeArgumentContainedBy(CheckIdenticalNullabilityVisitor.java:193)
com.uber.nullaway.generics.CheckIdenticalNullabilityVisitor.visitClassType(CheckIdenticalNullabilityVisitor.java:122)
...
com.uber.nullaway.generics.GenericsChecks.checkTypeParameterNullnessForAssignability(GenericsChecks.java:1092)
com.uber.nullaway.NullAway.matchVariable(NullAway.java:1982)
Expected
The check of the 5-level test finishes in about the time it took before #1972, about 0.1 s after warm-up, and the time grows polynomially with the depth.
Possible cause (a reading of the code, not tested)
Before #1972, RestoreNullnessAnnotationsVisitor did not descend into the components of an intersection, so each asSuper call was cheap. It now walks the implicit wildcard bounds through every component. The BoundPair and BoundResult cache belongs to one visitor, and restoreExplicitNullabilityAnnotations creates a new visitor for each call (TypeSubstitutionUtils.java:132), so nothing is reused between the asSuper calls that wildcardContains makes at each level. Within one call, BoundResult.canReuse rejects a cached result while a pair of its cycle is active, which holds for every result below a back edge. Two possible directions: skip the implicit wildcard bounds of intersection components, which would also remove the need for the cycle bookkeeping, or keep a cache across the asSuper calls of one check.
Activity
Add AGENTS.md rules against repeated analysis work and recursion cycl…
Verify bound-cache reuse without reflection
Fix cyclic intersection assignment performance (apache#2095)