Skip to content

Commit e61be1b

Browse files
committed
rustdoc-search: reuse empty map/array in function signatures
Map is implemented as a pointer to a mutable object. Rustdoc never mutates function signatures after constructing them, but the JS engine doesn't know that. To save a bunch of memory, use a single immutable map for every decoded type object with no bindings or generics.
1 parent 0ee9cfd commit e61be1b

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

src/librustdoc/html/static/js/search.js

+21-5
Original file line numberDiff line numberDiff line change
@@ -2717,9 +2717,25 @@ ${item.displayPath}<span class="${type}">${name}</span>\
27172717
* @return {Array<FunctionSearchType>}
27182718
*/
27192719
function buildItemSearchTypeAll(types, lowercasePaths) {
2720-
return types.map(type => buildItemSearchType(type, lowercasePaths));
2720+
return types.length > 0 ?
2721+
types.map(type => buildItemSearchType(type, lowercasePaths)) :
2722+
EMPTY_GENERICS_ARRAY;
27212723
}
27222724

2725+
/**
2726+
* Empty, immutable map used in item search types with no bindings.
2727+
*
2728+
* @type {Map<integer, Array<Functiontype>>}
2729+
*/
2730+
const EMPTY_BINDINGS_MAP = new Map();
2731+
2732+
/**
2733+
* Empty, immutable map used in item search types with no bindings.
2734+
*
2735+
* @type {Array<Functiontype>}
2736+
*/
2737+
const EMPTY_GENERICS_ARRAY = [];
2738+
27232739
/**
27242740
* Converts a single type.
27252741
*
@@ -2732,15 +2748,15 @@ ${item.displayPath}<span class="${type}">${name}</span>\
27322748
let pathIndex, generics, bindings;
27332749
if (typeof type === "number") {
27342750
pathIndex = type;
2735-
generics = [];
2736-
bindings = new Map();
2751+
generics = EMPTY_GENERICS_ARRAY;
2752+
bindings = EMPTY_BINDINGS_MAP;
27372753
} else {
27382754
pathIndex = type[PATH_INDEX_DATA];
27392755
generics = buildItemSearchTypeAll(
27402756
type[GENERICS_DATA],
27412757
lowercasePaths
27422758
);
2743-
if (type.length > BINDINGS_DATA) {
2759+
if (type.length > BINDINGS_DATA && type[BINDINGS_DATA].length > 0) {
27442760
bindings = new Map(type[BINDINGS_DATA].map(binding => {
27452761
const [assocType, constraints] = binding;
27462762
// Associated type constructors are represented sloppily in rustdoc's
@@ -2759,7 +2775,7 @@ ${item.displayPath}<span class="${type}">${name}</span>\
27592775
];
27602776
}));
27612777
} else {
2762-
bindings = new Map();
2778+
bindings = EMPTY_BINDINGS_MAP;
27632779
}
27642780
}
27652781
if (pathIndex < 0) {

0 commit comments

Comments
 (0)