Clarify chart cache budget drops

This commit is contained in:
Neil Cawse committed 2026-10-22 06:30:27 +00:00
1 parent 5b8c2cde8d
commit 3d82a9cf4e
3 files changed
+24 -6

No files matched your search

+10 -5
View File
@@ -132,17 +132,22 @@ const MAX_CACHED_POINTS: usize = gridcraft_core::MAX_ROWS as usize;
/// The points of a cache or literal (`c` holds `ptCount`, `pts` the `<c:pt>`s), by index; missing
/// points are `None`. Points without a valid `idx` (schema-invalid, but some writers omit it) go
/// in document order. A missing, unreadable or too small `ptCount` gives way to the points present.
///
/// The per-chart budget is checked before allocation. If the requested count would exceed the
/// remaining budget, all present points are retained when they fit; only trailing gaps are
/// clamped. If present points extend past the remaining budget, points beyond that boundary are
/// dropped as well, and the caller is warned.
fn read_points<'a>(c: &El, pts: &'a El, budget: &mut ChartReadBudget) -> Option<Vec<Option<&'a str>>> {
let at = |i: usize, p: &El| p.attr("idx").and_then(|v| v.trim().parse::<usize>().ok()).unwrap_or(i);
let placed = || pts.kids("pt").enumerate().map(|(i, p)| (at(i, p), p)).filter(|(i, _)| *i < MAX_CACHED_POINTS);
let highest = placed().map(|(i, _)| i + 1).max().unwrap_or(0);
let requested =
c.child_val("ptCount").and_then(|v| v.trim().parse::<usize>().ok()).map(|n| n.min(MAX_CACHED_POINTS).max(highest)).unwrap_or(highest);
let count = if requested > budget.remaining {
budget.exceeded = true;
highest.min(budget.remaining)
} else {
let count = if requested <= budget.remaining {
requested
} else {
budget.exceeded = true;
if highest <= budget.remaining { highest } else { budget.remaining }
};
budget.remaining = budget.remaining.saturating_sub(count);
if count == 0 {
@@ -438,7 +443,7 @@ fn axis_ids(g: &El) -> Vec<String> {
}
/// A chart part, with the cached points of each series when it is a PivotChart (empty otherwise),
#[allow(dead_code)]
#[cfg(test)]
pub(crate) fn read_chart(root: &El) -> Option<(Chart, Vec<PivotSeriesCache>)> {
read_chart_with_budget(root).map(|(chart, caches, _)| (chart, caches))
}
+1 -1
View File
@@ -176,7 +176,7 @@ pub fn read_drawing(cx: &mut Ctx<'_>, part: &str, sheet: &mut Sheet) -> Result<(
ch.id = cx.next_object_id();
ch.anchor = anchor;
if budget_exceeded {
cx.warn("chart cached points exceeded the per-chart import budget; trailing gaps were truncated");
cx.warn("cached chart points were dropped after the per-chart import budget was exceeded");
}
if !caches.is_empty() {
cx.pivot_charts.push((ch.id, caches));
+13
View File
@@ -266,6 +266,19 @@ fn chart_cache_budget_bounds_many_empty_series() {
assert!(warned);
}
#[test]
fn chart_cache_budget_drops_points_beyond_boundary() {
let series: String = (0..17)
.map(|i| format!(r#"<c:ser><c:idx val="{i}"/><c:order val="{i}"/><c:val><c:numLit><c:ptCount val="1048576"/><c:pt idx="1048575"><c:v>{i}</c:v></c:pt></c:numLit></c:val></c:ser>"#))
.collect();
let root = crate::xml::parse(chart_xml(false, &format!("<c:pieChart>{series}</c:pieChart>"), "").as_bytes()).unwrap();
let (chart, _, warned) = crate::chart::read_chart_with_budget(&root).unwrap();
assert_eq!(chart.series.len(), 17);
assert!(chart.series.iter().take(16).all(|s| !s.values.is_empty()));
assert_eq!(chart.series[16].values, "");
assert!(warned);
}
#[test]
fn a_deleted_automatic_title_stays_deleted() {
let one = ser("<c:v>Vehicles</c:v>", &str_cache("S!$A$1:$A$2", &["a", "b"]), &num_cache("S!$B$1:$B$2", 2, &[]));