File tree Expand file tree Collapse file tree 2 files changed +11
-2
lines changed Expand file tree Collapse file tree 2 files changed +11
-2
lines changed Original file line number Diff line number Diff line change @@ -29,8 +29,18 @@ pub(super) struct HtmlWithLimit {
29
29
impl HtmlWithLimit {
30
30
/// Create a new buffer, with a limit of `length_limit`.
31
31
pub ( super ) fn new ( length_limit : usize ) -> Self {
32
+ let buf = if length_limit > 1000 {
33
+ // If the length limit is really large, don't preallocate tons of memory.
34
+ String :: new ( )
35
+ } else {
36
+ // The length limit is actually a good heuristic for initial allocation size.
37
+ // Measurements showed that using it as the initial capacity ended up using less memory
38
+ // than `String::new`.
39
+ // See https://github.com/rust-lang/rust/pull/88173#discussion_r692531631 for more.
40
+ String :: with_capacity ( length_limit)
41
+ } ;
32
42
Self {
33
- buf : String :: new ( ) ,
43
+ buf,
34
44
len : 0 ,
35
45
limit : length_limit,
36
46
unclosed_tags : Vec :: new ( ) ,
Original file line number Diff line number Diff line change @@ -1096,7 +1096,6 @@ fn markdown_summary_with_limit(
1096
1096
let p = Parser :: new_with_broken_link_callback ( md, opts ( ) , Some ( & mut replacer) ) ;
1097
1097
let mut p = LinkReplacer :: new ( p, link_names) ;
1098
1098
1099
- // FIXME: capacity
1100
1099
let mut buf = HtmlWithLimit :: new ( length_limit) ;
1101
1100
let mut stopped_early = false ;
1102
1101
p. try_for_each ( |event| {
You can’t perform that action at this time.
0 commit comments