Skip to content

Commit 40036cc

Browse files
committed
WIP: alternate interface for OrderedJson to reduce allocations
1 parent 3bc767e commit 40036cc

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

src/librustdoc/html/render/ordered_json.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,25 @@ 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+
}
39+
40+
pub(crate) fn array_unsorted_serialize<T: Serialize, I: IntoIterator<Item = T>>(items: I) -> Self {
41+
let mut out = Vec::with_capacity(1024);
42+
let mut add_comma = false;
43+
out.push(b'[');
44+
for item in items {
45+
if add_comma {
46+
out.push(b',');
47+
}
48+
// nothing we serialize should be able to fail to serialize.
49+
serde_json::to_writer(&mut out, &item).expect("serialization failed");
50+
}
51+
out.push(b']');
52+
Self(String::from_utf8(out).expect("json was not utf8"))
53+
}
3554
}
3655

3756
impl fmt::Display for OrderedJson {
@@ -79,5 +98,24 @@ impl fmt::Display for EscapedJson {
7998
}
8099
}
81100

101+
/// Builds an json array.
102+
#[derive(Default)]
103+
pub(crate) struct OrderedJsonBuilder {
104+
partial: Vec<u8>,
105+
}
106+
107+
impl OrderedJsonBuilder {
108+
fn push(item: impl Serialize) {
109+
if self.partial.is_empty() {
110+
self.partial.push('[');
111+
} else {
112+
self.partial.push(',');
113+
}
114+
serde_json::to_writer(&mut out, &item).expect("serialization failed");
115+
}
116+
}
117+
118+
119+
82120
#[cfg(test)]
83121
mod tests;

src/librustdoc/html/render/search_index.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,7 @@ pub(crate) fn build_index(
796796
desc_index,
797797
empty_desc,
798798
};
799+
799800
let index = OrderedJson::array_unsorted([
800801
OrderedJson::serialize(crate_name.as_str()).unwrap(),
801802
OrderedJson::serialize(data).unwrap(),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// type checking. See README.md in this directory for more info.
44

55
/* eslint-disable */
6-
declare global {
6+
declare global {
77
/** Map from crate name to directory structure, for source view */
88
declare var srcIndex: Map<string, rustdoc.Dir>;
99
/** Defined and documented in `storage.js` */

0 commit comments

Comments
 (0)