Skip to content

Commit f6854ab

Browse files
committed
rustdoc: Use an array instead of an object for the search index.
`buildIndex` JS function recovers them into the original object form. This greatly reduces the size of the uncompressed search index (27%), while this effect is less visible after gzipped (~5%).
1 parent f1de04c commit f6854ab

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

src/librustdoc/html/render.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,17 +313,17 @@ pub fn run(mut krate: clean::Crate, dst: Path) -> io::IoResult<()> {
313313
if i > 0 {
314314
try!(write!(&mut w, ","));
315315
}
316-
try!(write!(&mut w, "\\{ty:{:u},name:\"{}\",path:\"{}\",desc:{}",
316+
try!(write!(&mut w, r#"[{:u},"{}","{}",{}"#,
317317
item.ty, item.name, item.path,
318318
item.desc.to_json().to_str()));
319319
match item.parent {
320320
Some(nodeid) => {
321321
let pathid = *nodeid_to_pathid.find(&nodeid).unwrap();
322-
try!(write!(&mut w, ",parent:{}", pathid));
322+
try!(write!(&mut w, ",{}", pathid));
323323
}
324324
None => {}
325325
}
326-
try!(write!(&mut w, "\\}"));
326+
try!(write!(&mut w, "]"));
327327
}
328328
try!(write!(&mut w, "];"));
329329
try!(write!(&mut w, "allPaths['{}'] = [", krate.name));
@@ -332,7 +332,7 @@ pub fn run(mut krate: clean::Crate, dst: Path) -> io::IoResult<()> {
332332
if i > 0 {
333333
try!(write!(&mut w, ","));
334334
}
335-
try!(write!(&mut w, "\\{type:{:u},name:'{}'\\}",
335+
try!(write!(&mut w, r#"[{:u},"{}"]"#,
336336
short, *fqp.last().unwrap()));
337337
}
338338
try!(write!(&mut w, "];"));

src/librustdoc/html/static/main.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@
294294
if ((validate) &&
295295
(name.toLowerCase().indexOf(keys[i]) > -1 ||
296296
path.toLowerCase().indexOf(keys[i]) > -1 ||
297-
parent.name.toLowerCase().indexOf(keys[i]) > -1))
297+
parent[1].toLowerCase().indexOf(keys[i]) > -1))
298298
{
299299
validate = true;
300300
} else {
@@ -423,12 +423,14 @@
423423
'">' + name + '</a>';
424424
} else if (item.parent !== undefined) {
425425
var myparent = allPaths[item.crate][item.parent];
426+
var parentType = myparent[0];
427+
var parentName = myparent[1];
426428
var anchor = '#' + type + '.' + name;
427-
output += item.path + '::' + myparent.name +
429+
output += item.path + '::' + parentName +
428430
'::<a href="' + rootPath +
429431
item.path.replace(/::/g, '/') +
430-
'/' + itemTypes[myparent.type] +
431-
'.' + myparent.name +
432+
'/' + itemTypes[parentType] +
433+
'.' + parentName +
432434
'.html' + anchor +
433435
'" class="' + type +
434436
'">' + name + '</a>';
@@ -545,10 +547,12 @@
545547
// all other search operations have access to this cached data for
546548
// faster analysis operations
547549
for (i = 0; i < len; i += 1) {
548-
rawSearchIndex[crate][i].crate = crate;
549-
searchIndex.push(rawSearchIndex[crate][i]);
550-
if (typeof rawSearchIndex[crate][i].name === "string") {
551-
var word = rawSearchIndex[crate][i].name.toLowerCase();
550+
var rawRow = rawSearchIndex[crate][i];
551+
var row = {crate: crate, ty: rawRow[0], name: rawRow[1],
552+
path: rawRow[2], desc: rawRow[3], parent: rawRow[4]};
553+
searchIndex.push(row);
554+
if (typeof row.name === "string") {
555+
var word = row.name.toLowerCase();
552556
searchWords.push(word);
553557
} else {
554558
searchWords.push("");

0 commit comments

Comments
 (0)