Skip to content

Commit 43d45ef

Browse files
committed
rustdoc-search: reduce async machinery in value lookups
This commit is a mirrored change from stringdex that makes `at()` not always return a promise, which is fine because we can still `await` it.
1 parent a171994 commit 43d45ef

File tree

3 files changed

+57
-50
lines changed

3 files changed

+57
-50
lines changed

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1758,12 +1758,8 @@ class DocSearch {
17581758
const l = crateNames.length;
17591759
const names = [];
17601760
for (let i = 0; i < l; ++i) {
1761-
names.push(crateNames.at(i).then(name => {
1762-
if (name === undefined) {
1763-
return "";
1764-
}
1765-
return this.utf8decoder.decode(name);
1766-
}));
1761+
const name = await crateNames.at(i);
1762+
names.push(name === undefined ? "" : this.utf8decoder.decode(name));
17671763
}
17681764
return Promise.all(names);
17691765
}

src/librustdoc/html/static/js/stringdex.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ declare namespace stringdex {
2929
*/
3030
interface DataColumn {
3131
isEmpty(id: number): boolean;
32-
at(id: number): Promise<Uint8Array|undefined>;
32+
at(id: number): Promise<Uint8Array>|Uint8Array|undefined;
3333
search(name: Uint8Array|string): Promise<Trie?>;
3434
searchLev(name: Uint8Array|string): AsyncGenerator<Trie>;
3535
length: number,

src/librustdoc/html/static/js/stringdex.js

Lines changed: 54 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2261,7 +2261,7 @@ function loadDatabase(hooks) {
22612261
this.hashes = hashes;
22622262
this.emptyset = emptyset;
22632263
this.name = name;
2264-
/** @type {{"hash": Uint8Array, "data": Promise<Uint8Array[]>?, "end": number}[]} */
2264+
/** @type {{"hash": Uint8Array, "data": Uint8Array[]?, "end": number}[]} */
22652265
this.buckets = [];
22662266
this.bucket_keys = [];
22672267
const l = counts.length;
@@ -2295,64 +2295,75 @@ function loadDatabase(hooks) {
22952295
/**
22962296
* Look up a cell by row ID.
22972297
* @param {number} id
2298-
* @returns {Promise<Uint8Array|undefined>}
2298+
* @returns {Promise<Uint8Array>|Uint8Array|undefined}
22992299
*/
2300-
async at(id) {
2300+
at(id) {
23012301
if (this.emptyset.contains(id)) {
2302-
return Promise.resolve(EMPTY_UINT8);
2302+
return EMPTY_UINT8;
23032303
} else {
23042304
let idx = -1;
23052305
while (this.bucket_keys[idx + 1] <= id) {
23062306
idx += 1;
23072307
}
23082308
if (idx === -1 || idx >= this.bucket_keys.length) {
2309-
return Promise.resolve(undefined);
2309+
return undefined;
23102310
} else {
23112311
const start = this.bucket_keys[idx];
2312-
const {hash, end} = this.buckets[idx];
2312+
const bucket = this.buckets[idx];
23132313
let data = this.buckets[idx].data;
23142314
if (data === null) {
2315-
const dataSansEmptysetOrig = await registry.dataLoadByNameAndHash(
2316-
this.name,
2317-
hash,
2318-
);
2319-
// After the `await` resolves, another task might fill
2320-
// in the data. If so, we should use that.
2321-
data = this.buckets[idx].data;
2322-
if (data !== null) {
2323-
return (await data)[id - start];
2324-
}
2325-
const dataSansEmptyset = [...dataSansEmptysetOrig];
2326-
/** @type {(Uint8Array[])|null} */
2327-
let dataWithEmptyset = null;
2328-
let pos = start;
2329-
let insertCount = 0;
2330-
while (pos < end) {
2331-
if (this.emptyset.contains(pos)) {
2332-
if (dataWithEmptyset === null) {
2333-
dataWithEmptyset = dataSansEmptyset.splice(0, insertCount);
2334-
} else if (insertCount !== 0) {
2335-
dataWithEmptyset.push(
2336-
...dataSansEmptyset.splice(0, insertCount),
2337-
);
2338-
}
2339-
insertCount = 0;
2340-
dataWithEmptyset.push(EMPTY_UINT8);
2341-
} else {
2342-
insertCount += 1;
2343-
}
2344-
pos += 1;
2345-
}
2346-
data = Promise.resolve(
2347-
dataWithEmptyset === null ?
2348-
dataSansEmptyset :
2349-
dataWithEmptyset.concat(dataSansEmptyset),
2315+
return this.atAsyncFetch(id, start, bucket);
2316+
} else {
2317+
return data[id - start];
2318+
}
2319+
}
2320+
}
2321+
}
2322+
/**
2323+
* Look up a cell by row ID.
2324+
* @param {number} id
2325+
* @param {number} start
2326+
* @param {{hash: Uint8Array, data: Uint8Array[] | null, end: number}} bucket
2327+
* @returns {Promise<Uint8Array>}
2328+
*/
2329+
async atAsyncFetch(id, start, bucket) {
2330+
const {hash, end} = bucket;
2331+
const dataSansEmptysetOrig = await registry.dataLoadByNameAndHash(
2332+
this.name,
2333+
hash,
2334+
);
2335+
// After the `await` resolves, another task might fill
2336+
// in the data. If so, we should use that.
2337+
let data = bucket.data;
2338+
if (data !== null) {
2339+
return data[id - start];
2340+
}
2341+
const dataSansEmptyset = [...dataSansEmptysetOrig];
2342+
/** @type {(Uint8Array[])|null} */
2343+
let dataWithEmptyset = null;
2344+
let pos = start;
2345+
let insertCount = 0;
2346+
while (pos < end) {
2347+
if (this.emptyset.contains(pos)) {
2348+
if (dataWithEmptyset === null) {
2349+
dataWithEmptyset = dataSansEmptyset.splice(0, insertCount);
2350+
} else if (insertCount !== 0) {
2351+
dataWithEmptyset.push(
2352+
...dataSansEmptyset.splice(0, insertCount),
23502353
);
2351-
this.buckets[idx].data = data;
23522354
}
2353-
return (await data)[id - start];
2355+
insertCount = 0;
2356+
dataWithEmptyset.push(EMPTY_UINT8);
2357+
} else {
2358+
insertCount += 1;
23542359
}
2360+
pos += 1;
23552361
}
2362+
data = dataWithEmptyset === null ?
2363+
dataSansEmptyset :
2364+
dataWithEmptyset.concat(dataSansEmptyset);
2365+
bucket.data = data;
2366+
return data[id - start];
23562367
}
23572368
/**
23582369
* Search by exact substring

0 commit comments

Comments
 (0)