From 94fc52efae962a1613a440a03fc77a6df7fec801 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 08:50:20 -0500 Subject: [PATCH] xlsx: share inline dxf number formats within a styles part (#3260) * xlsx: share each custom number format across cell formats, cap font names at 31 characters Every cell format record copied its font (name included) and built its own copy of its number-format code, so many short records naming one long font or code multiplied it in memory. Custom codes are now built once and shared, and font names are cut to Excel's 31 characters. * xlsx: drop stray worktree entries added by mistake * address review: compare truncated theme font for the scheme link; intern inline dxf number formats --- crates/xlsx/src/styles.rs | 8 +++++--- crates/xlsx/src/tests/fixtures.rs | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/crates/xlsx/src/styles.rs b/crates/xlsx/src/styles.rs index e80f5928e..f25424d59 100644 --- a/crates/xlsx/src/styles.rs +++ b/crates/xlsx/src/styles.rs @@ -393,12 +393,14 @@ pub fn read_styles(root: &El, checkboxes: &[bool], minor_font: &str) -> StylesIn } } } - let dxfs = root.child("dxfs").map(|d| d.kids("dxf").map(|x| read_dxf(x, &palette, &shared_fmts)).collect()).unwrap_or_default(); + // Inline dxf format codes are interned: a code repeated by many dxfs is one allocation. + let mut inline_fmts = HashMap::new(); + let dxfs = root.child("dxfs").map(|d| d.kids("dxf").map(|x| read_dxf(x, &palette, &shared_fmts, &mut inline_fmts)).collect()).unwrap_or_default(); StylesIn { xfs, dxfs, palette, named, num_fmts: fmts } } /// One differential format (`dxf`): the style it sets over a cell's own. -fn read_dxf(x: &El, palette: &[u32], fmts: &HashMap) -> Style { +fn read_dxf<'a>(x: &'a El, palette: &[u32], fmts: &HashMap, inline_fmts: &mut HashMap<&'a str, NumFmt>) -> Style { let mut s = Style::default(); if let Some(e) = x.child("font") { let f = read_font(e, palette); @@ -421,7 +423,7 @@ fn read_dxf(x: &El, palette: &[u32], fmts: &HashMap) -> Style { } if let Some(n) = x.child("numFmt") { if let Some(code) = n.attr("formatCode") { - s.num_fmt = NumFmt::new(code); + s.num_fmt = inline_fmts.entry(code).or_insert_with(|| NumFmt::new(code)).clone(); } else if let Some(id) = n.attr_u32("numFmtId") { s.num_fmt = num_fmt_code(id, fmts); } diff --git a/crates/xlsx/src/tests/fixtures.rs b/crates/xlsx/src/tests/fixtures.rs index 76f40851b..7a5cc5676 100644 --- a/crates/xlsx/src/tests/fixtures.rs +++ b/crates/xlsx/src/tests/fixtures.rs @@ -869,3 +869,17 @@ fn every_skipped_drawing_object_is_reported() { assert_eq!(hits, 1, "object {n}: {:?}", rep.warnings); } } + +/// A number format code repeated by many differential formats is read into one allocation. +#[test] +fn inline_dxf_number_formats_are_shared() { + let code = format!("0.00{}", " \"x\"".repeat(50)); + let dxfs: String = (0..3).map(|_| format!(r#""#, code.replace('"', """))).collect(); + let styles = + format!(r#"{dxfs}"#); + let root = crate::xml::parse(styles.as_bytes()).unwrap(); + let read = crate::styles::read_styles(&root, &[], "Calibri"); + assert_eq!(read.dxfs.len(), 3); + assert_eq!(read.dxfs[0].num_fmt.as_str(), code); + assert!(read.dxfs.iter().all(|d| std::sync::Arc::ptr_eq(&d.num_fmt.0, &read.dxfs[0].num_fmt.0))); +}