From 17f402798e5aad206d0d29b88a09346f100f7176 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 16 Nov 2026 00:00:18 +0100 Subject: [PATCH] xml: compare strict expanded attribute names by namespace id, not a URI copy A declaration's URI was copied once per attribute that used it, so one long URI and many attributes multiplied memory. Give each binding an id (shared by equal URIs, found once at declaration) and key expanded names by that id. --- crates/xml/src/read.rs | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/crates/xml/src/read.rs b/crates/xml/src/read.rs index a5d9b58ee..aaea862fc 100644 --- a/crates/xml/src/read.rs +++ b/crates/xml/src/read.rs @@ -13,6 +13,10 @@ use crate::write::is_xml_char; /// The namespace the `xml` prefix is always bound to. pub const XML_NS: &str = "http://www.w3.org/XML/1998/namespace"; +/// [`Events::scope_ids`] id of the XML namespace, bound to `xml:` without a declaration. +const XML_NS_ID: usize = usize::MAX; +/// Expanded-name id of an attribute in no namespace (or with an undeclared prefix). +const NO_NS_ID: usize = usize::MAX - 1; /// What the reader refuses to exceed. #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -470,6 +474,10 @@ pub struct Events<'o, 'i> { end_names: bool, open: Vec, scope: Vec<(String, String)>, + /// For each binding in `scope`, an id shared by every binding of an equal URI (the index of + /// the first such binding, or [`XML_NS_ID`]), with the URI's hash. Strict reads compare + /// expanded attribute names by this id instead of copying the URI for each attribute. + scope_ids: Vec<(u64, usize)>, /// The end of an empty element, due after its start. pending: bool, /// Whether a root element has started. @@ -501,7 +509,7 @@ impl<'o, 'i> Events<'o, 'i> { cfg.trim_text_start = false; cfg.trim_text_end = false; cfg.expand_empty_elements = false; - Events { reader, opts, end_names, open: Vec::new(), scope: Vec::new(), pending: false, rooted: false, span: 0..0 } + Events { reader, opts, end_names, open: Vec::new(), scope: Vec::new(), scope_ids: Vec::new(), pending: false, rooted: false, span: 0..0 } } /// The bytes of the input the last event was read from. Both events of an empty element @@ -615,6 +623,7 @@ impl<'o, 'i> Events<'o, 'i> { fn end(&mut self) -> Event { let (scope, name) = self.open.pop().map_or((0, String::new()), |o| (o.scope, o.name)); self.scope.truncate(scope); + self.scope_ids.truncate(scope); Event::End { name } } @@ -670,6 +679,7 @@ impl<'o, 'i> Events<'o, 'i> { if is_ns_decl(key) { let prefix = self.decode(key.get(6..).unwrap_or_default())?.into_owned(); let uri = self.attr_value(&a.value)?; + self.scope_ids.push(self.uri_id(&uri)); self.scope.push((prefix, uri)); } } @@ -681,6 +691,20 @@ impl<'o, 'i> Events<'o, 'i> { Ok(()) } + /// The id an expanded name uses for `uri`: one hash and at most one full comparison per + /// declaration, so a long URI costs its length once however many attributes use it. + fn uri_id(&self, uri: &str) -> (u64, usize) { + use std::hash::{Hash, Hasher}; + let mut h = std::collections::hash_map::DefaultHasher::new(); + uri.hash(&mut h); + let hash = h.finish(); + if uri == XML_NS { + return (hash, XML_NS_ID); + } + let first = self.scope_ids.iter().zip(&self.scope).position(|((h, _), (_, u))| *h == hash && u == uri); + (hash, first.map_or(self.scope.len(), |i| self.scope_ids.get(i).map_or(i, |(_, id)| *id))) + } + /// In a strict read: is the reader before or after the root element? fn outside_root(&self) -> bool { self.opts.strict && self.open.is_empty() @@ -730,13 +754,17 @@ impl<'o, 'i> Events<'o, 'i> { // Default namespaces do not apply to attributes. Keep undeclared prefixes // distinct so standalone fragments retain the reader's existing behaviour. let expanded = match raw_key.split_once(':') { - Some(("xml", local)) => (XML_NS, local), - Some((prefix, local)) => { - self.scope.iter().rev().find(|(p, _)| p == prefix).map_or(("", raw_key.as_ref()), |(_, uri)| (uri.as_str(), local)) - } - None => ("", raw_key.as_ref()), + Some(("xml", local)) => (XML_NS_ID, local), + Some((prefix, local)) => self + .scope + .iter() + .zip(&self.scope_ids) + .rev() + .find(|((p, _), _)| p == prefix) + .map_or((NO_NS_ID, raw_key.as_ref()), |(_, (_, id))| (*id, local)), + None => (NO_NS_ID, raw_key.as_ref()), }; - if !expanded_names.insert((expanded.0.to_string(), expanded.1.to_string())) { + if !expanded_names.insert((expanded.0, expanded.1.to_string())) { return self.syntax(format!("duplicate expanded attribute `{raw_key}`")); } }