@@ -14,6 +14,7 @@ use crate::{
14
14
} ;
15
15
use anyhow:: Result ;
16
16
use horrorshow:: { box_html, helper:: doctype, html, prelude:: * } ;
17
+ use rayon:: prelude:: * ;
17
18
use std:: {
18
19
collections:: BTreeMap ,
19
20
ops:: { Deref , DerefMut } ,
@@ -33,6 +34,10 @@ pub const ALL_DOC_FILENAME: &str = "all.html";
33
34
pub const INDEX_FILENAME : & str = "index.html" ;
34
35
pub const IDENTITY : & str = "#" ;
35
36
37
+ type DocLinkMap = BTreeMap < BlockTitle , Vec < DocLink > > ;
38
+ type ModuleMap = BTreeMap < ModulePrefixes , DocLinkMap > ;
39
+ type RenderResult = ( RenderedDocument , ModuleMap , DocLinks ) ;
40
+
36
41
/// Something that can be rendered to HTML.
37
42
pub ( crate ) trait Renderable {
38
43
fn render ( self , render_plan : RenderPlan ) -> Result < Box < dyn RenderBox > > ;
@@ -90,19 +95,36 @@ impl RenderedDocumentation {
90
95
style : DocStyle :: AllDoc ( program_kind. as_title_str ( ) . to_string ( ) ) ,
91
96
links : BTreeMap :: default ( ) ,
92
97
} ;
93
- let mut module_map: BTreeMap < ModulePrefixes , BTreeMap < BlockTitle , Vec < DocLink > > > =
94
- BTreeMap :: new ( ) ;
95
- for doc in raw_docs. 0 {
96
- rendered_docs
97
- . 0
98
- . push ( RenderedDocument :: from_doc ( & doc, render_plan. clone ( ) ) ?) ;
98
+ // Parallel document rendering
99
+ let rendered_results: Result < Vec < RenderResult > , anyhow:: Error > = raw_docs
100
+ . 0
101
+ . par_iter ( )
102
+ . map ( |doc| {
103
+ let rendered_doc = RenderedDocument :: from_doc ( doc, render_plan. clone ( ) ) ?;
104
+ let mut local_module_map = ModuleMap :: new ( ) ;
105
+ let mut local_all_docs = DocLinks {
106
+ style : DocStyle :: AllDoc ( program_kind. as_title_str ( ) . to_string ( ) ) ,
107
+ links : BTreeMap :: default ( ) ,
108
+ } ;
109
+
110
+ populate_decls ( doc, & mut local_module_map) ;
111
+ populate_modules ( doc, & mut local_module_map) ;
112
+ populate_doc_links ( doc, & mut local_all_docs. links ) ;
113
+
114
+ Ok ( ( rendered_doc, local_module_map, local_all_docs) )
115
+ } )
116
+ . collect ( ) ;
99
117
100
- // Here we gather all of the `doc_links` based on which module they belong to.
101
- populate_decls ( & doc, & mut module_map) ;
102
- // Create links to child modules.
103
- populate_modules ( & doc, & mut module_map) ;
104
- // Above we check for the module a link belongs to, here we want _all_ links so the check is much more shallow.
105
- populate_all_doc ( & doc, & mut all_docs) ;
118
+ // Merge results sequentially
119
+ let mut module_map = ModuleMap :: new ( ) ;
120
+ for ( rendered_doc, local_module_map, local_all_docs) in rendered_results? {
121
+ rendered_docs. 0 . push ( rendered_doc) ;
122
+
123
+ for ( key, value) in local_module_map {
124
+ module_map. entry ( key) . or_default ( ) . extend ( value) ;
125
+ }
126
+
127
+ all_docs. links . extend ( local_all_docs. links ) ;
106
128
}
107
129
108
130
// ProjectIndex
@@ -199,7 +221,8 @@ impl DerefMut for RenderedDocumentation {
199
221
}
200
222
}
201
223
202
- fn populate_doc_links ( doc : & Document , doc_links : & mut BTreeMap < BlockTitle , Vec < DocLink > > ) {
224
+ /// Adds a document's link to the appropriate category in the doc links map.
225
+ fn populate_doc_links ( doc : & Document , doc_links : & mut DocLinkMap ) {
203
226
let key = doc. item_body . ty . as_block_title ( ) ;
204
227
match doc_links. get_mut ( & key) {
205
228
Some ( links) => links. push ( doc. link ( ) ) ,
@@ -208,23 +231,19 @@ fn populate_doc_links(doc: &Document, doc_links: &mut BTreeMap<BlockTitle, Vec<D
208
231
}
209
232
}
210
233
}
211
- fn populate_decls (
212
- doc : & Document ,
213
- module_map : & mut BTreeMap < ModulePrefixes , BTreeMap < BlockTitle , Vec < DocLink > > > ,
214
- ) {
234
+ /// Organizes document links by module prefix for navigation.
235
+ fn populate_decls ( doc : & Document , module_map : & mut ModuleMap ) {
215
236
let module_prefixes = & doc. module_info . module_prefixes ;
216
237
if let Some ( doc_links) = module_map. get_mut ( module_prefixes) {
217
238
populate_doc_links ( doc, doc_links)
218
239
} else {
219
- let mut doc_links: BTreeMap < BlockTitle , Vec < DocLink > > = BTreeMap :: new ( ) ;
240
+ let mut doc_links = DocLinkMap :: new ( ) ;
220
241
populate_doc_links ( doc, & mut doc_links) ;
221
242
module_map. insert ( module_prefixes. clone ( ) , doc_links) ;
222
243
}
223
244
}
224
- fn populate_modules (
225
- doc : & Document ,
226
- module_map : & mut BTreeMap < ModulePrefixes , BTreeMap < BlockTitle , Vec < DocLink > > > ,
227
- ) {
245
+ /// Creates links to parent modules for hierarchical navigation.
246
+ fn populate_modules ( doc : & Document , module_map : & mut ModuleMap ) {
228
247
let mut module_clone = doc. module_info . clone ( ) ;
229
248
while module_clone. parent ( ) . is_some ( ) {
230
249
let html_filename = if module_clone. depth ( ) > 2 {
@@ -257,16 +276,13 @@ fn populate_modules(
257
276
}
258
277
}
259
278
} else {
260
- let mut doc_links: BTreeMap < BlockTitle , Vec < DocLink > > = BTreeMap :: new ( ) ;
279
+ let mut doc_links = DocLinkMap :: new ( ) ;
261
280
doc_links. insert ( BlockTitle :: Modules , vec ! [ module_link] ) ;
262
281
module_map. insert ( module_prefixes. clone ( ) , doc_links) ;
263
282
}
264
283
module_clone. module_prefixes . pop ( ) ;
265
284
}
266
285
}
267
- fn populate_all_doc ( doc : & Document , all_docs : & mut DocLinks ) {
268
- populate_doc_links ( doc, & mut all_docs. links ) ;
269
- }
270
286
271
287
/// The finalized HTML file contents.
272
288
#[ derive( Debug ) ]
0 commit comments