wasm-slim 0.1.1

WASM bundle size optimizer
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
//! Custom allocator detection and optimization for WASM
//!
//! Analyzes dependency tree to recommend custom allocator usage when beneficial.
//!
//! # Overview
//!
//! WASM bundles using default system allocator carry overhead. Custom allocators
//! like `wee_alloc` trade allocation speed for size (2-5% reduction).
//!
//! This module:
//! - Detects existing custom allocators (wee_alloc, lol_alloc, dlmalloc)
//! - Counts allocation-heavy dependencies
//! - Recommends allocator when ≥5 allocation-heavy deps present
//!
//! # Examples
//!
//! ```no_run
//! use wasm_slim::analyzer::allocator::AllocatorDetector;
//! use std::path::Path;
//!
//! let detector = AllocatorDetector::new(Path::new("."));
//! if let Ok(Some(recommendation)) = detector.check_allocator_optimization() {
//!     println!("Suggestion: {}", recommendation.suggestion);
//! }
//! ```

use cargo_metadata::MetadataCommand;
use std::path::Path;

use super::deps::{DependencyAnalysisError, DependencyIssue, IssueSeverity};

/// Detects allocator optimization opportunities
pub struct AllocatorDetector {
    project_root: std::path::PathBuf,
}

impl AllocatorDetector {
    /// Create a new allocator detector for the given project
    ///
    /// # Arguments
    ///
    /// * `project_root` - Path to the project root containing Cargo.toml
    pub fn new(project_root: impl AsRef<Path>) -> Self {
        Self {
            project_root: project_root.as_ref().to_path_buf(),
        }
    }

    /// Check if custom allocator optimization would benefit this project
    ///
    /// Returns `Some(DependencyIssue)` if:
    /// - No custom allocator is currently used
    /// - Project has ≥5 allocation-heavy dependencies
    ///
    /// Returns `None` if:
    /// - Custom allocator already present (wee_alloc, lol_alloc, dlmalloc)
    /// - Too few allocation-heavy dependencies (<5)
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use wasm_slim::analyzer::allocator::AllocatorDetector;
    /// # use std::path::Path;
    /// let detector = AllocatorDetector::new(Path::new("."));
    /// match detector.check_allocator_optimization() {
    ///     Ok(Some(issue)) => println!("Recommendation: {}", issue.suggestion),
    ///     Ok(None) => println!("Allocator already optimized"),
    ///     Err(e) => eprintln!("Analysis failed: {}", e),
    /// }
    /// ```
    pub fn check_allocator_optimization(
        &self,
    ) -> Result<Option<DependencyIssue>, DependencyAnalysisError> {
        let metadata = MetadataCommand::new()
            .current_dir(&self.project_root)
            .exec()?;

        self.check_allocator_optimization_with_metadata(&metadata)
    }

    /// Check allocator optimization with provided metadata (for testing)
    ///
    /// # Arguments
    ///
    /// * `metadata` - Cargo metadata to analyze
    ///
    /// # Returns
    ///
    /// `Some(DependencyIssue)` if custom allocator would benefit the project, `None` otherwise
    pub(crate) fn check_allocator_optimization_with_metadata(
        &self,
        metadata: &cargo_metadata::Metadata,
    ) -> Result<Option<DependencyIssue>, DependencyAnalysisError> {
        // Check if custom allocator already present
        let has_custom_allocator = metadata
            .packages
            .iter()
            .any(|p| matches!(p.name.as_str(), "wee_alloc" | "lol_alloc" | "dlmalloc"));

        if has_custom_allocator {
            return Ok(None); // Already optimized
        }

        // Count allocation-heavy dependencies
        let heavy_alloc_deps = self.count_allocation_heavy_deps(metadata);

        // Recommend if >= 5 allocation-heavy deps (heuristic threshold)
        if heavy_alloc_deps >= 5 {
            let estimated_savings_kb = self.estimate_allocator_savings(heavy_alloc_deps);

            Ok(Some(DependencyIssue {
                package: "wasm-allocator".to_string(),
                version: "n/a".to_string(),
                severity: IssueSeverity::Medium,
                issue: format!(
                    "No custom allocator detected. Project has {} allocation-heavy dependencies using default system allocator.",
                    heavy_alloc_deps
                ),
                suggestion: "Add wee_alloc for 2-5% size reduction:\n\n   [dependencies]\n   wee_alloc = \"0.4.5\"\n\n   And configure global allocator in lib.rs:\n   #[cfg(target_arch = \"wasm32\")]\n   #[global_allocator]\n   static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;\n\n   Note: wee_alloc trades smaller size for slightly slower allocation performance.".to_string(),
                size_impact_kb: Some(estimated_savings_kb),
                savings_percent: Some(3), // Conservative 3% estimate
            }))
        } else {
            Ok(None) // Not enough heap usage to warrant custom allocator
        }
    }

    /// Count dependencies that do heavy heap allocation
    ///
    /// Uses heuristics to identify crates likely to allocate frequently:
    /// - String/text processing (regex, serde_json, toml, etc.)
    /// - Collections (vec-heavy, tree structures)
    /// - Parsing/compiling (parsers, compilers)
    /// - Data structures (btree, linked lists, etc.)
    fn count_allocation_heavy_deps(&self, metadata: &cargo_metadata::Metadata) -> usize {
        metadata
            .packages
            .iter()
            .filter(|p| Self::is_allocation_heavy(&p.name))
            .count()
    }

    /// Check if a crate is known to do heavy heap allocation
    ///
    /// This is a heuristic based on common patterns:
    /// - String manipulation crates
    /// - JSON/serialization
    /// - Parsers and compilers
    /// - Collection-heavy libraries
    pub(crate) fn is_allocation_heavy(crate_name: &str) -> bool {
        matches!(
            crate_name,
            // String/text processing
            "regex" | "regex-syntax" | "fancy-regex" |
            // Serialization (creates many temporary objects)
            "serde_json" | "serde_yaml" | "toml" | "toml_edit" | "ron" |
            // Parsers (build ASTs in memory)
            "syn" | "swc_core" | "swc_ecma_parser" | "nom" | "pest" | "lalrpop-util" |
            // HTML/XML
            "html5ever" | "xml-rs" | "quick-xml" |
            // Data structures
            "petgraph" | "indexmap" | "linked-hash-map" |
            // String formatting
            "format_bytes" | "indoc" |
            // Web frameworks (session management, request buffers)
            "actix-web" | "axum" | "warp" | "rocket" |
            // Database (query builders, ORMs)
            "sqlx" | "diesel" | "sea-orm" |
            // Crypto (work buffers)
            "openssl" | "rustls" | "ring" |
            // Compression (buffers)
            "flate2" | "brotli" | "zstd" |
            // Template engines
            "tera" | "handlebars" | "askama" |
            // CLI (parsing, help text)
            "clap" | "structopt"
        )
    }

    /// Estimate allocator savings based on allocation-heavy dependency count
    ///
    /// Returns (min_kb, max_kb) tuple
    ///
    /// Heuristic formula:
    /// - Base savings: 10-20KB (allocator overhead)
    /// - Per-dependency factor: 2-4KB per allocation-heavy dep
    /// - Capped at reasonable maximum (100KB)
    fn estimate_allocator_savings(&self, heavy_dep_count: usize) -> (u32, u32) {
        let base_min = 10;
        let base_max = 20;
        let per_dep_min = 2;
        let per_dep_max = 4;

        let min_kb = (base_min + (heavy_dep_count as u32 * per_dep_min)).min(50);
        let max_kb = (base_max + (heavy_dep_count as u32 * per_dep_max)).min(100);

        (min_kb, max_kb)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use cargo_metadata::Metadata;

    #[test]
    fn test_is_allocation_heavy_detects_known_crates() {
        assert!(AllocatorDetector::is_allocation_heavy("regex"));
        assert!(AllocatorDetector::is_allocation_heavy("serde_json"));
        assert!(AllocatorDetector::is_allocation_heavy("syn"));
        assert!(AllocatorDetector::is_allocation_heavy("clap"));
        assert!(!AllocatorDetector::is_allocation_heavy("unknown-crate"));
    }

    #[test]
    fn test_estimate_allocator_savings_scales_with_deps() {
        let detector = AllocatorDetector::new(".");

        // Few deps - minimal savings
        let (min_5, max_5) = detector.estimate_allocator_savings(5);
        assert!(min_5 >= 10);
        assert!(max_5 <= 40);
        assert!(min_5 < max_5);

        // Many deps - more savings
        let (min_20, max_20) = detector.estimate_allocator_savings(20);
        assert!(min_20 > min_5);
        assert!(max_20 > max_5);

        // Cap at reasonable max
        let (min_100, max_100) = detector.estimate_allocator_savings(100);
        assert!(min_100 <= 50);
        assert!(max_100 <= 100);
    }

    #[test]
    fn test_check_allocator_optimization_with_custom_allocator_returns_none() {
        // Create mock metadata with wee_alloc present
        let json = r#"{
            "packages": [
                {
                    "name": "wee_alloc",
                    "version": "0.4.5",
                    "id": "wee_alloc 0.4.5",
                    "license": "MIT",
                    "license_file": null,
                    "description": "Small allocator",
                    "source": null,
                    "dependencies": [],
                    "targets": [],
                    "features": {},
                    "manifest_path": "/fake/Cargo.toml",
                    "metadata": null,
                    "publish": null,
                    "authors": [],
                    "categories": [],
                    "keywords": [],
                    "readme": null,
                    "repository": null,
                    "homepage": null,
                    "documentation": null,
                    "edition": "2021",
                    "links": null,
                    "default_run": null,
                    "rust_version": null
                },
                {
                    "name": "regex",
                    "version": "1.0.0",
                    "id": "regex 1.0.0",
                    "license": "MIT",
                    "license_file": null,
                    "description": "Regex",
                    "source": null,
                    "dependencies": [],
                    "targets": [],
                    "features": {},
                    "manifest_path": "/fake/Cargo.toml",
                    "metadata": null,
                    "publish": null,
                    "authors": [],
                    "categories": [],
                    "keywords": [],
                    "readme": null,
                    "repository": null,
                    "homepage": null,
                    "documentation": null,
                    "edition": "2021",
                    "links": null,
                    "default_run": null,
                    "rust_version": null
                }
            ],
            "workspace_members": [],
            "workspace_default_members": [],
            "resolve": null,
            "target_directory": "/fake/target",
            "version": 1,
            "workspace_root": "/fake",
            "metadata": null
        }"#;

        let metadata: Metadata = serde_json::from_str(json).unwrap();
        let detector = AllocatorDetector::new(".");

        // Should return None since wee_alloc is present
        let result = detector
            .check_allocator_optimization_with_metadata(&metadata)
            .unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn test_check_allocator_optimization_with_few_deps_returns_none() {
        // Create mock metadata with only 2 allocation-heavy deps (below threshold)
        let json = r#"{
            "packages": [
                {
                    "name": "regex",
                    "version": "1.0.0",
                    "id": "regex 1.0.0",
                    "license": "MIT",
                    "license_file": null,
                    "description": "Regex",
                    "source": null,
                    "dependencies": [],
                    "targets": [],
                    "features": {},
                    "manifest_path": "/fake/Cargo.toml",
                    "metadata": null,
                    "publish": null,
                    "authors": [],
                    "categories": [],
                    "keywords": [],
                    "readme": null,
                    "repository": null,
                    "homepage": null,
                    "documentation": null,
                    "edition": "2021",
                    "links": null,
                    "default_run": null,
                    "rust_version": null
                },
                {
                    "name": "serde_json",
                    "version": "1.0.0",
                    "id": "serde_json 1.0.0",
                    "license": "MIT",
                    "license_file": null,
                    "description": "JSON",
                    "source": null,
                    "dependencies": [],
                    "targets": [],
                    "features": {},
                    "manifest_path": "/fake/Cargo.toml",
                    "metadata": null,
                    "publish": null,
                    "authors": [],
                    "categories": [],
                    "keywords": [],
                    "readme": null,
                    "repository": null,
                    "homepage": null,
                    "documentation": null,
                    "edition": "2021",
                    "links": null,
                    "default_run": null,
                    "rust_version": null
                }
            ],
            "workspace_members": [],
            "workspace_default_members": [],
            "resolve": null,
            "target_directory": "/fake/target",
            "version": 1,
            "workspace_root": "/fake",
            "metadata": null
        }"#;

        let metadata: Metadata = serde_json::from_str(json).unwrap();
        let detector = AllocatorDetector::new(".");

        // Should return None since < 5 allocation-heavy deps
        let result = detector
            .check_allocator_optimization_with_metadata(&metadata)
            .unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn test_check_allocator_optimization_with_many_deps_returns_issue() {
        // Create mock metadata with 6 allocation-heavy deps (above threshold)
        let json = r#"{
            "packages": [
                {"name": "regex", "version": "1.0.0", "id": "regex 1.0.0", "license": "MIT", "license_file": null, "description": "Regex", "source": null, "dependencies": [], "targets": [], "features": {}, "manifest_path": "/fake/Cargo.toml", "metadata": null, "publish": null, "authors": [], "categories": [], "keywords": [], "readme": null, "repository": null, "homepage": null, "documentation": null, "edition": "2021", "links": null, "default_run": null, "rust_version": null},
                {"name": "serde_json", "version": "1.0.0", "id": "serde_json 1.0.0", "license": "MIT", "license_file": null, "description": "JSON", "source": null, "dependencies": [], "targets": [], "features": {}, "manifest_path": "/fake/Cargo.toml", "metadata": null, "publish": null, "authors": [], "categories": [], "keywords": [], "readme": null, "repository": null, "homepage": null, "documentation": null, "edition": "2021", "links": null, "default_run": null, "rust_version": null},
                {"name": "toml", "version": "1.0.0", "id": "toml 1.0.0", "license": "MIT", "license_file": null, "description": "TOML", "source": null, "dependencies": [], "targets": [], "features": {}, "manifest_path": "/fake/Cargo.toml", "metadata": null, "publish": null, "authors": [], "categories": [], "keywords": [], "readme": null, "repository": null, "homepage": null, "documentation": null, "edition": "2021", "links": null, "default_run": null, "rust_version": null},
                {"name": "syn", "version": "1.0.0", "id": "syn 1.0.0", "license": "MIT", "license_file": null, "description": "Parser", "source": null, "dependencies": [], "targets": [], "features": {}, "manifest_path": "/fake/Cargo.toml", "metadata": null, "publish": null, "authors": [], "categories": [], "keywords": [], "readme": null, "repository": null, "homepage": null, "documentation": null, "edition": "2021", "links": null, "default_run": null, "rust_version": null},
                {"name": "clap", "version": "4.0.0", "id": "clap 4.0.0", "license": "MIT", "license_file": null, "description": "CLI", "source": null, "dependencies": [], "targets": [], "features": {}, "manifest_path": "/fake/Cargo.toml", "metadata": null, "publish": null, "authors": [], "categories": [], "keywords": [], "readme": null, "repository": null, "homepage": null, "documentation": null, "edition": "2021", "links": null, "default_run": null, "rust_version": null},
                {"name": "html5ever", "version": "1.0.0", "id": "html5ever 1.0.0", "license": "MIT", "license_file": null, "description": "HTML parser", "source": null, "dependencies": [], "targets": [], "features": {}, "manifest_path": "/fake/Cargo.toml", "metadata": null, "publish": null, "authors": [], "categories": [], "keywords": [], "readme": null, "repository": null, "homepage": null, "documentation": null, "edition": "2021", "links": null, "default_run": null, "rust_version": null}
            ],
            "workspace_members": [],
            "workspace_default_members": [],
            "resolve": null,
            "target_directory": "/fake/target",
            "version": 1,
            "workspace_root": "/fake",
            "metadata": null
        }"#;

        let metadata: Metadata = serde_json::from_str(json).unwrap();
        let detector = AllocatorDetector::new(".");

        // Should return Some since >= 5 allocation-heavy deps and no custom allocator
        let result = detector
            .check_allocator_optimization_with_metadata(&metadata)
            .unwrap();
        assert!(result.is_some());

        let issue = result.unwrap();
        assert_eq!(issue.package, "wasm-allocator");
        assert_eq!(issue.severity, IssueSeverity::Medium);
        assert!(issue.suggestion.contains("wee_alloc"));
        assert!(issue.size_impact_kb.is_some());
        assert_eq!(issue.savings_percent, Some(3));
    }

    #[test]
    fn test_allocator_detection_recognizes_all_variants() {
        // Test with lol_alloc (alternative allocator)
        let json_lol = r#"{
            "packages": [
                {
                    "name": "lol_alloc",
                    "version": "0.4.0",
                    "id": "lol_alloc 0.4.0",
                    "license": "MIT",
                    "license_file": null,
                    "description": "Allocator",
                    "source": null,
                    "dependencies": [],
                    "targets": [],
                    "features": {},
                    "manifest_path": "/fake/Cargo.toml",
                    "metadata": null,
                    "publish": null,
                    "authors": [],
                    "categories": [],
                    "keywords": [],
                    "readme": null,
                    "repository": null,
                    "homepage": null,
                    "documentation": null,
                    "edition": "2021",
                    "links": null,
                    "default_run": null,
                    "rust_version": null
                }
            ],
            "workspace_members": [],
            "workspace_default_members": [],
            "resolve": null,
            "target_directory": "/fake/target",
            "version": 1,
            "workspace_root": "/fake",
            "metadata": null
        }"#;

        let metadata: Metadata = serde_json::from_str(json_lol).unwrap();
        let detector = AllocatorDetector::new(".");

        // Should return None - lol_alloc is recognized as custom allocator
        assert!(detector
            .check_allocator_optimization_with_metadata(&metadata)
            .unwrap()
            .is_none());
    }

    #[test]
    fn test_count_allocation_heavy_deps_accuracy() {
        // Mix of heavy and normal deps
        let json = r#"{
            "packages": [
                {"name": "regex", "version": "1.0.0", "id": "regex 1.0.0", "license": "MIT", "license_file": null, "description": "Regex", "source": null, "dependencies": [], "targets": [], "features": {}, "manifest_path": "/fake/Cargo.toml", "metadata": null, "publish": null, "authors": [], "categories": [], "keywords": [], "readme": null, "repository": null, "homepage": null, "documentation": null, "edition": "2021", "links": null, "default_run": null, "rust_version": null},
                {"name": "serde", "version": "1.0.0", "id": "serde 1.0.0", "license": "MIT", "license_file": null, "description": "Serde", "source": null, "dependencies": [], "targets": [], "features": {}, "manifest_path": "/fake/Cargo.toml", "metadata": null, "publish": null, "authors": [], "categories": [], "keywords": [], "readme": null, "repository": null, "homepage": null, "documentation": null, "edition": "2021", "links": null, "default_run": null, "rust_version": null},
                {"name": "serde_json", "version": "1.0.0", "id": "serde_json 1.0.0", "license": "MIT", "license_file": null, "description": "JSON", "source": null, "dependencies": [], "targets": [], "features": {}, "manifest_path": "/fake/Cargo.toml", "metadata": null, "publish": null, "authors": [], "categories": [], "keywords": [], "readme": null, "repository": null, "homepage": null, "documentation": null, "edition": "2021", "links": null, "default_run": null, "rust_version": null},
                {"name": "anyhow", "version": "1.0.0", "id": "anyhow 1.0.0", "license": "MIT", "license_file": null, "description": "Error", "source": null, "dependencies": [], "targets": [], "features": {}, "manifest_path": "/fake/Cargo.toml", "metadata": null, "publish": null, "authors": [], "categories": [], "keywords": [], "readme": null, "repository": null, "homepage": null, "documentation": null, "edition": "2021", "links": null, "default_run": null, "rust_version": null}
            ],
            "workspace_members": [],
            "workspace_default_members": [],
            "resolve": null,
            "target_directory": "/fake/target",
            "version": 1,
            "workspace_root": "/fake",
            "metadata": null
        }"#;

        let metadata: Metadata = serde_json::from_str(json).unwrap();
        let detector = AllocatorDetector::new(".");

        let count = detector.count_allocation_heavy_deps(&metadata);
        assert_eq!(count, 2); // regex and serde_json
    }
}