1
Fork 0

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.
This commit is contained in:
Kang Seonghoon 2014-04-10 02:24:00 +09:00
parent 9eb336a020
commit 8f5d71cf71
2 changed files with 19 additions and 3 deletions

View file

@ -309,12 +309,23 @@ pub fn run(mut krate: clean::Crate, dst: Path) -> io::IoResult<()> {
let index = { let index = {
let mut w = MemWriter::new(); let mut w = MemWriter::new();
try!(write!(&mut w, r#"searchIndex['{}'] = \{"items":["#, krate.name)); try!(write!(&mut w, r#"searchIndex['{}'] = \{"items":["#, krate.name));
let mut lastpath = ~"";
for (i, item) in cache.search_index.iter().enumerate() { for (i, item) in cache.search_index.iter().enumerate() {
// Omit the path if it is same to that of the prior item.
let path;
if lastpath == item.path {
path = "";
} else {
lastpath = item.path.clone();
path = item.path.as_slice();
};
if i > 0 { if i > 0 {
try!(write!(&mut w, ",")); try!(write!(&mut w, ","));
} }
try!(write!(&mut w, r#"[{:u},"{}","{}",{}"#, try!(write!(&mut w, r#"[{:u},"{}","{}",{}"#,
item.ty, item.name, item.path, item.ty, item.name, path,
item.desc.to_json().to_str())); item.desc.to_json().to_str()));
match item.parent { match item.parent {
Some(nodeid) => { Some(nodeid) => {
@ -325,7 +336,9 @@ pub fn run(mut krate: clean::Crate, dst: Path) -> io::IoResult<()> {
} }
try!(write!(&mut w, "]")); try!(write!(&mut w, "]"));
} }
try!(write!(&mut w, r#"],"paths":["#)); try!(write!(&mut w, r#"],"paths":["#));
for (i, &nodeid) in pathid_to_nodeid.iter().enumerate() { for (i, &nodeid) in pathid_to_nodeid.iter().enumerate() {
let &(ref fqp, short) = cache.paths.find(&nodeid).unwrap(); let &(ref fqp, short) = cache.paths.find(&nodeid).unwrap();
if i > 0 { if i > 0 {
@ -334,6 +347,7 @@ pub fn run(mut krate: clean::Crate, dst: Path) -> io::IoResult<()> {
try!(write!(&mut w, r#"[{:u},"{}"]"#, try!(write!(&mut w, r#"[{:u},"{}"]"#,
short, *fqp.last().unwrap())); short, *fqp.last().unwrap()));
} }
try!(write!(&mut w, r"]\};")); try!(write!(&mut w, r"]\};"));
str::from_utf8(w.unwrap().as_slice()).unwrap().to_owned() str::from_utf8(w.unwrap().as_slice()).unwrap().to_owned()

View file

@ -539,7 +539,7 @@
// an array of [(Number) item type, // an array of [(Number) item type,
// (String) name, // (String) name,
// (String) full path, // (String) full path or empty string for previous path,
// (String) description, // (String) description,
// (optional Number) the parent path index to `paths`] // (optional Number) the parent path index to `paths`]
var items = rawSearchIndex[crate].items; var items = rawSearchIndex[crate].items;
@ -561,10 +561,11 @@
// all other search operations have access to this cached data for // all other search operations have access to this cached data for
// faster analysis operations // faster analysis operations
var len = items.length; var len = items.length;
var lastPath = "";
for (var i = 0; i < len; i += 1) { for (var i = 0; i < len; i += 1) {
var rawRow = items[i]; var rawRow = items[i];
var row = {crate: crate, ty: rawRow[0], name: rawRow[1], var row = {crate: crate, ty: rawRow[0], name: rawRow[1],
path: rawRow[2], desc: rawRow[3], path: rawRow[2] || lastPath, desc: rawRow[3],
parent: paths[rawRow[4]]}; parent: paths[rawRow[4]]};
searchIndex.push(row); searchIndex.push(row);
if (typeof row.name === "string") { if (typeof row.name === "string") {
@ -573,6 +574,7 @@
} else { } else {
searchWords.push(""); searchWords.push("");
} }
lastPath = row.path;
} }
} }
return searchWords; return searchWords;