Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[performance] BoundSet: move properTypesByInferenceVariable before loop #3597

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,7 @@ boolean incorporate(InferenceContext18 context, TypeBound [] first, TypeBound []
boolean analyzeNull = context.environment.globalOptions.isAnnotationBasedNullAnalysisEnabled;
ConstraintTypeFormula [] mostRecentFormulas = new ConstraintTypeFormula[4]; // poor man's cache to toss out duplicates, in pathological cases there are a good quarter million of them.
// check each pair, in each way.
Map<InferenceVariable,TypeBound> properTypesByInferenceVariable = properTypesByInferenceVariable(first, next);
for (TypeBound bound1 : first) {
for (TypeBound bound2 : next) {
if (bound1 == bound2)
Expand All @@ -548,18 +549,18 @@ boolean incorporate(InferenceContext18 context, TypeBound [] first, TypeBound []
case ReductionResult.SAME:
switch (bound2.relation) {
case ReductionResult.SAME:
newConstraint = combineSameSame(bound1, bound2, first, next);
newConstraint = combineSameSame(bound1, bound2, first, next, properTypesByInferenceVariable);
break;
case ReductionResult.SUBTYPE:
case ReductionResult.SUPERTYPE:
newConstraint = combineSameSubSuper(bound1, bound2, first, next);
newConstraint = combineSameSubSuper(bound1, bound2, first, next, properTypesByInferenceVariable);
break;
}
break;
case ReductionResult.SUBTYPE:
switch (bound2.relation) {
case ReductionResult.SAME:
newConstraint = combineSameSubSuper(bound2, bound1, first, next);
newConstraint = combineSameSubSuper(bound2, bound1, first, next, properTypesByInferenceVariable);
break;
case ReductionResult.SUPERTYPE:
newConstraint = combineSuperAndSub(bound2, bound1);
Expand All @@ -573,7 +574,7 @@ boolean incorporate(InferenceContext18 context, TypeBound [] first, TypeBound []
case ReductionResult.SUPERTYPE:
switch (bound2.relation) {
case ReductionResult.SAME:
newConstraint = combineSameSubSuper(bound2, bound1, first, next);
newConstraint = combineSameSubSuper(bound2, bound1, first, next, properTypesByInferenceVariable);
break;
case ReductionResult.SUBTYPE:
newConstraint = combineSuperAndSub(bound1, bound2);
Expand Down Expand Up @@ -740,33 +741,33 @@ void addTypeBoundsFromWildcardBound(InferenceContext18 context, InferenceSubstit
reduceOneConstraint(context, formula);
}

private ConstraintTypeFormula combineSameSame(TypeBound boundS, TypeBound boundT, TypeBound[] firstBounds, TypeBound[] nextBounds) {
private ConstraintTypeFormula combineSameSame(TypeBound boundS, TypeBound boundT, TypeBound[] firstBounds, TypeBound[] nextBounds, Map<InferenceVariable,TypeBound> properTypesByInferenceVariable) {

// α = S and α = T imply ⟨S = T⟩
if (TypeBinding.equalsEquals(boundS.left, boundT.left))
return ConstraintTypeFormula.create(boundS.right, boundT.right, ReductionResult.SAME, boundS.isSoft||boundT.isSoft);

// match against more shapes:
ConstraintTypeFormula newConstraint;
newConstraint = combineSameSameWithProperType(boundS, boundT, firstBounds, nextBounds);
newConstraint = combineSameSameWithProperType(boundS, boundT, firstBounds, nextBounds, properTypesByInferenceVariable);
if (newConstraint != null)
return newConstraint;
newConstraint = combineSameSameWithProperType(boundT, boundS, firstBounds, nextBounds);
newConstraint = combineSameSameWithProperType(boundT, boundS, firstBounds, nextBounds, properTypesByInferenceVariable);
if (newConstraint != null)
return newConstraint;
return null;
}

// pre: boundLeft.left != boundRight.left
private ConstraintTypeFormula combineSameSameWithProperType(TypeBound boundLeft, TypeBound boundRight,
TypeBound[] firstBounds, TypeBound[] nextBounds) {
TypeBound[] firstBounds, TypeBound[] nextBounds, Map<InferenceVariable,TypeBound> properTypesByInferenceVariable) {
// α = U and S = T imply ⟨S[α:=U] = T[α:=U]⟩
TypeBinding u = boundLeft.right;
if (enableOptimizationForBug543480 && isParameterizedDependency(boundRight)) {
// Performance optimization: do not incorporate arguments one by one, which yielt 2^n new bounds (n=number of type arguments) in the past.
// Instead, all arguments of a parameterized dependency are incorporated at once - but only when they are available.
return incorporateIntoParameterizedDependencyIfAllArgumentsAreProperTypes(boundRight,
firstBounds, nextBounds);
firstBounds, nextBounds, properTypesByInferenceVariable);
}
if (u.isProperType(true)) {
InferenceVariable alpha = boundLeft.left;
Expand All @@ -783,7 +784,7 @@ private ConstraintTypeFormula combineSameSameWithProperType(TypeBound boundLeft,
return null;
}

private ConstraintTypeFormula combineSameSubSuper(TypeBound boundS, TypeBound boundT, TypeBound[] firstBounds, TypeBound[] nextBounds) {
private ConstraintTypeFormula combineSameSubSuper(TypeBound boundS, TypeBound boundT, TypeBound[] firstBounds, TypeBound[] nextBounds, Map<InferenceVariable,TypeBound> properTypesByInferenceVariable) {
// α = S and α <: T imply ⟨S <: T⟩
// α = S and T <: α imply ⟨T <: S⟩
InferenceVariable alpha = boundS.left;
Expand All @@ -810,21 +811,21 @@ private ConstraintTypeFormula combineSameSubSuper(TypeBound boundS, TypeBound bo
return ConstraintTypeFormula.create(t, s, boundT.relation, boundT.isSoft||boundS.isSoft);
}
}
return combineSameSubSuperWithProperType(boundS, boundT, alpha, firstBounds, nextBounds);
return combineSameSubSuperWithProperType(boundS, boundT, alpha, firstBounds, nextBounds, properTypesByInferenceVariable);
}

// pre: boundLeft.left != boundRight.left
// pre: boundLeft.left != boundRight.right
private ConstraintTypeFormula combineSameSubSuperWithProperType(TypeBound boundLeft, TypeBound boundRight,
InferenceVariable alpha, TypeBound[] firstBounds, TypeBound[] nextBounds) {
InferenceVariable alpha, TypeBound[] firstBounds, TypeBound[] nextBounds, Map<InferenceVariable,TypeBound> properTypesByInferenceVariable) {
// α = U and S <: T imply ⟨S[α:=U] <: T[α:=U]⟩
TypeBinding u = boundLeft.right;

if (enableOptimizationForBug543480 && isParameterizedDependency(boundRight)) {
// Performance optimization: do not incorporate arguments one by one, which yielt 2^n new bounds (n=number of type arguments) in the past.
// Instead, all arguments of a parameterized dependency are incorporated at once - but only when they are available.
return incorporateIntoParameterizedDependencyIfAllArgumentsAreProperTypes(boundRight,
firstBounds, nextBounds);
firstBounds, nextBounds, properTypesByInferenceVariable);
}
if (u.isProperType(true)) {
boolean substitute = TypeBinding.equalsEquals(alpha, boundRight.left);
Expand Down Expand Up @@ -871,20 +872,19 @@ private boolean isParameterizedDependency(TypeBound typeBound) {
}

private ConstraintTypeFormula incorporateIntoParameterizedDependencyIfAllArgumentsAreProperTypes(TypeBound typeBound,
TypeBound[] firstBounds, TypeBound[] nextBounds) {
Collection<TypeBound> properTypesForAllInferenceVariables = getProperTypesForAllInferenceVariablesOrNull((ParameterizedTypeBinding)typeBound.right, firstBounds, nextBounds);
TypeBound[] firstBounds, TypeBound[] nextBounds, Map<InferenceVariable,TypeBound> properTypesByInferenceVariable) {
if(properTypesByInferenceVariable.isEmpty()) {
return null;
}
Collection<TypeBound> properTypesForAllInferenceVariables = getProperTypesForAllInferenceVariablesOrNull((ParameterizedTypeBinding)typeBound.right, firstBounds, nextBounds, properTypesByInferenceVariable);
if (null != properTypesForAllInferenceVariables) {
return combineWithProperTypes(properTypesForAllInferenceVariables, typeBound);
}
return null;
}

private Collection<TypeBound> getProperTypesForAllInferenceVariablesOrNull(ParameterizedTypeBinding parameterizedType,
TypeBound[] firstBounds, TypeBound[] nextBounds) {
final Map<InferenceVariable,TypeBound> properTypesByInferenceVariable = properTypesByInferenceVariable(firstBounds, nextBounds);
if(properTypesByInferenceVariable.size() == 0) {
return null;
}
TypeBound[] firstBounds, TypeBound[] nextBounds, Map<InferenceVariable,TypeBound> properTypesByInferenceVariable) {
final Set<InferenceVariable> inferenceVariables = getInferenceVariables(parameterizedType);
if(properTypesByInferenceVariable.keySet().containsAll(inferenceVariables)) {
return properTypesByInferenceVariable.values();
Expand Down
Loading