Files
swift-mirror/include/swift/IDE/CodeCompletionResultSink.h
Alex Hoppen 94351a2f82 [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
2022-03-02 23:13:09 +01:00

93 lines
3.1 KiB
C++

//===--- CodeCompletionResultSink.h ---------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_IDE_CODECOMPLETIONRESULTSINK
#define SWIFT_IDE_CODECOMPLETIONRESULTSINK
#include "swift/IDE/CodeCompletionResult.h"
namespace swift {
namespace ide {
struct CodeCompletionResultSink {
using AllocatorPtr = std::shared_ptr<llvm::BumpPtrAllocator>;
/// The allocator used to allocate results "native" to this sink.
AllocatorPtr Allocator;
/// Allocators that keep alive "foreign" results imported into this sink from
/// other sinks.
std::vector<AllocatorPtr> ForeignAllocators;
/// Whether to annotate the results with XML.
bool annotateResult = false;
/// Whether to emit object literals if desired.
bool includeObjectLiterals = true;
/// Whether to emit type initializers in addition to type names in expression
/// position.
bool addInitsToTopLevel = false;
/// Whether to perform "call pettern heuristics".
bool enableCallPatternHeuristics = false;
/// Whether to include an item without any default arguments.
bool addCallWithNoDefaultArgs = true;
private:
/// Whether the code completion results computed for this sink are intended to
/// only be stored in the cache. In this case no contextual information is
/// computed and all types in \c ContextFreeCodeCompletionResult should be
/// USR-based instead of AST-based.
USRBasedTypeArena *USRTypeArena = nullptr;
public:
std::vector<CodeCompletionResult *> Results;
/// A single-element cache for module names stored in Allocator, keyed by a
/// clang::Module * or swift::ModuleDecl *.
std::pair<void *, NullTerminatedStringRef> LastModule;
CodeCompletionResultSink()
: Allocator(std::make_shared<llvm::BumpPtrAllocator>()) {}
llvm::BumpPtrAllocator &getAllocator() { return *Allocator; }
/// Marks the sink as producing results for the code completion cache.
/// In this case the produced results will not contain any contextual
/// information and all types in the \c ContextFreeCodeCompletionResult are
/// USR-based.
void setProduceContextFreeResults(USRBasedTypeArena &USRTypeArena) {
this->USRTypeArena = &USRTypeArena;
}
/// See \c setProduceContextFreeResults.
bool shouldProduceContextFreeResults() const {
return USRTypeArena != nullptr;
}
/// If \c shouldProduceContextFreeResults is \c true, returns the arena in
/// which the USR-based types of the \c ContextFreeCodeCompletionResult should
/// be stored.
USRBasedTypeArena &getUSRTypeArena() const {
assert(USRTypeArena != nullptr &&
"Must only be called if shouldProduceContextFreeResults is true");
return *USRTypeArena;
}
};
} // end namespace ide
} // end namespace swift
#endif // SWIFT_IDE_CODECOMPLETIONRESULTSINK