Skip to content

Commit 8f59eb6

Browse files
committed
Estimate path length instead of hardcoding 64 bytes
1 parent 53f1bed commit 8f59eb6

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/librustdoc/html/format.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use crate::formats::item_type::ItemType;
3030
use crate::html::escape::Escape;
3131
use crate::html::render::Context;
3232

33+
use super::url_parts_builder::estimate_item_path_byte_length;
3334
use super::url_parts_builder::UrlPartsBuilder;
3435

3536
crate trait Print {
@@ -505,8 +506,7 @@ crate enum HrefError {
505506

506507
// Panics if `syms` is empty.
507508
crate fn join_with_double_colon(syms: &[Symbol]) -> String {
508-
// 64 bytes covers 99.9%+ of cases.
509-
let mut s = String::with_capacity(64);
509+
let mut s = String::with_capacity(estimate_item_path_byte_length(syms.len()));
510510
s.push_str(&syms[0].as_str());
511511
for sym in &syms[1..] {
512512
s.push_str("::");

src/librustdoc/html/url_parts_builder.rs

+8
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,14 @@ impl UrlPartsBuilder {
110110
/// This is intentionally on the lower end to avoid overallocating.
111111
const AVG_PART_LENGTH: usize = 5;
112112

113+
/// Estimate the number of bytes in an item's path, based on how many segments it has.
114+
///
115+
/// **Note:** This is only to be used with, e.g., [`String::with_capacity()`];
116+
/// the return value is just a rough estimate.
117+
crate const fn estimate_item_path_byte_length(segment_count: usize) -> usize {
118+
AVG_PART_LENGTH * segment_count
119+
}
120+
113121
impl<'a> FromIterator<&'a str> for UrlPartsBuilder {
114122
fn from_iter<T: IntoIterator<Item = &'a str>>(iter: T) -> Self {
115123
let iter = iter.into_iter();

0 commit comments

Comments
 (0)