diff --git a/crates/java-class/src/java_class.rs b/crates/java-class/src/java_class.rs index 0bd1853dd0..47fd0ef67a 100644 --- a/crates/java-class/src/java_class.rs +++ b/crates/java-class/src/java_class.rs @@ -383,11 +383,23 @@ impl<'p> Names<'p> { /// The length of each rendered text is kept by kind and index, so a text over the budget is /// built at most once however many entries name it. pub(crate) fn keep(&mut self, kind: &'static str, idx: u16, render: fn(&[Const], u16) -> String) -> String { - if let Some(&len) = self.built.get(&(kind, idx)) { + self.keep_as((kind, idx), kind, idx, render) + } + + /// Like `keep` for a loadable constant. Constants that copy the same Utf8 the same way share + /// one kept length, so many `String` entries over one long Utf8 build its text at most once + /// past the budget. + pub(crate) fn constant(&mut self, idx: u16, render: fn(&[Const], u16) -> String) -> String { + let key = const_text_key(self.pool, idx); + self.keep_as(key, "const", idx, render) + } + + fn keep_as(&mut self, key: (&'static str, u16), kind: &'static str, idx: u16, render: fn(&[Const], u16) -> String) -> String { + if let Some(&len) = self.built.get(&key) { return if self.admit(len) { render(self.pool, idx) } else { format!("<{kind}#{idx}>") }; } let text = render(self.pool, idx); - self.built.insert((kind, idx), text.len()); + self.built.insert(key, text.len()); if self.admit(text.len()) { text } else { format!("<{kind}#{idx}>") } } @@ -600,7 +612,7 @@ fn parse_members(c: &mut Cursor<'_>, names: &mut Names<'_>, warnings: &mut Vec m.code = parse_code(a.info, names), "ConstantValue" => { if let Some(i) = Reader::new(a.info).u16_be(0) { - m.constant_value = Some(names.keep("const", i, const_display)); + m.constant_value = Some(names.constant(i, const_display)); } } "Exceptions" => m.exceptions = parse_exceptions_attr(a.info, names), @@ -756,6 +768,17 @@ fn const_display(pool: &[Const], idx: u16) -> String { } } +/// The key under which `Names` keeps the length of `const_display(pool, idx)`: the Utf8 a +/// `String`, `Class` or `MethodType` constant copies, otherwise the constant itself. +fn const_text_key(pool: &[Const], idx: u16) -> (&'static str, u16) { + match pool.get(idx as usize) { + Some(Const::String(s)) => ("const-string", *s), + Some(Const::Class(n)) => ("const-class", *n), + Some(Const::MethodType(d)) => ("const-method-type", *d), + _ => ("const", idx), + } +} + // --------------------------------------------------------------------------------------------- // Resolution and formatting on a parsed class diff --git a/crates/java-class/src/regressions/java_copied_names.rs b/crates/java-class/src/regressions/java_copied_names.rs index 8ee60e0ccf..2279807c17 100644 --- a/crates/java-class/src/regressions/java_copied_names.rs +++ b/crates/java-class/src/regressions/java_copied_names.rs @@ -210,6 +210,10 @@ fn counted_render(pool: &[Const], idx: u16) -> String { RENDERS.with(|n| n.set(n.get() + 1)); match pool.get(usize::from(idx)) { Some(Const::Utf8(s)) => format!("{s:?}"), + Some(Const::String(s)) => match pool.get(usize::from(*s)) { + Some(Const::Utf8(text)) => format!("{text:?}"), + _ => String::new(), + }, _ => String::new(), } } @@ -226,3 +230,19 @@ fn text_over_the_budget_is_rendered_once() { // Only the copied texts are rendered; the refusals reuse the length kept from the first one. assert_eq!(RENDERS.with(Cell::get), copied); } + +#[test] +fn string_constants_over_one_utf8_are_rendered_once_past_the_budget() { + let entries = 2 * MAX_COPIED_TEXT / BIG; + let mut pool = vec![Const::Unusable, Const::Utf8("A".repeat(BIG))]; + pool.extend((0..entries).map(|_| Const::String(1))); + let mut names = Names::new(&pool); + RENDERS.with(|n| n.set(0)); + let kept: Vec = (2..pool.len()).map(|i| names.constant(i as u16, counted_render)).collect(); + let copied = kept.iter().filter(|t| t.len() == BIG + 2).count(); + assert!(copied >= 1 && copied < kept.len(), "{copied} of {} copied", kept.len()); + assert_eq!(kept.last().map(String::as_str), Some(format!("", pool.len() - 1).as_str())); + // Each `String` entry is its own pool index, but all of them quote entry 1, so the refusals + // reuse the length kept for entry 1 instead of building the text again. + assert_eq!(RENDERS.with(Cell::get), copied); +}