Skip to content

Commit 9ec5b21

Browse files
author
Jesus Lapastora Núñez
committed
Do not clone nested_chlidren on each get
`.clone()` as an escape hatch is a big mistake.
1 parent 0ac58a9 commit 9ec5b21

File tree

1 file changed

+14
-12
lines changed
  • engine/baml-lib/ast/src/ast/baml_vis

1 file changed

+14
-12
lines changed

engine/baml-lib/ast/src/ast/baml_vis/graph.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ impl<'index, 'pre> GraphBuilder<'index, 'pre> {
238238
}
239239

240240
// NOTE: we may wont to make the caches have more lifetime
241-
// than build so that we can return &'cache [NodeId] after restoring.
241+
// than build so that we can return &'cache [NodeId] after writing.
242242
fn build_header(
243243
&mut self,
244244
hid: Hid,
@@ -254,8 +254,12 @@ impl<'index, 'pre> GraphBuilder<'index, 'pre> {
254254
return (entry, exits);
255255
}
256256
let header = self.by_hid[&hid];
257-
let md_children = self.md_children.get(&hid).cloned().unwrap_or_default();
258-
let nested_children = self.nested_children.get(&hid).cloned().unwrap_or_default();
257+
let md_children = self.md_children.get(&hid).map(Vec::as_slice).unwrap_or(&[]);
258+
let nested_children = self
259+
.nested_children
260+
.get(&hid)
261+
.map(Vec::as_slice)
262+
.unwrap_or(&[]);
259263
let has_md = !md_children.is_empty();
260264
let has_nested = !nested_children.is_empty();
261265
let is_branching = header.label_kind == HeaderLabelKind::If;
@@ -305,15 +309,14 @@ impl<'index, 'pre> GraphBuilder<'index, 'pre> {
305309
cluster: parent_cluster,
306310
});
307311
self.header_entry.insert(hid, node_id);
308-
let mut exits = vec![node_id];
309-
if md_children.len() == 1 {
312+
let exits = if md_children.len() == 1 {
310313
let (c_entry, c_exits) =
311314
self.build_header(md_children[0], visited_scopes, parent_cluster);
312315
self.graph.edges.push(Edge {
313316
from: node_id,
314317
to: c_entry,
315318
});
316-
exits = c_exits;
319+
c_exits
317320
} else if nested_children.len() == 1 {
318321
let child_root_hid = nested_children[0];
319322
let child_scope = self.by_hid[&child_root_hid].scope;
@@ -324,9 +327,9 @@ impl<'index, 'pre> GraphBuilder<'index, 'pre> {
324327
from: node_id,
325328
to: c_entry,
326329
});
327-
exits = c_exits;
328-
}
329-
self.header_exits.insert(hid, exits.clone());
330+
c_exits
331+
};
332+
self.header_exits.insert(hid, exits);
330333
return (node_id, exits);
331334
}
332335

@@ -412,11 +415,11 @@ impl<'index, 'pre> GraphBuilder<'index, 'pre> {
412415
});
413416

414417
// Merge markdown children and direct nested roots, preserving each list's internal order
415-
let items_merged = merge_by_pos(&self.by_hid, &md_children, &nested_children);
418+
let items_merged = merge_by_pos(self.by_hid, &md_children, &nested_children);
416419

417420
let mut first_rep: Option<_> = None;
418421
let mut prev_exits: Option<_> = None;
419-
for child_hid in items_merged.into_iter() {
422+
for child_hid in items_merged {
420423
// If this child is a direct nested root for the current container, prebuild its scope
421424
// inside this container's cluster and use the scope's final exits.
422425
let mut prebuilt_scope_last_exits = None;
@@ -477,7 +480,6 @@ impl<'index, 'pre> GraphBuilder<'index, 'pre> {
477480
) {
478481
if let Some(callees) = self.index.header_calls.get(&hid) {
479482
for callee in callees {
480-
// TODO: get rid of `callee.clone()`
481483
if let Some(&cached_id) = self.call_node_cache.get(&(hid, callee)) {
482484
self.graph.edges.push(Edge {
483485
from: cached_id,

0 commit comments

Comments
 (0)