Skip to content

Commit da71465

Browse files
authored
edit_prediction_context: Minor optimization of text similarity + some renames (#38941)
Release Notes: - N/A
1 parent bcc8149 commit da71465

File tree

10 files changed

+198
-176
lines changed

10 files changed

+198
-176
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,7 @@ futures-lite = "1.13"
511511
git2 = { version = "0.20.1", default-features = false }
512512
globset = "0.4"
513513
handlebars = "4.3"
514+
hashbrown = "0.15.3"
514515
heck = "0.5"
515516
heed = { version = "0.21.0", features = ["read-txn-no-tls"] }
516517
hex = "0.4.3"

crates/cloud_llm_client/src/predict_edits_v3.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,13 @@ pub struct ReferencedDeclaration {
103103
/// Index within `signatures`.
104104
#[serde(skip_serializing_if = "Option::is_none", default)]
105105
pub parent_index: Option<usize>,
106-
pub score_components: ScoreComponents,
106+
pub score_components: DeclarationScoreComponents,
107107
pub signature_score: f32,
108108
pub declaration_score: f32,
109109
}
110110

111111
#[derive(Debug, Clone, Serialize, Deserialize)]
112-
pub struct ScoreComponents {
112+
pub struct DeclarationScoreComponents {
113113
pub is_same_file: bool,
114114
pub is_referenced_nearby: bool,
115115
pub is_referenced_in_breadcrumb: bool,
@@ -119,12 +119,12 @@ pub struct ScoreComponents {
119119
pub reference_line_distance: u32,
120120
pub declaration_line_distance: u32,
121121
pub declaration_line_distance_rank: usize,
122-
pub containing_range_vs_item_jaccard: f32,
123-
pub containing_range_vs_signature_jaccard: f32,
122+
pub excerpt_vs_item_jaccard: f32,
123+
pub excerpt_vs_signature_jaccard: f32,
124124
pub adjacent_vs_item_jaccard: f32,
125125
pub adjacent_vs_signature_jaccard: f32,
126-
pub containing_range_vs_item_weighted_overlap: f32,
127-
pub containing_range_vs_signature_weighted_overlap: f32,
126+
pub excerpt_vs_item_weighted_overlap: f32,
127+
pub excerpt_vs_signature_weighted_overlap: f32,
128128
pub adjacent_vs_item_weighted_overlap: f32,
129129
pub adjacent_vs_signature_weighted_overlap: f32,
130130
}

crates/cloud_zeta2_prompt/src/cloud_zeta2_prompt.rs

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pub struct PlannedSnippet<'a> {
7070
}
7171

7272
#[derive(EnumIter, Clone, Copy, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)]
73-
pub enum SnippetStyle {
73+
pub enum DeclarationStyle {
7474
Signature,
7575
Declaration,
7676
}
@@ -84,10 +84,10 @@ pub struct SectionLabels {
8484
impl<'a> PlannedPrompt<'a> {
8585
/// Greedy one-pass knapsack algorithm to populate the prompt plan. Does the following:
8686
///
87-
/// Initializes a priority queue by populating it with each snippet, finding the SnippetStyle
88-
/// that minimizes `score_density = score / snippet.range(style).len()`. When a "signature"
89-
/// snippet is popped, insert an entry for the "declaration" variant that reflects the cost of
90-
/// upgrade.
87+
/// Initializes a priority queue by populating it with each snippet, finding the
88+
/// DeclarationStyle that minimizes `score_density = score / snippet.range(style).len()`. When a
89+
/// "signature" snippet is popped, insert an entry for the "declaration" variant that reflects
90+
/// the cost of upgrade.
9191
///
9292
/// TODO: Implement an early halting condition. One option might be to have another priority
9393
/// queue where the score is the size, and update it accordingly. Another option might be to
@@ -131,13 +131,13 @@ impl<'a> PlannedPrompt<'a> {
131131
struct QueueEntry {
132132
score_density: OrderedFloat<f32>,
133133
declaration_index: usize,
134-
style: SnippetStyle,
134+
style: DeclarationStyle,
135135
}
136136

137137
// Initialize priority queue with the best score for each snippet.
138138
let mut queue: BinaryHeap<QueueEntry> = BinaryHeap::new();
139139
for (declaration_index, declaration) in request.referenced_declarations.iter().enumerate() {
140-
let (style, score_density) = SnippetStyle::iter()
140+
let (style, score_density) = DeclarationStyle::iter()
141141
.map(|style| {
142142
(
143143
style,
@@ -186,7 +186,7 @@ impl<'a> PlannedPrompt<'a> {
186186
this.budget_used += additional_bytes;
187187
this.add_parents(&mut included_parents, additional_parents);
188188
let planned_snippet = match queue_entry.style {
189-
SnippetStyle::Signature => {
189+
DeclarationStyle::Signature => {
190190
let Some(text) = declaration.text.get(declaration.signature_range.clone())
191191
else {
192192
return Err(anyhow!(
@@ -203,7 +203,7 @@ impl<'a> PlannedPrompt<'a> {
203203
text_is_truncated: declaration.text_is_truncated,
204204
}
205205
}
206-
SnippetStyle::Declaration => PlannedSnippet {
206+
DeclarationStyle::Declaration => PlannedSnippet {
207207
path: declaration.path.clone(),
208208
range: declaration.range.clone(),
209209
text: &declaration.text,
@@ -213,19 +213,21 @@ impl<'a> PlannedPrompt<'a> {
213213
this.snippets.push(planned_snippet);
214214

215215
// When a Signature is consumed, insert an entry for Definition style.
216-
if queue_entry.style == SnippetStyle::Signature {
217-
let signature_size = declaration_size(&declaration, SnippetStyle::Signature);
218-
let declaration_size = declaration_size(&declaration, SnippetStyle::Declaration);
219-
let signature_score = declaration_score(&declaration, SnippetStyle::Signature);
220-
let declaration_score = declaration_score(&declaration, SnippetStyle::Declaration);
216+
if queue_entry.style == DeclarationStyle::Signature {
217+
let signature_size = declaration_size(&declaration, DeclarationStyle::Signature);
218+
let declaration_size =
219+
declaration_size(&declaration, DeclarationStyle::Declaration);
220+
let signature_score = declaration_score(&declaration, DeclarationStyle::Signature);
221+
let declaration_score =
222+
declaration_score(&declaration, DeclarationStyle::Declaration);
221223

222224
let score_diff = declaration_score - signature_score;
223225
let size_diff = declaration_size.saturating_sub(signature_size);
224226
if score_diff > 0.0001 && size_diff > 0 {
225227
queue.push(QueueEntry {
226228
declaration_index: queue_entry.declaration_index,
227229
score_density: OrderedFloat(score_diff / (size_diff as f32)),
228-
style: SnippetStyle::Declaration,
230+
style: DeclarationStyle::Declaration,
229231
});
230232
}
231233
}
@@ -510,20 +512,20 @@ impl<'a> PlannedPrompt<'a> {
510512
}
511513
}
512514

513-
fn declaration_score_density(declaration: &ReferencedDeclaration, style: SnippetStyle) -> f32 {
515+
fn declaration_score_density(declaration: &ReferencedDeclaration, style: DeclarationStyle) -> f32 {
514516
declaration_score(declaration, style) / declaration_size(declaration, style) as f32
515517
}
516518

517-
fn declaration_score(declaration: &ReferencedDeclaration, style: SnippetStyle) -> f32 {
519+
fn declaration_score(declaration: &ReferencedDeclaration, style: DeclarationStyle) -> f32 {
518520
match style {
519-
SnippetStyle::Signature => declaration.signature_score,
520-
SnippetStyle::Declaration => declaration.declaration_score,
521+
DeclarationStyle::Signature => declaration.signature_score,
522+
DeclarationStyle::Declaration => declaration.declaration_score,
521523
}
522524
}
523525

524-
fn declaration_size(declaration: &ReferencedDeclaration, style: SnippetStyle) -> usize {
526+
fn declaration_size(declaration: &ReferencedDeclaration, style: DeclarationStyle) -> usize {
525527
match style {
526-
SnippetStyle::Signature => declaration.signature_range.len(),
527-
SnippetStyle::Declaration => declaration.text.len(),
528+
DeclarationStyle::Signature => declaration.signature_range.len(),
529+
DeclarationStyle::Declaration => declaration.text.len(),
528530
}
529531
}

crates/edit_prediction_context/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ cloud_llm_client.workspace = true
1818
collections.workspace = true
1919
futures.workspace = true
2020
gpui.workspace = true
21+
hashbrown.workspace = true
2122
itertools.workspace = true
2223
language.workspace = true
2324
log.workspace = true

0 commit comments

Comments
 (0)