cargo-leptos 0.2.43

Build tool for Leptos.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
use std::collections::{HashMap, HashSet, VecDeque};

use super::dep_graph::{DepGraph, DepNode};
use super::read::{ExportId, ImportId, InputFuncId, InputModule, SymbolIndex};
use crate::config::Project;
use crate::internal_prelude::*;
use regex::Regex;
use std::sync::LazyLock;

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct SplitModule {
    pub module_name: String,
    pub load_func: SymbolIndex,
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct SplitPoint {
    pub module_name: String,
    pub import: ImportId,
    pub import_func: InputFuncId,
    pub export: ExportId,
    pub export_func: InputFuncId,
}

pub fn get_split_points(module: &InputModule) -> Result<Vec<SplitPoint>> {
    macro_rules! process_imports_or_exports {
        ($pattern:expr, $map:ident, $member:ident, $id_ty:ty) => {
            let mut $map = HashMap::<(String, String), $id_ty>::new();
            {
                static PATTERN: LazyLock<Regex> = LazyLock::new(|| Regex::new($pattern).unwrap());

                for (id, item) in module.$member.iter().enumerate() {
                    let Some(captures) = PATTERN.captures(&item.name) else {
                        continue;
                    };
                    let (_, [module_name, unique_id]) = captures.extract();
                    $map.insert((module_name.into(), unique_id.into()), id);
                }
            }
        };
    }

    process_imports_or_exports!(
        "__wasm_split_00(.*)00_import_([0-9a-f]{32})",
        import_map,
        imports,
        ImportId
    );
    process_imports_or_exports!(
        "__wasm_split_00(.*)00_export_([0-9a-f]{32})",
        export_map,
        exports,
        ExportId
    );

    let split_points = import_map
        .drain()
        .map(|(key, import_id)| -> Result<SplitPoint> {
            let export_id = export_map
                .remove(&key)
                .ok_or_else(|| anyhow!("No corresponding export for split import {key:?}"))?;
            let export = module.exports[export_id];
            let wasmparser::Export {
                kind: wasmparser::ExternalKind::Func,
                index,
                ..
            } = export
            else {
                bail!("Expected exported function but received: {export:?}");
            };
            let &import_func = module.imported_func_map.get(&import_id).ok_or_else(|| {
                anyhow!(
                    "Expected imported function but received: {:?}",
                    &module.imports[import_id]
                )
            })?;
            Ok(SplitPoint {
                module_name: key.0,
                import: import_id,
                import_func,
                export: export_id,
                export_func: index as InputFuncId,
            })
        })
        .collect::<Result<Vec<SplitPoint>>>()?;

    /* for (key, _) in export_map.iter() {
        bail!("No corresponding import for split export {key:?}");
    } */

    if !export_map.is_empty() {
        bail!(
            "No corresponding imports for split export(s) {:?}",
            export_map.keys().collect::<Vec<_>>()
        );
    }

    Ok(split_points)
}

#[derive(Debug, Default)]
pub struct ReachabilityGraph {
    pub reachable: HashSet<DepNode>,
    pub parents: HashMap<DepNode, DepNode>,
}

#[derive(Debug, Default)]
pub struct OutputModuleInfo {
    pub included_symbols: HashSet<DepNode>,
    pub parents: HashMap<DepNode, DepNode>,
    pub shared_imports: HashSet<InputFuncId>,
    pub split_points: Vec<SplitPoint>,
}

impl OutputModuleInfo {
    pub fn print(&self, module_name: &str, module: &InputModule) {
        print_deps(module_name, module, &self.included_symbols, &self.parents);
    }
}

impl From<ReachabilityGraph> for OutputModuleInfo {
    fn from(reachability: ReachabilityGraph) -> Self {
        Self {
            included_symbols: reachability.reachable,
            parents: reachability.parents,
            ..Default::default()
        }
    }
}

fn print_deps(
    module_name: &str,
    module: &InputModule,
    reachable: &HashSet<DepNode>,
    parents: &HashMap<DepNode, DepNode>,
) {
    let format_dep = |dep: &DepNode| match dep {
        DepNode::Function(index) => {
            let name = module.names.functions.get(index);
            format!("func[{index}] <{name:?}>")
        }
        DepNode::DataSymbol(index) => {
            let symbol = module.symbols[*index];
            format!("{symbol:?}")
        }
    };

    trace!("wasm_split SPLIT: ============== {module_name}");
    let mut total_size: usize = 0;
    for dep in reachable.iter() {
        let DepNode::Function(index) = dep else {
            continue;
        };
        let size = index
            .checked_sub(module.imported_funcs.len())
            .map(|defined_index| module.defined_funcs[defined_index].body.range().len())
            .unwrap_or_default();
        total_size += size;
        trace!("wasm_split    {} size={size:?}", format_dep(dep));
        let mut node = dep;
        while let Some(parent) = parents.get(node) {
            trace!("wasm_split       <== {}", format_dep(parent));
            node = parent;
        }
    }
    trace!("wasm_split SPLIT: ============== {module_name}  : total size: {total_size}");
}

impl ReachabilityGraph {
    #[allow(unused)] // useful to keep this function for future debugging as needed
    pub fn print(&self, module_name: &str, module: &InputModule) {
        print_deps(module_name, module, &self.reachable, &self.parents);
    }
}

pub fn find_reachable_deps(
    deps: &DepGraph,
    roots: &HashSet<DepNode>,
    exclude: &HashSet<DepNode>,
    wb_descriptors: &HashSet<DepNode>,
    uses_wb_descriptor: &mut HashSet<DepNode>,
) -> ReachabilityGraph {
    let mut queue: VecDeque<DepNode> = roots.iter().copied().collect();
    let mut seen = HashSet::<DepNode>::new();
    let mut parents = HashMap::<DepNode, DepNode>::new();
    while let Some(node) = queue.pop_front() {
        seen.insert(node);
        let Some(children) = deps.get(&node) else {
            continue;
        };
        for child in children {
            // if a function calls a wasm-bindgen descriptor, this needs to be
            // expanded during wasm-bindgen, which is only run on the main bundle
            //
            // the caller needs to be moved into the main bundle, so the descriptor will be processed
            // all other functions that the caller calls *also* need to be moved into the main bundle, so
            // they will be available during this processing
            if wb_descriptors.contains(child) {
                uses_wb_descriptor.insert(node);
                uses_wb_descriptor.extend(children.iter().copied());
                continue;
            }
            if seen.contains(child) || exclude.contains(child) {
                continue;
            }
            parents.entry(*child).or_insert(node);
            queue.push_back(*child);
        }
    }
    ReachabilityGraph {
        reachable: seen,
        parents,
    }
}

pub fn get_main_module_roots(
    module: &InputModule,
    split_points: &[SplitPoint],
) -> HashSet<DepNode> {
    let mut roots: HashSet<DepNode> = HashSet::new();
    if let Some(id) = module.start {
        roots.insert(DepNode::Function(id));
    }
    for export in module.exports.iter() {
        let wasmparser::Export {
            index,
            kind: wasmparser::ExternalKind::Func,
            ..
        } = export
        else {
            continue;
        };
        roots.insert(DepNode::Function(*index as usize));
    }
    for func_id in 0..module.imported_funcs.len() {
        roots.insert(DepNode::Function(func_id));
    }
    for split_point in split_points.iter() {
        roots.remove(&DepNode::Function(split_point.export_func));
        roots.remove(&DepNode::Function(split_point.import_func));
    }
    roots
}

pub fn get_split_points_by_module(
    split_points: &[SplitPoint],
) -> HashMap<String, Vec<&SplitPoint>> {
    split_points
        .iter()
        .fold(HashMap::new(), |mut map, split_point| {
            map.entry(split_point.module_name.clone())
                .or_default()
                .push(split_point);
            map
        })
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone)]
pub enum SplitModuleIdentifier {
    Main,
    Split { name: String, hash: String },
    Chunk { splits: Vec<String>, hash: String },
}

impl SplitModuleIdentifier {
    pub fn name(&self, proj: &Project) -> String {
        match self {
            Self::Main => proj.lib.output_name.clone(),
            Self::Split { name, .. } => name.clone(),
            Self::Chunk { splits, .. } => splits.join("_"),
        }
    }

    pub fn name_hashed(&self, proj: &Project) -> String {
        match self {
            Self::Main => proj.lib.output_name.clone(),
            Self::Split { name, hash } => format!("{name}.{hash}"),
            Self::Chunk { splits, hash } => format!("{}.{}", splits.join("_"), hash),
        }
    }

    pub fn set_hash(&mut self, new_hash: String) {
        match self {
            Self::Main => {}
            Self::Split { hash, .. } => *hash = new_hash,
            Self::Chunk { hash, .. } => *hash = new_hash,
        }
    }
}

#[derive(Debug, Default)]
pub struct SplitProgramInfo {
    pub output_modules: Vec<(SplitModuleIdentifier, OutputModuleInfo)>,
    pub output_module_identifiers: HashMap<SplitModuleIdentifier, usize>,
    pub shared_funcs: HashSet<InputFuncId>,
    pub symbol_output_module: HashMap<DepNode, usize>,
}

pub fn compute_split_modules(
    module: &InputModule,
    dep_graph: &DepGraph,
    split_points: &[SplitPoint],
    wb_descriptors: &HashSet<DepNode>,
) -> Result<SplitProgramInfo> {
    let split_points_by_module = get_split_points_by_module(split_points);

    trace!("wasm_split split_points={split_points:?}");

    let split_func_map: HashMap<InputFuncId, InputFuncId> = split_points
        .iter()
        .map(|split_point| (split_point.import_func, split_point.export_func))
        .collect();

    let remove_ignored_deps = |deps: &mut HashSet<DepNode>| {
        for split_point in split_points.iter() {
            deps.remove(&DepNode::Function(split_point.import_func));
        }
    };
    let remove_ignored_funcs = |deps: &mut HashSet<InputFuncId>| {
        for split_point in split_points.iter() {
            deps.remove(&split_point.import_func);
        }
    };

    let main_roots = get_main_module_roots(module, split_points);

    let mut uses_wb_descriptor = HashSet::new();
    let mut main_deps = find_reachable_deps(
        dep_graph,
        &main_roots,
        &HashSet::new(),
        &HashSet::new(),
        &mut uses_wb_descriptor,
    );

    remove_ignored_deps(&mut main_deps.reachable);

    // Determine reachable symbols (excluding main module symbols) for each
    // split module. Symbols may be reachable from more than one split module;
    // these symbols will be moved to a separate module.
    let mut split_module_candidates: HashMap<String, ReachabilityGraph> = split_points_by_module
        .iter()
        .map(|(module_name, entry_points)| {
            let mut roots = HashSet::<DepNode>::new();
            for entry_point in entry_points.iter() {
                roots.insert(DepNode::Function(entry_point.export_func));
            }
            let mut split_functions = find_reachable_deps(
                dep_graph,
                &roots,
                &main_deps.reachable,
                wb_descriptors,
                &mut uses_wb_descriptor,
            );

            remove_ignored_deps(&mut split_functions.reachable);
            (module_name.clone(), split_functions)
        })
        .collect();

    // remove deps that use wasm-bindgen descriptors and put them in the main module
    for (module_name, deps) in split_module_candidates.iter_mut() {
        for dep in &uses_wb_descriptor {
            deps.reachable.remove(dep);
        }
    }

    // Set of split modules from which each symbol is reachable.
    let mut dep_candidate_modules = HashMap::<DepNode, Vec<String>>::new();
    for (module_name, deps) in split_module_candidates.iter() {
        for dep in deps.reachable.iter() {
            dep_candidate_modules
                .entry(*dep)
                .or_default()
                .push(module_name.clone());
        }
    }

    main_deps.reachable.extend(uses_wb_descriptor);

    let mut program_info = SplitProgramInfo::default();

    let mut split_module_contents = HashMap::<SplitModuleIdentifier, OutputModuleInfo>::new();

    split_module_contents.insert(SplitModuleIdentifier::Main, main_deps.into());

    for (dep, mut modules) in dep_candidate_modules {
        if modules.len() > 1 {
            modules.sort();
            for module in modules.iter() {
                let module_contents = split_module_candidates.get_mut(module).unwrap();
                module_contents.reachable.remove(&dep);
            }
            split_module_contents
                .entry(SplitModuleIdentifier::Chunk {
                    splits: modules,
                    // the hash will be computed when emitting each module
                    hash: String::new(),
                })
                .or_default()
                .included_symbols
                .insert(dep);
        }
    }

    split_module_contents.extend(split_module_candidates.drain().map(|(module_name, deps)| {
        (
            SplitModuleIdentifier::Split {
                name: module_name,
                hash: String::new(),
            },
            deps.into(),
        )
    }));

    for contents in split_module_contents.values_mut() {
        for symbol in contents.included_symbols.iter() {
            let Some(neighbors) = dep_graph.get(symbol) else {
                continue;
            };
            for mut called_func_id in neighbors.iter().filter_map(|symbol| match symbol {
                DepNode::Function(func_id) => Some(*func_id),
                _ => None,
            }) {
                called_func_id = *split_func_map
                    .get(&called_func_id)
                    .unwrap_or(&called_func_id);
                if !contents
                    .included_symbols
                    .contains(&DepNode::Function(called_func_id))
                {
                    contents.shared_imports.insert(called_func_id);
                    program_info.shared_funcs.insert(called_func_id);
                }
            }
        }
        remove_ignored_funcs(&mut contents.shared_imports);
    }
    remove_ignored_funcs(&mut program_info.shared_funcs);

    for split_point in split_points {
        program_info.shared_funcs.insert(split_point.export_func);
        let output_module = split_module_contents
            .get_mut(&SplitModuleIdentifier::Split {
                name: split_point.module_name.to_string(),
                hash: String::new(),
            })
            .unwrap();
        output_module.split_points.push(split_point.clone());
    }

    program_info.output_modules = split_module_contents.drain().collect();
    program_info
        .output_modules
        .sort_by_key(|(identifier, _)| (*identifier).clone());
    program_info.output_module_identifiers = program_info
        .output_modules
        .iter()
        .enumerate()
        .map(|(index, (identifier, _))| (identifier.clone(), index))
        .collect();

    for (output_index, (_, info)) in program_info.output_modules.iter().enumerate() {
        for &symbol in info.included_symbols.iter() {
            program_info
                .symbol_output_module
                .insert(symbol, output_index);
        }
    }

    Ok(program_info)
}