memscope-rs 0.2.1

A memory tracking library for Rust applications.
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
# Architecture Overview

## Architecture Evolution

This document describes the architectural improvements in the `improve` branch compared to the `master` branch.

## Three-Layer Object Model

The most significant architectural improvement is the implementation of a **Three-Layer Object Model** for memory tracking.

### Architecture Diagram

```mermaid
graph TB
    subgraph "Layer 1: User API"
        A[tracker! macro] --> B[Global Tracker]
        A --> C[Local Tracker]
    end
    
    subgraph "Layer 2: Core Engine"
        B --> D[Capture Backend]
        C --> D
        D --> E{Backend Type}
        E -->|Sync| F[Core Backend]
        E -->|Async| G[Async Backend]
        E -->|Lockfree| H[Lockfree Backend]
        E -->|Unified| I[Unified Backend]
    end
    
    subgraph "Layer 3: Analysis Engine"
        F --> J[Event Store]
        G --> J
        H --> J
        I --> J
        J --> K[Analysis Engine]
        K --> L[Detectors]
        K --> M[Classifiers]
        K --> N[Metrics]
    end
    
    subgraph "Output Layer"
        L --> O[Reports]
        M --> O
        N --> O
        O --> P[Dashboard]
        O --> Q[Timeline]
        O --> R[Snapshots]
    end
```

## Key Architectural Improvements

### 1. Unified Node Identity System

**Before (master)**:

- Multiple identity systems for different components
- Inconsistent tracking across modules
- Difficult to correlate related memory events

**After (improve)**:

- Single unified `NodeId` system
- Consistent identity across all components
- Easy correlation of memory events

```mermaid
graph LR
    A[Memory Allocation] -->|creates| B[NodeId]
    B --> C[Event Store]
    B --> D[Ownership Graph]
    B --> E[Timeline]
    B --> F[Snapshot]
    
    style B fill:#f9f,stroke:#333,stroke-width:4px
```

### 2. Modular Backend System

**Before (master)**:

- Monolithic tracking implementation
- Limited backend options
- Difficult to extend

**After (improve)**:

- Pluggable backend architecture
- Multiple backend types:
  - **Core Backend**: Synchronous, low-latency (\~21ns)
  - **Async Backend**: Asynchronous support (\~21ns)
  - **Lockfree Backend**: High concurrency (\~40ns)
  - **Unified Backend**: Unified interface (\~40ns)

```mermaid
graph TB
    subgraph "Backend Interface"
        A[CaptureBackend Trait]
    end
    
    subgraph "Implementations"
        B[Core Backend]
        C[Async Backend]
        D[Lockfree Backend]
        E[Unified Backend]
    end
    
    A --> B
    A --> C
    A --> D
    A --> E
    
    B --> F[Single-threaded]
    B --> G[Low-latency]
    C --> H[Async runtime]
    D --> I[High concurrency]
    E --> J[Unified API]
```

### 3. Event-Driven Architecture

**Before (master)**:

- Direct coupling between components
- Difficult to add new features
- Limited extensibility

**After (improve)**:

- Event-driven architecture with centralized `EventStore`
- Loose coupling between components
- Easy to add new analysis modules

```mermaid
graph TB
    A[Memory Events] --> B[Event Store]
    B --> C[Analysis Engine]
    B --> D[Query Engine]
    B --> E[Render Engine]
    B --> F[Timeline Engine]
    
    C --> G[Detectors]
    C --> H[Classifiers]
    C --> I[Metrics]
    
    D --> J[Queries]
    E --> K[Dashboard]
    E --> L[Reports]
    F --> M[Timeline]
    F --> N[Replay]
```

### 4. Comprehensive Analysis Modules

**New Analysis Capabilities**:

```mermaid
graph TB
    A[Analysis Engine] --> B[Detectors]
    A --> C[Classifiers]
    A --> D[Metrics]
    A --> E[Quality]
    
    B --> B1[Leak Detector]
    B --> B2[UAF Detector]
    B --> B3[Overflow Detector]
    B --> B4[Lifecycle Detector]
    B --> B5[Safety Detector]
    
    C --> C1[Type Classifier]
    C --> C2[Pattern Matcher]
    C --> C3[Rule Engine]
    
    D --> D1[Performance Metrics]
    D --> D2[Memory Metrics]
    D --> D3[Concurrency Metrics]
    
    E --> E1[Code Quality]
    E --> E2[Memory Quality]
    E --> E3[Safety Quality]
```

### 5. Enhanced Capture System

**Before (master)**:

- Basic allocation tracking
- Limited type information
- No lifecycle tracking

**After (improve)**:

- Comprehensive capture system with:
  - Allocation tracking
  - Deallocation tracking
  - Reallocation tracking
  - Move tracking
  - Lifecycle events
  - Type information
  - Stack traces
  - Thread information

```mermaid
graph TB
    A[Memory Operation] --> B[Capture System]
    B --> C{Operation Type}
    
    C -->|Alloc| D[Allocation Info]
    C -->|Dealloc| E[Deallocation Info]
    C -->|Realloc| F[Reallocation Info]
    C -->|Move| G[Move Info]
    
    D --> H[Type Info]
    D --> I[Stack Trace]
    D --> J[Thread Info]
    D --> K[Timestamp]
    
    E --> H
    E --> I
    E --> J
    E --> K
    
    F --> H
    F --> I
    F --> J
    F --> K
    
    G --> H
    G --> I
    G --> J
    G --> K
```

## Module Organization

### Current Architecture (improve branch)

```
memscope-rs/
├── src/
│   ├── core/                    # Core tracking functionality
│   │   ├── tracker/            # Tracker implementations
│   │   ├── types/              # Core types (TrackKind, HeapPtr)
│   │   ├── allocator.rs        # Custom allocator
│   │   └── safe_operations.rs  # Safe operation wrappers
│   │
│   ├── capture/                 # Memory capture system
│   │   ├── backends/           # Pluggable backends
│   │   ├── platform/           # Platform-specific code
│   │   └── types/              # Capture types
│   │
│   ├── analysis/               # Analysis modules
│   │   ├── detectors/          # Memory issue detectors
│   │   ├── classification/     # Type classification
│   │   ├── lifecycle/          # Lifecycle analysis
│   │   ├── metrics/            # Metrics collection
│   │   └── quality/            # Quality analysis
│   │
│   ├── analysis_engine/        # Unified analysis engine
│   ├── event_store/            # Centralized event storage
│   ├── query/                  # Query engine
│   ├── render_engine/          # Output rendering
│   ├── snapshot/               # Snapshot system
│   ├── timeline/               # Timeline engine
│   ├── view/                   # Read-only views
│   ├── facade/                 # Unified API
│   └── metadata/               # Metadata management
```

## Performance Characteristics

### Backend Performance (M3 Max)

| Backend  | Allocation | Deallocation | Reallocation | Move  |
| -------- | ---------- | ------------ | ------------ | ----- |
| Core     | 21 ns      | 21 ns        | 21 ns        | 21 ns |
| Async    | 21 ns      | 21 ns        | 21 ns        | 21 ns |
| Lockfree | 40 ns      | 40 ns        | 40 ns        | 40 ns |
| Unified  | 40 ns      | 40 ns        | 40 ns        | 40 ns |

### Tracking Overhead

| Operation          | Latency | Throughput   |
| ------------------ | ------- | ------------ |
| Single Track (64B) | 528 ns  | 115.55 MiB/s |
| Single Track (1KB) | 544 ns  | 1.75 GiB/s   |
| Single Track (1MB) | 4.72 µs | 206.74 GiB/s |
| Batch Track (1000) | 541 µs  | 1.85 Melem/s |

### Analysis Performance

| Analysis Type   | Scale         | Latency |
| --------------- | ------------- | ------- |
| Stats Query     | Any           | 250 ns  |
| Small Analysis  | 1,000 allocs  | 536 µs  |
| Medium Analysis | 10,000 allocs | 5.85 ms |
| Large Analysis  | 50,000 allocs | 35.7 ms |

### Concurrency Performance

| Threads | Latency | Efficiency |
| ------- | ------- | ---------- |
| 1       | 19.3 µs | 100%       |
| 4       | 55.7 µs | 139%       |
| 8       | 138 µs  | 112%       |
| 16      | 475 µs  | 65%        |
| 32      | 1.04 ms | 59%        |

**Optimal Concurrency**: 4-8 threads

## Design Principles

### 1. Separation of Concerns

- **Capture Layer**: Responsible for capturing memory events
- **Analysis Layer**: Responsible for analyzing captured data
- **Output Layer**: Responsible for presenting results

### 2. Extensibility

- Pluggable backends
- Modular analysis components
- Easy to add new detectors and classifiers

### 3. Performance First

- Nanosecond-level tracking overhead
- Minimal impact on application performance
- Optimized for production use

### 4. Type Safety

- Strong typing throughout
- Compile-time guarantees
- Minimal runtime checks

### 5. Thread Safety

- Lock-free data structures where possible
- Efficient synchronization primitives
- Safe concurrent access

## Detailed Module Architecture

### Analysis Module Structure

The `analysis/` module contains 14 submodules, each responsible for specific analysis tasks:

```mermaid
graph TB
    A[Analysis Module] --> B[Detectors]
    A --> C[Classification]
    A --> D[Lifecycle]
    A --> E[Metrics]
    A --> F[Quality]
    A --> G[Relation Inference]
    A --> H[Safety]
    A --> I[Security]
    
    B --> B1[Leak Detector]
    B --> B2[UAF Detector]
    B --> B3[Overflow Detector]
    B --> B4[Lifecycle Detector]
    B --> B5[Safety Detector]
    
    C --> C1[Type Classifier]
    C --> C2[Pattern Matcher]
    C --> C3[Rule Engine]
    
    D --> D1[Lifecycle Summary]
    D --> D2[Ownership History]
    
    E --> E1[Metrics Collector]
    E --> E2[Metrics Reporter]
    
    F --> F1[Quality Analyzer]
    F --> F2[Quality Checker]
    F --> F3[Quality Validator]
    
    G --> G1[Clone Detector]
    G --> G2[Container Detector]
    G --> G3[Shared Detector]
    G --> G4[Slice Detector]
```

#### Detectors Submodule

**Purpose**: Detect various memory issues

**Components**:
- **Leak Detector**: Identifies unreleased allocations
- **UAF Detector**: Detects use-after-free patterns
- **Overflow Detector**: Finds buffer overflows
- **Lifecycle Detector**: Tracks object lifecycles
- **Safety Detector**: Identifies unsafe code patterns

**Performance**:
- Detection time: O(n) where n is allocation count
- Memory overhead: Minimal (uses existing data)

#### Classification Submodule

**Purpose**: Classify types and patterns

**Components**:
- **Type Classifier**: Categorizes types (primitive, collection, smart pointer, etc.)
- **Pattern Matcher**: Identifies common patterns
- **Rule Engine**: Applies classification rules

**Performance**:
- Classification: 40-56 ns per type
- Caching: Subsequent lookups < 10 ns

#### Lifecycle Submodule

**Purpose**: Track object lifecycles

**Components**:
- **Lifecycle Summary**: Aggregates lifecycle information
- **Ownership History**: Tracks ownership changes

**Features**:
- Birth/death tracking
- Ownership transfer detection
- Lifecycle phase analysis

#### Metrics Submodule

**Purpose**: Collect and report metrics

**Components**:
- **Metrics Collector**: Gathers metrics data
- **Metrics Reporter**: Generates reports

**Metrics Tracked**:
- Allocation count and size
- Deallocation patterns
- Memory fragmentation
- Type distribution

#### Quality Submodule

**Purpose**: Analyze code quality

**Components**:
- **Quality Analyzer**: Analyzes quality metrics
- **Quality Checker**: Checks quality rules
- **Quality Validator**: Validates quality standards

**Quality Metrics**:
- Memory efficiency
- Allocation patterns
- Resource utilization

#### Relation Inference Submodule

**Purpose**: Infer relationships between variables

**Components**:
- **Clone Detector**: Detects clone operations
- **Container Detector**: Identifies container relationships
- **Shared Detector**: Finds shared ownership
- **Slice Detector**: Detects slice relationships

**Use Cases**:
- Understanding data flow
- Detecting ownership patterns
- Finding potential issues

#### Safety Submodule

**Purpose**: Analyze safety aspects

**Components**:
- **Safety Analyzer**: Analyzes safety issues
- **Safety Engine**: Safety rule engine

**Safety Checks**:
- Unsafe code usage
- FFI boundary safety
- Thread safety

### Capture Module Structure

The `capture/` module is organized into three submodules:

```mermaid
graph TB
    A[Capture Module] --> B[Backends]
    A --> C[Platform]
    A --> D[Types]
    
    B --> B1[Core Backend]
    B --> B2[Async Backend]
    B --> B3[Lockfree Backend]
    B --> B4[Unified Backend]
    
    B --> B5[Global Tracking]
    B --> B6[Task Profile]
    B --> B7[Hotspot Analysis]
    
    C --> C1[Allocator]
    C --> C2[Memory Info]
    C --> C3[Stack Walker]
    C --> C4[Symbol Resolver]
    
    D --> D1[Allocation Types]
    D --> D2[Lifecycle Types]
    D --> D3[Ownership Types]
    D --> D4[Smart Pointer Types]
```

#### Backends Submodule

**Purpose**: Provide pluggable capture backends

**Backend Types**:

1. **Core Backend**
   - Synchronous operations
   - Lowest latency (~21ns)
   - Single-threaded optimal

2. **Async Backend**
   - Async runtime support
   - Task ID tracking
   - Similar latency to Core (~21ns)

3. **Lockfree Backend**
   - Lock-free data structures
   - High concurrency support
   - Slightly higher latency (~40ns)

4. **Unified Backend**
   - Unified interface
   - Combines multiple strategies
   - Flexible configuration (~40ns)

**Additional Components**:
- **Global Tracking**: Process-wide tracking
- **Task Profile**: Async task profiling
- **Hotspot Analysis**: Identify allocation hotspots
- **Bottleneck Analysis**: Find performance bottlenecks

#### Platform Submodule

**Purpose**: Platform-specific implementations

**Components**:
- **Allocator**: Custom allocator integration
- **Memory Info**: System memory information
- **Stack Walker**: Stack trace capture
- **Symbol Resolver**: Symbol demangling

**Platform Support**:
- Linux: Full support
- macOS: Full support
- Windows: Full support

#### Types Submodule

**Purpose**: Define capture data structures

**Type Categories**:
- **Allocation Types**: Allocation info, size, address
- **Lifecycle Types**: Birth, death, lifecycle events
- **Ownership Types**: Ownership tracking
- **Smart Pointer Types**: Rc, Arc, Box, Weak tracking

### Unified Analyzer Architecture

The `analyzer/` module provides a unified entry point:

```mermaid
graph TB
    A[Analyzer] --> B[MemoryView]
    
    A --> C[Graph Analysis]
    A --> D[Detection Analysis]
    A --> E[Metrics Analysis]
    A --> F[Timeline Analysis]
    A --> G[Classification Analysis]
    A --> H[Safety Analysis]
    A --> I[Export Engine]
    
    B --> C
    B --> D
    B --> E
    B --> F
    B --> G
    B --> H
    
    I --> J[JSON Export]
    I --> K[HTML Export]
    I --> L[Text Export]
```

**Key Features**:
- **Lazy Initialization**: Modules created on demand
- **Shared View**: All modules share MemoryView
- **Unified API**: Consistent interface across modules

## Comparison with Master Branch

### Code Statistics

| Metric        | Master   | Improve       | Change |
| ------------- | -------- | ------------- | ------ |
| Total Lines   | \~15,000 | \~45,000      | +200%  |
| Modules       | 8        | 15            | +87%   |
| Test Coverage | \~60%    | \~85%         | +42%   |
| Documentation | Basic    | Comprehensive | +300%  |

### Feature Comparison

| Feature             | Master | Improve   |
| ------------------- | ------ | --------- |
| Backend Types       | 1      | 4         |
| Detectors           | 2      | 5         |
| Analysis Modules    | 3      | 10+       |
| Export Formats      | 2      | 5+        |
| Concurrency Support | Basic  | Advanced  |
| Performance         | Good   | Excellent |

# References

- [Performance Analysis Report]PERFORMANCE_ANALYSIS_EN.md
- [Benchmark Guide]BENCHMARK_GUIDE_EN.md
- [API Documentation]https://docs.rs/memscope-rs

***

**Last Updated**: 2026-04-12\
**Test Environment**: Apple M3 Max, macOS Sonoma