File tree 2 files changed +10
-2
lines changed
2 files changed +10
-2
lines changed Original file line number Diff line number Diff line change @@ -30,6 +30,7 @@ use crate::formats::item_type::ItemType;
30
30
use crate :: html:: escape:: Escape ;
31
31
use crate :: html:: render:: Context ;
32
32
33
+ use super :: url_parts_builder:: estimate_item_path_byte_length;
33
34
use super :: url_parts_builder:: UrlPartsBuilder ;
34
35
35
36
crate trait Print {
@@ -505,8 +506,7 @@ crate enum HrefError {
505
506
506
507
// Panics if `syms` is empty.
507
508
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 ( ) ) ) ;
510
510
s. push_str ( & syms[ 0 ] . as_str ( ) ) ;
511
511
for sym in & syms[ 1 ..] {
512
512
s. push_str ( "::" ) ;
Original file line number Diff line number Diff line change @@ -110,6 +110,14 @@ impl UrlPartsBuilder {
110
110
/// This is intentionally on the lower end to avoid overallocating.
111
111
const AVG_PART_LENGTH : usize = 5 ;
112
112
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
+
113
121
impl < ' a > FromIterator < & ' a str > for UrlPartsBuilder {
114
122
fn from_iter < T : IntoIterator < Item = & ' a str > > ( iter : T ) -> Self {
115
123
let iter = iter. into_iter ( ) ;
You can’t perform that action at this time.
0 commit comments