Skip to content

Commit ab6915d

Browse files
committed
rustdoc: Use smaller sequential numbers instead of NodeIds for parents.
`allPaths` is now a flat array in effect. This decreases the size of the search index by about 4--5% (gzipped or not).
1 parent 5d284a0 commit ab6915d

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

src/librustdoc/html/render.rs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,9 @@ pub fn run(mut krate: clean::Crate, dst: Path) -> io::IoResult<()> {
262262
});
263263
cache.stack.push(krate.name.clone());
264264
krate = cache.fold_crate(krate);
265+
266+
let mut nodeid_to_pathid = HashMap::new();
267+
let mut pathid_to_nodeid = Vec::new();
265268
{
266269
let Cache { search_index: ref mut index,
267270
orphan_methods: ref meths, paths: ref mut paths, ..} = cache;
@@ -283,17 +286,21 @@ pub fn run(mut krate: clean::Crate, dst: Path) -> io::IoResult<()> {
283286
}
284287
};
285288

286-
// Prune the paths that do not appear in the index.
287-
let mut unseen: HashSet<ast::NodeId> = paths.keys().map(|&id| id).collect();
289+
// Reduce `NodeId` in paths into smaller sequential numbers,
290+
// and prune the paths that do not appear in the index.
288291
for item in index.iter() {
289292
match item.parent {
290-
Some(ref pid) => { unseen.remove(pid); }
293+
Some(nodeid) => {
294+
if !nodeid_to_pathid.contains_key(&nodeid) {
295+
let pathid = pathid_to_nodeid.len();
296+
nodeid_to_pathid.insert(nodeid, pathid);
297+
pathid_to_nodeid.push(nodeid);
298+
}
299+
}
291300
None => {}
292301
}
293302
}
294-
for pid in unseen.iter() {
295-
paths.remove(pid);
296-
}
303+
assert_eq!(nodeid_to_pathid.len(), pathid_to_nodeid.len());
297304
}
298305

299306
// Publish the search index
@@ -308,23 +315,25 @@ pub fn run(mut krate: clean::Crate, dst: Path) -> io::IoResult<()> {
308315
item.ty, item.name, item.path,
309316
item.desc.to_json().to_str()));
310317
match item.parent {
311-
Some(id) => {
312-
try!(write!(&mut w, ",parent:'{}'", id));
318+
Some(nodeid) => {
319+
let pathid = *nodeid_to_pathid.find(&nodeid).unwrap();
320+
try!(write!(&mut w, ",parent:{}", pathid));
313321
}
314322
None => {}
315323
}
316324
try!(write!(&mut w, "\\}"));
317325
}
318326
try!(write!(&mut w, "];"));
319-
try!(write!(&mut w, "allPaths['{}'] = \\{", krate.name));
320-
for (i, (&id, &(ref fqp, short))) in cache.paths.iter().enumerate() {
327+
try!(write!(&mut w, "allPaths['{}'] = [", krate.name));
328+
for (i, &nodeid) in pathid_to_nodeid.iter().enumerate() {
329+
let &(ref fqp, short) = cache.paths.find(&nodeid).unwrap();
321330
if i > 0 {
322331
try!(write!(&mut w, ","));
323332
}
324-
try!(write!(&mut w, "'{}':\\{type:'{}',name:'{}'\\}",
325-
id, short, *fqp.last().unwrap()));
333+
try!(write!(&mut w, "\\{type:'{}',name:'{}'\\}",
334+
short, *fqp.last().unwrap()));
326335
}
327-
try!(write!(&mut w, "\\};"));
336+
try!(write!(&mut w, "];"));
328337

329338
str::from_utf8(w.unwrap().as_slice()).unwrap().to_owned()
330339
};

0 commit comments

Comments
 (0)