Skip to content

[WIP] Quick-and-dirty speed-up for certain large switches #21981

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

Closed
wants to merge 1 commit into from
Closed
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
81 changes: 80 additions & 1 deletion lib/Sema/TypeCheckSwitchStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,85 @@ namespace {
}
}

/// Naive compacting of successive spaces that share a prefix.
///
/// This turns DISJOINT((.a1, .b1), (.a1, .b2), (.a2, .b1)) into
/// DISJOINT((.a1, DISJOINT(.b1, .b2)), (.a2, .b1)).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be generally beneficial since identifying empty constructors is faster than disjuncts (single empty element in constructor makes whole constructor empty), which limits space explosion, right? (Sorry it's been a little while)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like that. The main thing is that minus is expensive on constructors with multiple elements. In pseudocode:

> let s = (A, B) - (.a1, .b1)
s = (A - .a1, B) | (A, B - .b1)
> s - (.a1, .b2)
(A - .a1, B) | (A - .a1, B - .b1) | (A, B - .b1 - .b2)

This gets even worse with higher arity. With this change, we get:

> (A, B) - (.a1, .b1 | .b2)
(A - .a1, B) | (A, B - .b1 - .b2)

i.e. the exponential explosion is much slower.

Someone following along may have noticed another potential optimization: (A - .a1, B - .b1) in the first example is a subset of (A - .a1, B) and can therefore be dropped from the disjoint. We aren't checking that when forming disjoints but maybe we should.

Space compact() {
if (getKind() != SpaceKind::Disjunct)
return *this;

SmallVector<Space, 4> newSpaces;

auto startingPoint = getSpaces().begin();
while (startingPoint != getSpaces().end()) {
auto lastOfSet = std::adjacent_find(startingPoint,
getSpaces().end(),
[](const Space &left,
const Space &right) -> bool {
switch (PairSwitch(left.getKind(), right.getKind())) {
PAIRCASE (SpaceKind::Constructor, SpaceKind::Constructor): {
assert(left.getType()->isEqual(right.getType()) &&
"mismatched constructor types");
if (left.getHead() != right.getHead())
return true;

auto leftSpaces = left.getSpaces().begin();
auto leftEnd = left.getSpaces().end();
auto rightSpaces = right.getSpaces().begin();

// Note: assumes the two spaces are the same length, based on
// checking the type and constructor label.
auto mismatchPair = std::mismatch(leftSpaces, leftEnd,
rightSpaces);
if (mismatchPair.first == leftEnd) {
// The two spaces are identical. Unlikely, but possible.
return false;
}
if (std::next(mismatchPair.first) == leftEnd) {
// The two spaces are identical in all but the last argument.
// That means it's part of the same set.
return false;
}
return true;
}
default:
return true;
}
});

auto afterSet = lastOfSet;
if (afterSet != getSpaces().end())
afterSet = std::next(lastOfSet);
SWIFT_DEFER { startingPoint = afterSet; };

if (afterSet == std::next(startingPoint)) {
newSpaces.push_back(*startingPoint);
continue;
}

SmallVector<Space, 4> varyingElemSpaces;
for (const Space &space : llvm::make_range(startingPoint, afterSet)) {
const Space *lastSubSpace = nullptr;
// FIXME: std::forward_list is working against us here.
for (const Space &subSpace : space.getSpaces())
lastSubSpace = &subSpace;
assert(lastSubSpace &&
"should only get here when there's something to vary");
varyingElemSpaces.push_back(*lastSubSpace);
}

SmallVector<Space, 4> subSpaces(startingPoint->getSpaces().begin(),
startingPoint->getSpaces().end());
subSpaces.back() = Space::forDisjunct(varyingElemSpaces);

newSpaces.push_back(Space::forConstructor(startingPoint->getType(),
startingPoint->getHead(),
subSpaces));
}
return Space::forDisjunct(newSpaces);
}

// Decompose a type into its component spaces.
static void decompose(TypeChecker &TC, const DeclContext *DC, Type tp,
SmallVectorImpl<Space> &arr) {
Expand Down Expand Up @@ -947,7 +1026,7 @@ namespace {
}

Space totalSpace = Space::forType(subjectType, Identifier());
Space coveredSpace = Space::forDisjunct(spaces);
Space coveredSpace = Space::forDisjunct(spaces).compact();

unsigned minusCount = 0;
auto diff = totalSpace.minus(coveredSpace, TC, DC, &minusCount);
Expand Down