cargo-mate 1.8.0

Rust development companion that enhances cargo with intelligent workflows, state management, performance optimization, and comprehensive project monitoring.
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
## Build Optimization Commands

The build optimization system automatically applies performance-enhancing configurations to Cargo.toml files. This feature implements comprehensive build performance optimization with multiple profiles, automatic backup/restore functionality, and CPU-aware recommendations.

### Key Features
- **Multiple Profiles**: Aggressive, Balanced, Conservative, and Custom optimization profiles
- **Automatic Backups**: Creates backup of Cargo.toml before modifications
- **CPU-Aware**: Automatically detects CPU cores for optimal job count recommendations
- **Safe Operation**: One-click restore functionality from backups
- **Performance Tracking**: Shows optimization impact and recommendations

### How It Works

The optimization system modifies your `Cargo.toml` to add performance-enhancing settings:

```toml
[build]
jobs = 8                    # Number of parallel jobs
incremental = true          # Enable incremental compilation

[profile.dev]
opt-level = 1              # Basic optimizations for faster runtime
debug = 1                  # Reduced debug info for faster builds
codegen-units = 256        # More parallelism during compilation
lto = false                # Disable link-time optimization for dev builds
```

### `cm optimize aggressive`
**Description**: Apply aggressive optimizations for maximum build speed

**Usage**:
```bash
cm optimize aggressive
```

**What it applies**:
- Parallel jobs: CPU core count (8 on 8-core system)
- Incremental compilation: true
- Optimization level: 1 (basic optimizations)
- Codegen units: 256 (maximum parallelism)
- Debug level: 1 (reduced debug info)
- LTO: false (faster dev builds)

**Output**:
```
📋 Backed up Cargo.toml to Cargo.toml.backup
✅ Applied Aggressive optimizations to Cargo.toml

🚀 Build Optimization Summary:
══════════════════════════════════════════════════
📊 Parallel Jobs: 8
🔄 Incremental: true
⚡ Optimization Level: 1
🐛 Debug Level: 1
🏗️  Codegen Units: 256
🔗 Link-Time Optimization: false

🌍 Environment Variables:
  CARGO_BUILD_JOBS = "8"
  CARGO_INCREMENTAL = "1"
══════════════════════════════════════════════════
💡 Run 'cargo build' to see the speed improvements!
```

---

### `cm optimize balanced`
**Description**: Apply balanced optimizations for good speed/stability

**Usage**:
```bash
cm optimize balanced
```

**What it applies**:
- Parallel jobs: CPU core count / 2 (4 on 8-core system)
- Incremental compilation: true
- Optimization level: 1 (basic optimizations)
- Codegen units: 128 (moderate parallelism)
- Debug level: 1 (reduced debug info)
- LTO: false (faster dev builds)

---

### `cm optimize conservative`
**Description**: Apply conservative optimizations for maximum stability

**Usage**:
```bash
cm optimize conservative
```

**What it applies**:
- Parallel jobs: 2
- Incremental compilation: true
- Optimization level: 0 (no optimizations)
- Codegen units: 64 (minimal parallelism)
- Debug level: 2 (full debug info)
- LTO: false (faster dev builds)

---

### `cm optimize custom [JOBS] [INCREMENTAL] [OPT_LEVEL] [DEBUG_LEVEL] [CODEGEN_UNITS]`
**Description**: Apply custom optimizations with specific values

**Usage**:
```bash
cm optimize custom 16 true 2 1 512
```

**Parameters**:
- `JOBS`: Number of parallel jobs (default: 4)
- `INCREMENTAL`: Enable incremental compilation (default: true)
- `OPT_LEVEL`: Optimization level 0-3 (default: 1)
- `DEBUG_LEVEL`: Debug level 0-2 (default: 1)
- `CODEGEN_UNITS`: Codegen units for parallelism (default: 128)

---

### `cm optimize status`
**Description**: Show current optimization status

**Usage**:
```bash
cm optimize status
```

**Output**:
```
🔍 Current Build Optimization Status:
══════════════════════════════════════════════════
📊 Build Configuration:
  incremental: true
  jobs: 8

⚡ Dev Profile:
  codegen-units: 256
  debug: 1
  lto: false
  opt-level: 1

🌍 Environment Variables:
  CARGO_BUILD_JOBS: "8"
  CARGO_INCREMENTAL: "1"
══════════════════════════════════════════════════
```

---

### `cm optimize recommendations`
**Description**: Show optimization recommendations based on your system

**Usage**:
```bash
cm optimize recommendations
```

**Output**:
```
💡 Build Optimization Recommendations:
══════════════════════════════════════════════════
🖥️  CPU Cores: 8
📊 Recommended Jobs: 8

🚀 Aggressive Profile:
  - Parallel jobs: 8
  - Incremental: true
  - Opt level: 1 (basic optimizations)
  - Codegen units: 256 (maximum parallelism)
  - Debug: 1 (reduced debug info)

⚖️  Balanced Profile:
  - Parallel jobs: 4
  - Incremental: true
  - Opt level: 1 (basic optimizations)
  - Codegen units: 128 (moderate parallelism)
  - Debug: 1 (reduced debug info)

🛡️  Conservative Profile:
  - Parallel jobs: 2
  - Incremental: true
  - Opt level: 0 (no optimizations)
  - Codegen units: 64 (minimal parallelism)
  - Debug: 2 (full debug info)

💡 Use 'cm optimize aggressive' for maximum speed
💡 Use 'cm optimize balanced' for good speed/stability
💡 Use 'cm optimize conservative' for maximum stability
```

---

### `cm optimize restore`
**Description**: Restore original Cargo.toml from backup

**Usage**:
```bash
cm optimize restore
```

**Output**:
```
✅ Restored Cargo.toml from backup
```

---

## Performance Impact

The optimizations provide significant build speed improvements:

### Expected Improvements
- **Parallel Compilation**: Up to 8x faster on 8-core systems
- **Incremental Builds**: Subsequent builds are significantly faster
- **Optimized Codegen**: Better utilization of CPU resources
- **Reduced Debug Overhead**: Faster runtime during development

### Typical Performance Gains
- **Aggressive Profile**: 40-60% faster builds on multi-core systems
- **Balanced Profile**: 20-30% faster builds with better stability
- **Conservative Profile**: Minimal speedup but maximum stability

## Best Practices

### Choosing a Profile

**Use Aggressive When:**
- You have a powerful multi-core machine
- Build times are a bottleneck
- You're doing rapid development iterations
- You can tolerate occasional instability

**Use Balanced When:**
- You want good speed without sacrificing stability
- You're working on a team project
- You need reliable incremental builds
- You have a mid-range development machine

**Use Conservative When:**
- Stability is more important than speed
- You're debugging complex issues
- You need full debug information
- You're on a resource-constrained system

### Workflow Integration

```bash
# Initial setup - get recommendations
cm optimize recommendations

# Apply aggressive optimizations
cm optimize aggressive

# Check status
cm optimize status

# If issues occur, restore
cm optimize restore

# Try balanced instead
cm optimize balanced
```

## Troubleshooting

### Build Failures After Optimization

```bash
# Restore original configuration
cm optimize restore

# Try a more conservative profile
cm optimize conservative

# Check for specific issues
cargo check --verbose
```

### Performance Not Improving

```bash
# Verify optimization is applied
cm optimize status

# Check CPU core count
cm optimize recommendations

# Try custom settings
cm optimize custom 16 true 2 1 512
```

### Backup File Missing

```bash
# Check if backup exists
ls -la Cargo.toml.backup

# If missing, create manual backup before optimizing
cp Cargo.toml Cargo.toml.backup
cm optimize aggressive
```

## Technical Details

### Backup System
- **Location**: `Cargo.toml.backup` in project root
- **Creation**: Automatic before any modifications
- **Restore**: One-command restore functionality
- **Safety**: Non-destructive operations with rollback capability

### CPU Detection
- **Automatic**: Detects available CPU cores
- **Recommendations**: Provides optimal job count suggestions
- **System-Specific**: Tailored optimization suggestions per system

### Environment Variables
The system sets these environment variables for cargo:
- `CARGO_BUILD_JOBS`: Number of parallel jobs
- `CARGO_INCREMENTAL`: Force incremental compilation

### Integration with Existing System
- **Version Management**: Works alongside auto-versioning
- **Display System**: Enhanced build feedback with optimizations
- **View System**: Can show optimization status in build results
- **Configuration Management**: Respects existing project settings

## Advanced Usage

### Custom Optimization Profiles

Create your own optimization profile:

```bash
# Custom: 16 jobs, opt-level 2, 512 codegen units
cm optimize custom 16 true 2 1 512

# Verify custom settings
cm optimize status
```

### Profile Templates (Future)
Save custom profiles for reuse:
```bash
# Future feature
cm optimize save-profile my-profile
cm optimize load-profile my-profile
```

### CI/CD Integration

```bash
# In CI pipeline
cm optimize aggressive
cargo build --release
cm optimize restore  # Restore after build
```

## Performance Monitoring

### Track Build Time Improvements

```bash
# Before optimization
time cargo build

# Apply optimization
cm optimize aggressive

# After optimization
time cargo build

# Compare results
```

### Monitor Resource Usage

```bash
# Check CPU utilization during build
top -p $(pgrep -f cargo)

# Monitor memory usage
cargo build --verbose 2>&1 | grep -i memory
```

## Limitations

### Known Limitations
- **Profile Persistence**: Custom profiles not yet saved between sessions
- **Workspace Support**: Limited workspace-level optimization
- **Cross-Compilation**: Some optimizations may not apply to cross-compilation targets

### Platform Considerations
- **Linux**: Full optimization support
- **macOS**: Full optimization support
- **Windows**: Full optimization support with some limitations on job count

## Future Enhancements

Planned improvements:
- **Profile Templates**: Save custom profiles for reuse
- **Performance Monitoring**: Track build time improvements over time
- **Conditional Optimization**: Apply different profiles for different build types
- **Team Sharing**: Share optimization profiles across team members
- **Workspace Support**: Workspace-level optimization configuration

---