about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNoah Lev <camelidcamel@gmail.com>2022-01-10 12:23:58 -0800
committerNoah Lev <camelidcamel@gmail.com>2022-01-14 12:05:35 -0800
commit8f59eb6da014cb146c1477ab0ddf35729c88e99f (patch)
tree11201dae5dcffad81c91aba9d4ae28cdba42bc31
parent53f1bed83ab3124ddc26c8e1e28666ae8a0f1842 (diff)
downloadrust-8f59eb6da014cb146c1477ab0ddf35729c88e99f.tar.gz
rust-8f59eb6da014cb146c1477ab0ddf35729c88e99f.zip
Estimate path length instead of hardcoding 64 bytes
-rw-r--r--src/librustdoc/html/format.rs4
-rw-r--r--src/librustdoc/html/url_parts_builder.rs8
2 files changed, 10 insertions, 2 deletions
diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs
index 342c15855d2..8571a6a137f 100644
--- a/src/librustdoc/html/format.rs
+++ b/src/librustdoc/html/format.rs
@@ -30,6 +30,7 @@ use crate::formats::item_type::ItemType;
 use crate::html::escape::Escape;
 use crate::html::render::Context;
 
+use super::url_parts_builder::estimate_item_path_byte_length;
 use super::url_parts_builder::UrlPartsBuilder;
 
 crate trait Print {
@@ -505,8 +506,7 @@ crate enum HrefError {
 
 // Panics if `syms` is empty.
 crate fn join_with_double_colon(syms: &[Symbol]) -> String {
-    // 64 bytes covers 99.9%+ of cases.
-    let mut s = String::with_capacity(64);
+    let mut s = String::with_capacity(estimate_item_path_byte_length(syms.len()));
     s.push_str(&syms[0].as_str());
     for sym in &syms[1..] {
         s.push_str("::");
diff --git a/src/librustdoc/html/url_parts_builder.rs b/src/librustdoc/html/url_parts_builder.rs
index 5c1557078aa..26cebe8c72b 100644
--- a/src/librustdoc/html/url_parts_builder.rs
+++ b/src/librustdoc/html/url_parts_builder.rs
@@ -110,6 +110,14 @@ impl UrlPartsBuilder {
 /// This is intentionally on the lower end to avoid overallocating.
 const AVG_PART_LENGTH: usize = 5;
 
+/// Estimate the number of bytes in an item's path, based on how many segments it has.
+///
+/// **Note:** This is only to be used with, e.g., [`String::with_capacity()`];
+/// the return value is just a rough estimate.
+crate const fn estimate_item_path_byte_length(segment_count: usize) -> usize {
+    AVG_PART_LENGTH * segment_count
+}
+
 impl<'a> FromIterator<&'a str> for UrlPartsBuilder {
     fn from_iter<T: IntoIterator<Item = &'a str>>(iter: T) -> Self {
         let iter = iter.into_iter();