Skip to content

Commit

Permalink
Estimate path length instead of hardcoding 64 bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
camelid committed Jan 14, 2022
1 parent 53f1bed commit 8f59eb6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/librustdoc/html/format.rs
Expand Up @@ -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 {
Expand Down Expand Up @@ -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("::");
Expand Down
8 changes: 8 additions & 0 deletions src/librustdoc/html/url_parts_builder.rs
Expand Up @@ -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();
Expand Down

0 comments on commit 8f59eb6

Please sign in to comment.