mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
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
55 lines
2.0 KiB
C++
55 lines
2.0 KiB
C++
//===--- CodeCompletionConsumer.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_CODECOMPLETIONCONSUMER
|
|
#define SWIFT_IDE_CODECOMPLETIONCONSUMER
|
|
|
|
#include "swift/IDE/CodeCompletionContext.h"
|
|
#include "swift/Parse/CodeCompletionCallbacks.h"
|
|
|
|
namespace swift {
|
|
namespace ide {
|
|
|
|
struct RequestedCachedModule;
|
|
|
|
/// An abstract base class for consumers of code completion results.
|
|
/// \see \c SimpleCachingCodeCompletionConsumer.
|
|
class CodeCompletionConsumer {
|
|
public:
|
|
virtual ~CodeCompletionConsumer() {}
|
|
virtual void
|
|
handleResultsAndModules(CodeCompletionContext &context,
|
|
ArrayRef<RequestedCachedModule> requestedModules,
|
|
const ExpectedTypeContext *TypeContext,
|
|
const DeclContext *DC) = 0;
|
|
};
|
|
|
|
/// A simplified code completion consumer interface that clients can use to get
|
|
/// CodeCompletionResults with automatic caching of top-level completions from
|
|
/// imported modules.
|
|
struct SimpleCachingCodeCompletionConsumer : public CodeCompletionConsumer {
|
|
|
|
// Implement the CodeCompletionConsumer interface.
|
|
void handleResultsAndModules(CodeCompletionContext &context,
|
|
ArrayRef<RequestedCachedModule> requestedModules,
|
|
const ExpectedTypeContext *TypeContext,
|
|
const DeclContext *DCForModules) override;
|
|
|
|
/// Clients should override this method to receive \p Results.
|
|
virtual void handleResults(CodeCompletionContext &context) = 0;
|
|
};
|
|
|
|
} // end namespace ide
|
|
} // end namespace swift
|
|
|
|
#endif // SWIFT_IDE_CODECOMPLETIONCONSUMER
|