Skip to content

Commit 438b5bb

Browse files
committed
[clangd] Use ML Code completion ranking as default.
This makes code completion use a Decision Forest based ranking algorithm to rank completion candidates. [Esitmated 6% accuracy boost]. This was previously hidden behind the flag --ranking-model=decision_forest. This patch makes it the default ranking algorithm. Note: this is a generic model, not specialized for any particular project. clangd does not collect or upload data to train code completion. Also treat Keywords separately as they are not recorded by the training set generator. Differential Revision: https://reviews.llvm.org/D96353
1 parent 14be3f0 commit 438b5bb

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

clang-tools-extra/clangd/CodeComplete.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ struct CodeCompleteOptions {
133133
enum CodeCompletionRankingModel {
134134
Heuristics,
135135
DecisionForest,
136-
} RankingModel = Heuristics;
136+
} RankingModel = DecisionForest;
137137

138138
/// Callback used to score a CompletionCandidate if DecisionForest ranking
139139
/// model is enabled.

clang-tools-extra/clangd/Quality.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -580,12 +580,16 @@ evaluateDecisionForest(const SymbolQualitySignals &Quality,
580580
// multiplciative boost (like NameMatch). This allows us to weigh the
581581
// prediciton score and NameMatch appropriately.
582582
Scores.ExcludingName = pow(Base, Evaluate(E));
583-
// NeedsFixIts is not part of the DecisionForest as generating training
584-
// data that needs fixits is not-feasible.
583+
// Following cases are not part of the generated training dataset:
584+
// - Symbols with `NeedsFixIts`.
585+
// - Forbidden symbols.
586+
// - Keywords: Dataset contains only macros and decls.
585587
if (Relevance.NeedsFixIts)
586588
Scores.ExcludingName *= 0.5;
587589
if (Relevance.Forbidden)
588590
Scores.ExcludingName *= 0;
591+
if (Quality.Category == SymbolQualitySignals::Keyword)
592+
Scores.ExcludingName *= 4;
589593

590594
// NameMatch should be a multiplier on total score to support rescoring.
591595
Scores.Total = Relevance.NameMatch * Scores.ExcludingName;

clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -647,13 +647,13 @@ TEST(CompletionTest, ScopedWithFilter) {
647647
}
648648

649649
TEST(CompletionTest, ReferencesAffectRanking) {
650-
auto Results = completions("int main() { abs^ }", {ns("absl"), func("absb")});
651-
EXPECT_THAT(Results.Completions,
652-
HasSubsequence(Named("absb"), Named("absl")));
653-
Results = completions("int main() { abs^ }",
654-
{withReferences(10000, ns("absl")), func("absb")});
655-
EXPECT_THAT(Results.Completions,
656-
HasSubsequence(Named("absl"), Named("absb")));
650+
EXPECT_THAT(completions("int main() { abs^ }", {func("absA"), func("absB")})
651+
.Completions,
652+
HasSubsequence(Named("absA"), Named("absB")));
653+
EXPECT_THAT(completions("int main() { abs^ }",
654+
{func("absA"), withReferences(1000, func("absB"))})
655+
.Completions,
656+
HasSubsequence(Named("absB"), Named("absA")));
657657
}
658658

659659
TEST(CompletionTest, ContextWords) {

0 commit comments

Comments
 (0)