Skip to content

Commit f5363cb

Browse files
Auto merge of #142634 - lolbinarycat:rustdoc-OrderedJson-writer, r=<try>
alternate interface for OrderedJson to reduce allocations inspired by #142380 (comment) r? `@GuillaumeGomez` <!-- homu-ignore:start --> <!-- If this PR is related to an unstable feature or an otherwise tracked effort, please link to the relevant tracking issue here. If you don't know of a related tracking issue or there are none, feel free to ignore this. This PR will get automatically assigned to a reviewer. In case you would like a specific user to review your work, you can assign it to them by using r? <reviewer name> --> <!-- homu-ignore:end -->
2 parents 86d0aef + 9c7d086 commit f5363cb

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

src/librustdoc/html/render/ordered_json.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ impl OrderedJson {
3232
let items = items.into_iter().format_with(",", |item, f| f(item.borrow()));
3333
Self(format!("[{items}]"))
3434
}
35+
36+
pub(crate) fn array_builder() -> OrderedJsonBuilder {
37+
OrderedJsonBuilder::default()
38+
}
3539
}
3640

3741
impl fmt::Display for OrderedJson {
@@ -79,5 +83,32 @@ impl fmt::Display for EscapedJson {
7983
}
8084
}
8185

86+
/// Builds an json array.
87+
#[derive(Default)]
88+
pub(crate) struct OrderedJsonBuilder {
89+
partial: Vec<u8>,
90+
}
91+
92+
impl OrderedJsonBuilder {
93+
pub(crate) fn push(&mut self, item: impl Serialize) -> &mut Self {
94+
if self.partial.is_empty() {
95+
self.partial.push(b'[');
96+
} else {
97+
self.partial.push(b',');
98+
}
99+
serde_json::to_writer(&mut self.partial, &item).expect("serialization failed");
100+
self
101+
}
102+
103+
pub(crate) fn build(&mut self) -> OrderedJson {
104+
let mut out = std::mem::take(&mut self.partial);
105+
if out.is_empty() {
106+
return OrderedJson("[]".to_string());
107+
}
108+
out.push(b']');
109+
OrderedJson(String::from_utf8(out).expect("serialization bug: json is invalid utf8"))
110+
}
111+
}
112+
82113
#[cfg(test)]
83114
mod tests;

src/librustdoc/html/render/search_index.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -796,10 +796,8 @@ pub(crate) fn build_index(
796796
desc_index,
797797
empty_desc,
798798
};
799-
let index = OrderedJson::array_unsorted([
800-
OrderedJson::serialize(crate_name.as_str()).unwrap(),
801-
OrderedJson::serialize(data).unwrap(),
802-
]);
799+
800+
let index = OrderedJson::array_builder().push(crate_name.as_str()).push(data).build();
803801
SerializedSearchIndex { index, desc }
804802
}
805803

src/librustdoc/html/static/js/rustdoc.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ declare namespace rustdoc {
129129

130130
/**
131131
* A single parsed "atom" in a search query. For example,
132-
*
132+
*
133133
* std::fmt::Formatter, Write -> Result<()>
134134
* ┏━━━━━━━━━━━━━━━━━━ ┌──── ┏━━━━━┅┅┅┅┄┄┄┄┄┄┄┄┄┄┄┄┄┄┐
135135
* ┃ │ ┗ QueryElement { ┊

0 commit comments

Comments
 (0)