From 2ec51cb7cb522a1e5e8be4512a0f33f6edbafab8 Mon Sep 17 00:00:00 2001 From: Josh Date: Sun, 15 Nov 2026 12:04:19 -0700 Subject: [PATCH] fix(engine): a full link chart skips what it can't draw, and a self-link is one end (#1947) From #2146's review, after it was merged: - A feature related to itself was counted as two new ends. With the chart at 2,999 and the feature not in it yet, its link to itself was left out although it needs only one place. It's now counted once. - A full chart still went through every destination of every feature: one with no room left tried each and drew none. With 1,500 pairs filling the chart, a later relationship of 10,000 features sharing one key with 10,000 others made 100 million tries. A feature the chart has no room for is now skipped whole, and one already in it goes straight to the destinations the chart holds (or to itself), which are the only ones it can still link to. What's drawn and what the notice says are unchanged by the second; the first draws the one link it left out. --- crates/engine/src/cmd/sharing.rs | 35 +++++++++++++++++++++++++++++--- crates/engine/src/tests.rs | 21 +++++++++++++++++++ 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/crates/engine/src/cmd/sharing.rs b/crates/engine/src/cmd/sharing.rs index fbd92b77b4..f125ef9553 100644 --- a/crates/engine/src/cmd/sharing.rs +++ b/crates/engine/src/cmd/sharing.rs @@ -1380,13 +1380,17 @@ fn link_chart(s: &Session, rel: Option<&str>) -> Result<(String, Option) } // The features a key relates to are counted once, however many features share it. let mut counted: HashSet = HashSet::new(); + // Where a key's destinations already in the chart sit among its destinations, found when + // the chart has no room for another one and kept while it stays that size: a full chart + // then looks at these only, not at every destination of every feature. + let mut in_chart: HashMap)> = HashMap::new(); for orow in 0..o.dataset.features.len() { let Some(key) = key_of(&o.dataset, orow, ok) else { continue }; let Some(drows) = by_key.get(&key) else { continue }; related.insert((om, o.id, orow)); // Counted in 64 bits: relations can pass `usize` in the browser (32 bits). relations += drows.len() as u64; - if counted.insert(key) { + if counted.insert(key.clone()) { related.extend(drows.iter().map(|&drow| (dm, d.id, drow))); } if links.len() >= MAX_LINKS { @@ -1398,23 +1402,48 @@ fn link_chart(s: &Session, rel: Option<&str>) -> Result<(String, Option) // Up to 200 links drawn from it: destinations the chart has no room for don't count // toward them, so a later one it can draw still is. let mut a = index.get(&(om, o.id, orow)).copied(); + // A feature the chart has no room for can't be drawn, so none of its links can be. + if a.is_none() && nodes.len() >= MAX_NODES { + continue; + } let mut drawn_here = 0; - for &drow in drows { + let mut i = 0; + while i < drows.len() { if drawn_here >= MAX_FEATURE_LINKS || links.len() >= MAX_LINKS { break; } - let new = usize::from(a.is_none()) + usize::from(!index.contains_key(&(dm, d.id, drow))); + let drow = drows[i]; + // A feature related to itself is one end, not two. + let origin_new = a.is_none(); + let dest_new = !index.contains_key(&(dm, d.id, drow)) && !(origin_new && (om, o.id, orow) == (dm, d.id, drow)); + let new = usize::from(origin_new) + usize::from(dest_new); if nodes.len() + new > MAX_NODES { + // No room for a destination the chart doesn't hold: on to the next one it + // does, or to the feature itself. + let (at, rows) = in_chart.entry(key.clone()).or_insert((usize::MAX, Vec::new())); + if *at != nodes.len() { + *rows = (0..drows.len()).filter(|&j| index.contains_key(&(dm, d.id, drows[j]))).collect(); + *at = nodes.len(); + } + let next_held = rows.get(rows.partition_point(|&j| j <= i)).copied(); + let next_self = + if origin_new && (om, o.id) == (dm, d.id) { drows.binary_search(&orow).ok().filter(|&j| j > i) } else { None }; + match next_held.into_iter().chain(next_self).min() { + Some(j) => i = j, + None => break, + } continue; } let (Some(from), Some(to)) = (a.or_else(|| node(&mut index, &mut nodes, om, o, orow, gi)), node(&mut index, &mut nodes, dm, d, drow, gi)) else { + i += 1; continue; }; a = Some(from); links.push(json!({"source": from, "target": to, "relationship": r.name})); drawn_here += 1; + i += 1; } } } diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index f7d5a62b3c..fd208d3d96 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -12625,6 +12625,27 @@ fn a_link_chart_says_what_its_limits_leave_out() { t.run("share.linkChart", &json!({})).unwrap(); // First draws 1 link (2 features), Fill 1,499 (2,997), and Last its one link to x (1 more). assert_eq!((drawn("Relationships").0, drawn("Relationships").1), (1501, 3000)); + // A feature related to itself is one end, not two: with the chart at 2,999, its link still + // fits. On a session of its own, with First and Fill as above. + let mut u = world(); + u.run("share.dataStore", &json!({"name": "out", "path": dir.to_str().unwrap()})).unwrap(); + add(&mut u, "self_first_a.csv", &["x".to_string()]); + add(&mut u, "self_first_b.csv", &["x".to_string()]); + add(&mut u, "past_fill_one.csv", &fill_one); + add(&mut u, "past_fill_many.csv", &fill_many); + add(&mut u, "past_self.csv", &["z".to_string()]); + for (name, origin, destination) in + [("First", "self_first_a", "self_first_b"), ("Fill", "past_fill_one", "past_fill_many"), ("Self", "past_self", "past_self")] + { + u.run( + "data.relate", + &json!({"name": name, "origin": origin, "originKey": "key", "destination": destination, "destinationKey": "key"}), + ) + .unwrap(); + } + u.run("share.linkChart", &json!({})).unwrap(); + // First draws 1 link (2 features), Fill 1,499 (2,997), and Self its feature's link to itself (1 more). + assert_eq!((drawn("Relationships").0, drawn("Relationships").1), (1501, 3000)); // A feature isn't drawn alone: one feature related to 2 (3 features), then pairs. After 1,498 // pairs the chart holds 2,999, and a pair needs 2 more: the rest are left out whole, rather // than an origin drawn without its link.