@@ -1450,11 +1450,10 @@ class NameTrie {
1450
1450
1451
1451
class DocSearch {
1452
1452
/**
1453
- * @param {Map<string, rustdoc.RawSearchIndexCrate> } rawSearchIndex
1454
1453
* @param {string } rootPath
1455
1454
* @param {rustdoc.SearchState } searchState
1456
1455
*/
1457
- constructor ( rawSearchIndex , rootPath , searchState ) {
1456
+ constructor ( rootPath , searchState ) {
1458
1457
/**
1459
1458
* @type {Map<String, RoaringBitmap> }
1460
1459
*/
@@ -1585,7 +1584,7 @@ class DocSearch {
1585
1584
/**
1586
1585
* @type {Array<rustdoc.Row> }
1587
1586
*/
1588
- this . searchIndex = this . buildIndex ( rawSearchIndex ) ;
1587
+ this . searchIndex = [ ] ;
1589
1588
}
1590
1589
1591
1590
/**
@@ -1911,9 +1910,9 @@ class DocSearch {
1911
1910
* Convert raw search index into in-memory search index.
1912
1911
*
1913
1912
* @param {Map<string, rustdoc.RawSearchIndexCrate> } rawSearchIndex
1914
- * @returns {rustdoc.Row[] }
1913
+ * @returns {Promise< rustdoc.Row[]> }
1915
1914
*/
1916
- buildIndex ( rawSearchIndex ) {
1915
+ async buildIndex ( rawSearchIndex ) {
1917
1916
/**
1918
1917
* Convert from RawFunctionSearchType to FunctionSearchType.
1919
1918
*
@@ -2177,6 +2176,20 @@ class DocSearch {
2177
2176
paths [ i ] = { ty, name, path, exactPath, unboxFlag } ;
2178
2177
}
2179
2178
2179
+ // Throttlers are used to yield to the JavaScript event loop
2180
+ // while this is being built.
2181
+ // They're generated up-front to avoid the "nesting level"
2182
+ // limit that limits our speed to 4ms per tick.
2183
+ // https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html
2184
+ const throttlers = [ ] ;
2185
+ len = itemTypes . length ;
2186
+ for ( let i = 0 ; i < len ; ++ i ) {
2187
+ if ( ( i & 0xFF ) === 0 ) { // 256 - 1
2188
+ throttlers . push ( new Promise ( resolve => {
2189
+ setTimeout ( resolve , 0 ) ;
2190
+ } ) ) ;
2191
+ }
2192
+ }
2180
2193
// convert `item*` into an object form, and construct word indices.
2181
2194
//
2182
2195
// before any analysis is performed lets gather the search terms to
@@ -2189,6 +2202,9 @@ class DocSearch {
2189
2202
let lastName = "" ;
2190
2203
let lastWord = "" ;
2191
2204
for ( let i = 0 ; i < len ; ++ i ) {
2205
+ if ( ( i & 0xFF ) === 0 ) { // 256 - 1
2206
+ await throttlers [ i >> 8 ] ;
2207
+ }
2192
2208
const bitIndex = i + 1 ;
2193
2209
if ( descIndex >= descShard . len &&
2194
2210
// @ts -expect-error
@@ -5377,20 +5393,26 @@ function updateCrate(ev) {
5377
5393
search ( true ) ;
5378
5394
}
5379
5395
5380
- // @ts -expect-error
5381
- function initSearch ( searchIndx ) {
5396
+ /**
5397
+ * @param {Map<string, import("./rustdoc").RawSearchIndexCrate> } searchIndx
5398
+ */
5399
+ async function initSearch ( searchIndx ) {
5400
+ if ( ROOT_PATH === null ) {
5401
+ return ;
5402
+ }
5382
5403
rawSearchIndex = searchIndx ;
5383
5404
if ( typeof window !== "undefined" ) {
5384
- // @ts -expect-error
5385
- docSearch = new DocSearch ( rawSearchIndex , ROOT_PATH , searchState ) ;
5405
+ docSearch = new DocSearch ( ROOT_PATH , window . searchState ) ;
5406
+ docSearch . searchIndex = await docSearch . buildIndex ( rawSearchIndex ) ;
5386
5407
registerSearchEvents ( ) ;
5387
5408
// If there's a search term in the URL, execute the search now.
5388
5409
if ( window . searchState . getQueryStringParams ( ) . search !== undefined ) {
5389
5410
search ( ) ;
5390
5411
}
5391
5412
} else if ( typeof exports !== "undefined" ) {
5392
- // @ts -expect-error
5393
- docSearch = new DocSearch ( rawSearchIndex , ROOT_PATH , searchState ) ;
5413
+ // @ts -ignore
5414
+ docSearch = new DocSearch ( ROOT_PATH , searchState ) ;
5415
+ docSearch . searchIndex = await docSearch . buildIndex ( rawSearchIndex ) ;
5394
5416
exports . docSearch = docSearch ;
5395
5417
exports . parseQuery = DocSearch . parseQuery ;
5396
5418
}
0 commit comments