Skip to content

Commit 8f5d71c

Browse files
committed
rustdoc: Omit repeated paths in the search index.
Since the items roughly follow the lexical order, there are many consecutive items with the same path value which can be easily compressed. For the library and compiler docs, this commit decreases the index size by 26% and 6% before and after gzip, respectively.
1 parent 9eb336a commit 8f5d71c

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/librustdoc/html/render.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -309,12 +309,23 @@ pub fn run(mut krate: clean::Crate, dst: Path) -> io::IoResult<()> {
309309
let index = {
310310
let mut w = MemWriter::new();
311311
try!(write!(&mut w, r#"searchIndex['{}'] = \{"items":["#, krate.name));
312+
313+
let mut lastpath = ~"";
312314
for (i, item) in cache.search_index.iter().enumerate() {
315+
// Omit the path if it is same to that of the prior item.
316+
let path;
317+
if lastpath == item.path {
318+
path = "";
319+
} else {
320+
lastpath = item.path.clone();
321+
path = item.path.as_slice();
322+
};
323+
313324
if i > 0 {
314325
try!(write!(&mut w, ","));
315326
}
316327
try!(write!(&mut w, r#"[{:u},"{}","{}",{}"#,
317-
item.ty, item.name, item.path,
328+
item.ty, item.name, path,
318329
item.desc.to_json().to_str()));
319330
match item.parent {
320331
Some(nodeid) => {
@@ -325,7 +336,9 @@ pub fn run(mut krate: clean::Crate, dst: Path) -> io::IoResult<()> {
325336
}
326337
try!(write!(&mut w, "]"));
327338
}
339+
328340
try!(write!(&mut w, r#"],"paths":["#));
341+
329342
for (i, &nodeid) in pathid_to_nodeid.iter().enumerate() {
330343
let &(ref fqp, short) = cache.paths.find(&nodeid).unwrap();
331344
if i > 0 {
@@ -334,6 +347,7 @@ pub fn run(mut krate: clean::Crate, dst: Path) -> io::IoResult<()> {
334347
try!(write!(&mut w, r#"[{:u},"{}"]"#,
335348
short, *fqp.last().unwrap()));
336349
}
350+
337351
try!(write!(&mut w, r"]\};"));
338352

339353
str::from_utf8(w.unwrap().as_slice()).unwrap().to_owned()

src/librustdoc/html/static/main.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@
539539

540540
// an array of [(Number) item type,
541541
// (String) name,
542-
// (String) full path,
542+
// (String) full path or empty string for previous path,
543543
// (String) description,
544544
// (optional Number) the parent path index to `paths`]
545545
var items = rawSearchIndex[crate].items;
@@ -561,10 +561,11 @@
561561
// all other search operations have access to this cached data for
562562
// faster analysis operations
563563
var len = items.length;
564+
var lastPath = "";
564565
for (var i = 0; i < len; i += 1) {
565566
var rawRow = items[i];
566567
var row = {crate: crate, ty: rawRow[0], name: rawRow[1],
567-
path: rawRow[2], desc: rawRow[3],
568+
path: rawRow[2] || lastPath, desc: rawRow[3],
568569
parent: paths[rawRow[4]]};
569570
searchIndex.push(row);
570571
if (typeof row.name === "string") {
@@ -573,6 +574,7 @@
573574
} else {
574575
searchWords.push("");
575576
}
577+
lastPath = row.path;
576578
}
577579
}
578580
return searchWords;

0 commit comments

Comments
 (0)