vdbe: move comments if instructions were moved around in emit_constant_insns()

This commit is contained in:
Jussi Saurio
2025-04-23 13:11:39 +03:00
parent 029e5eddde
commit c3441f9685
2 changed files with 28 additions and 13 deletions

View File

@@ -1,7 +1,6 @@
use std::{
cell::Cell,
cmp::Ordering,
collections::HashMap,
rc::{Rc, Weak},
sync::Arc,
};
@@ -35,7 +34,7 @@ pub struct ProgramBuilder {
// Bitmask of cursors that have emitted a SeekRowid instruction.
seekrowid_emitted_bitmask: u64,
// map of instruction index to manual comment (used in EXPLAIN only)
comments: Option<HashMap<InsnReference, &'static str>>,
comments: Option<Vec<(InsnReference, &'static str)>>,
pub parameters: Parameters,
pub result_columns: Vec<ResultSetColumn>,
pub table_references: Vec<TableReference>,
@@ -89,7 +88,7 @@ impl ProgramBuilder {
label_to_resolved_offset: Vec::with_capacity(opts.approx_num_labels),
seekrowid_emitted_bitmask: 0,
comments: if opts.query_mode == QueryMode::Explain {
Some(HashMap::new())
Some(Vec::new())
} else {
None
},
@@ -247,7 +246,7 @@ impl ProgramBuilder {
pub fn add_comment(&mut self, insn_index: BranchOffset, comment: &'static str) {
if let Some(comments) = &mut self.comments {
comments.insert(insn_index.to_offset_int(), comment);
comments.push((insn_index.to_offset_int(), comment));
}
}
@@ -298,6 +297,18 @@ impl ProgramBuilder {
*resolved_offset = Some((new_offset, *target));
}
}
// Fix comments to refer to new locations
if let Some(comments) = &mut self.comments {
for (old_offset, _) in comments.iter_mut() {
let new_offset = self
.insns
.iter()
.position(|(_, _, index)| *old_offset == *index as u32)
.expect("comment must exist") as u32;
*old_offset = new_offset;
}
}
}
pub fn offset(&self) -> BranchOffset {

View File

@@ -367,7 +367,7 @@ pub struct Program {
pub insns: Vec<(Insn, InsnFunction)>,
pub cursor_ref: Vec<(Option<String>, CursorType)>,
pub database_header: Arc<SpinLock<DatabaseHeader>>,
pub comments: Option<HashMap<InsnReference, &'static str>>,
pub comments: Option<Vec<(InsnReference, &'static str)>>,
pub parameters: crate::parameters::Parameters,
pub connection: Weak<Connection>,
pub n_change: Cell<i64>,
@@ -557,10 +557,11 @@ fn trace_insn(program: &Program, addr: InsnReference, insn: &Insn) {
addr,
insn,
String::new(),
program
.comments
.as_ref()
.and_then(|comments| comments.get(&{ addr }).copied())
program.comments.as_ref().and_then(|comments| comments
.iter()
.find(|(offset, _)| *offset == addr)
.map(|(_, comment)| comment)
.copied())
)
);
}
@@ -571,10 +572,13 @@ fn print_insn(program: &Program, addr: InsnReference, insn: &Insn, indent: Strin
addr,
insn,
indent,
program
.comments
.as_ref()
.and_then(|comments| comments.get(&{ addr }).copied()),
program.comments.as_ref().and_then(|comments| {
comments
.iter()
.find(|(offset, _)| *offset == addr)
.map(|(_, comment)| comment)
.copied()
}),
);
w.push_str(&s);
}