[CodeComplete] Compute type relations for global cached results

Computing the type relation for every item in the code completion cache is way to expensive (~4x slowdown for global completion that imports `SwiftUI`). Instead, compute a type’s supertypes (protocol conformances and superclasses) once and write their USRs to the cache. To compute a type relation we can then check if the contextual type is in the completion item’s supertypes.

This reduces the overhead of computing the type relations (again global completion that imports `SwiftUI`) to ~6% – measured by instructions executed.

Technically, we might miss some conversions like
- retroactive conformances inside another module (because we can’t cache them if that other module isn’t imported)
- complex generic conversions (just too complicated to model using USRs)

Because of this, we never report an `unrelated` type relation for global items but always default to `unknown`.

But I believe this change covers the most common cases and is a good tradeoff between accuracy and performance.

rdar://83846531
This commit is contained in:
Alex Hoppen
2022-03-01 09:16:23 +01:00
parent 6d9790511e
commit 640cfac61c
21 changed files with 997 additions and 159 deletions

View File

@@ -133,7 +133,7 @@ bool SourceKit::CodeCompletion::addCustomCompletions(
CodeCompletionString::create(sink.allocator, chunk);
auto *contextFreeResult =
ContextFreeCodeCompletionResult::createPatternOrBuiltInOperatorResult(
sink.allocator, CodeCompletionResultKind::Pattern, completionString,
sink.swiftSink, CodeCompletionResultKind::Pattern, completionString,
CodeCompletionOperatorKind::None, /*BriefDocComment=*/"",
CodeCompletionResultType::unknown(),
ContextFreeNotRecommendedReason::None,
@@ -142,7 +142,7 @@ bool SourceKit::CodeCompletion::addCustomCompletions(
*contextFreeResult, SemanticContextKind::Local,
CodeCompletionFlairBit::ExpressionSpecific,
/*NumBytesToErase=*/0, /*TypeContext=*/nullptr, /*DC=*/nullptr,
ContextualNotRecommendedReason::None,
/*USRTypeContext=*/nullptr, ContextualNotRecommendedReason::None,
CodeCompletionDiagnosticSeverity::None, /*DiagnosticMessage=*/"");
CompletionBuilder builder(sink, *swiftResult);