An open API service indexing awesome lists of open source software.

https://github.com/yeautyye/auto-allocator

πŸš€ Zero-config automatic memory allocator for Rust - just add one line and get up to 1.6x faster allocation performance across all platforms
https://github.com/yeautyye/auto-allocator

allocator automatic cross-platform embedded hardware-aware high-performance memory-management memory-optimization mimalloc no-std performance rust systems-programming wasm zero-config

Last synced: 3 months ago
JSON representation

πŸš€ Zero-config automatic memory allocator for Rust - just add one line and get up to 1.6x faster allocation performance across all platforms

Awesome Lists containing this project

README

          

# πŸš€ Auto-Allocator

[![Crates.io](https://img.shields.io/crates/v/auto-allocator.svg)](https://crates.io/crates/auto-allocator)
[![Documentation](https://img.shields.io/badge/docs.rs-auto--allocator-blue)](https://docs.rs/auto-allocator)
[![License: MIT/Apache-2.0/MPL-2.0](https://img.shields.io/badge/License-MIT%2FApache--2.0%2FMPL--2.0-brightgreen.svg)](https://github.com/YeautyYE/auto-allocator/blob/main/LICENSE-APACHE)
[![Rust Version](https://img.shields.io/badge/Rust-%3E=1.80.0-orange)](https://www.rust-lang.org/)

> **🎯 One line of code. Platform-intelligent optimization. Zero configuration.**

The smartest memory allocator for Rust that automatically selects the optimal allocator for your platform - delivering performance improvements where possible, and platform compliance where required.

## 🌟 Why Developers Choose Auto-Allocator

**🎯 Smart Optimization for Every Platform**
- **Performance where it helps**: 1.6x faster on multi-core Windows/macOS/Linux ([Microsoft Research](https://www.microsoft.com/en-us/research/uploads/prod/2019/06/mimalloc-tr-v1.pdf))
- **Compliance where it matters**: Respects Android/iOS official policies
- **Efficiency everywhere**: Optimal allocation from servers to microcontrollers

**⚑ Effortless Integration**
- **Truly zero-config** - just `use auto_allocator;` and you're optimized
- **Universal compatibility** - works on every Rust platform
- **Production ready** - handles platform differences automatically

**🧠 Platform Intelligence**
- **Respects each platform's strengths** - leverages native optimizations when better
- **Hardware-aware** - adapts to CPU cores and memory constraints
- **Research-backed** - every choice has technical justification

## ⚑ Quick Start

### 1. Add Dependency
```toml
[dependencies]
auto-allocator = "*"
```

### 2. Import and Use
```rust
use auto_allocator; // πŸŽ‰ Done! Memory allocation is now optimized

fn main() {
// Your existing code automatically benefits from optimal allocation
let data = vec![1, 2, 3, 4, 5];
let text = "Hello".repeat(1000);

// No changes needed - just faster memory operations!
println!("πŸš€ High-performance allocation active!");
}
```

### 3. Verify Optimization (Optional)
```rust
use auto_allocator;

fn main() {
let info = auto_allocator::get_allocator_info();
println!("βœ… Using: {:?}", info.allocator_type);
println!("πŸ’‘ {}", info.reason);
}
```

**✨ That's literally all you need!** Auto-Allocator handles everything else automatically.

## πŸ”¬ How It Works

Auto-Allocator uses **intelligent two-phase optimization**:

```
πŸ“‹ COMPILE TIME πŸš€ RUNTIME βœ… RESULT
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Platform β”‚ β”‚ CPU Core Count β”‚ β”‚ β”‚
β”‚ Detection │──────────────▢│ Analysis │──────────▢│ Optimal β”‚
β”‚ β”‚ β”‚ β”‚ β”‚ Allocator β”‚
β”‚ Compiler β”‚ β”‚ Memory β”‚ β”‚ Selection β”‚
β”‚ Analysis │──────────────▢│ Detection │──────────▢│ β”‚
β”‚ β”‚ β”‚ β”‚ β”‚ β”‚
β”‚ Feature β”‚ β”‚ Hardware β”‚ β”‚ β”‚
β”‚ Availability │──────────────▢│ Optimization │──────────▢│ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

🎯 90% of decisions made at compile-time for zero runtime overhead
⚑ Only high-performance platforms need runtime CPU detection
```

### 🎯 Platform-Specific Selection

| Platform | Selected Allocator | Expected Benefit | Technical Reason |
|----------|-------------------|------------------|------------------|
| **πŸ–₯️ Windows/macOS/Linux (Multi-core)** | **mimalloc** | **1.6x faster allocation** | Microsoft Research-proven performance |
| **πŸ“± Android** | **Scudo** | Platform security compliance | Google's official security policy |
| **πŸ“± iOS** | **libmalloc** | Deep system integration | Apple's optimization recommendation |
| **πŸ”’ BSD/Solaris** | **Native allocator** | Already optimal | Platform-tuned performance |
| **πŸ€– Embedded** | **embedded-alloc** | Resource efficiency | Designed for constraints |
| **πŸ› Debug builds** | **System** | Fast compilation | Development speed priority |
| **🌐 WASM** | **System** | Browser compatibility | Web standard compliance |

### πŸš€ Performance Results

**When mimalloc is selected** (Windows/macOS/Linux multi-core):
- **1.6x faster allocation** in multi-threaded scenarios ([Microsoft Research](https://www.microsoft.com/en-us/research/uploads/prod/2019/06/mimalloc-tr-v1.pdf))
- **Reduced lock contention** through free-list sharding
- **Better cache locality** and lower memory fragmentation

**Test it yourself**:
```bash
cargo bench # Benchmark your specific workload
```

**Key insight**: Auto-Allocator delivers performance improvements where they matter, while respecting platform policies elsewhere.

## πŸ›‘οΈ Security Features

### πŸ”’ When Available (Platform-Dependent)

Security features are **only available on platforms that use mimalloc-secure**:

```toml
# Only effective on Windows/macOS/Linux with mimalloc support
[dependencies]
auto-allocator = { version = "*", features = ["secure"] }
```

### 🎯 Platform-Specific Security

| Platform | Secure Mode Effect | Security Features |
|----------|-------------------|-------------------|
| **πŸ–₯️ Windows/macOS/Linux** | **mimalloc-secure activated** | Guard pages, encrypted free lists, randomization |
| **πŸ“± Android** | **No change** (uses Scudo) | Android's built-in security (UAF protection) |
| **πŸ“± iOS** | **No change** (uses libmalloc) | iOS system-level protections |
| **πŸ”’ BSD/Solaris** | **No change** (native allocators) | Platform built-in security hardening |
| **🌐 WASM** | **No change** (browser sandbox) | Browser security model isolation |
| **πŸ€– Embedded** | **No change** (resource constraints) | Standard embedded safety measures |

### πŸ“Š Security Trade-offs

| Configuration | Performance | Security Level | Available On |
|---------------|-------------|----------------|--------------|
| **Default** | 100% speed | Rust safety + platform defaults | All platforms |
| **Secure** | 90% speed | Enhanced heap protection | Windows/macOS/Linux only |

**πŸ’‘ Key insight**: Many platforms already have excellent built-in security - Auto-Allocator respects and leverages these instead of overriding them.

## πŸ› οΈ Advanced Usage

### πŸ” Check What's Being Used

```rust
use auto_allocator;

fn main() {
// πŸ” Inspect current allocator selection
let info = auto_allocator::get_allocator_info();
println!("πŸš€ Active: {:?}", info.allocator_type);
println!("πŸ’‘ Why: {}", info.reason);

// πŸ“ˆ System specifications
println!("πŸ–₯️ Hardware: {} cores, {} RAM",
info.system_info.cpu_cores,
auto_allocator::format_memory_size(info.system_info.total_memory_bytes));

// βœ… Validate optimal configuration
let (is_optimal, suggestion) = auto_allocator::check_allocator_optimization();
if !is_optimal {
println!("⚠️ Optimization tip: {}", suggestion.unwrap());
}

// 🎯 Get platform-specific recommendations
let (recommended, reason) = auto_allocator::get_recommended_allocator();
println!("πŸ’― Recommended: {:?} - {}", recommended, reason);
}
```

## πŸ”¬ Technical Deep-Dive

### πŸ† Why mimalloc Dominates Performance

**🎯 Peer-Reviewed Research**:
- [**Microsoft Research Study**](https://www.microsoft.com/en-us/research/uploads/prod/2019/06/mimalloc-tr-v1.pdf): **1.6x faster** than jemalloc in production
- **Free-list sharding**: Eliminates lock contention in multi-threaded applications
- **Cache-conscious design**: Better memory locality = faster access patterns
- **Battle-tested**: Powers Microsoft Azure, Office 365, and Windows services

## πŸ’‘ Examples & Tutorials

Explore real-world usage in the [`examples/`](examples/) directory:

| Example | Use Case | What You'll Learn |
|---------|----------|-------------------|
| **[πŸš€ simple_demo](examples/simple_demo/)** | Basic integration | Zero-config setup + system introspection |
| **[βœ… optimization_check](examples/optimization_check/)** | CI/CD validation | Automated performance verification |
| **[🌐 web_server](examples/web_server/)** | Production server | High-throughput web application |
| **[πŸ€– embedded_system](examples/embedded_system/)** | IoT/Embedded | Resource-constrained optimization + Real no_std compilation |

## πŸ“„ License

**Flexible licensing** for maximum compatibility:

- **[MIT License](LICENSE-MIT)** - Permissive, commercial-friendly
- **[Apache License 2.0](LICENSE-APACHE)** - Enterprise-preferred, patent protection
- **[Mozilla Public License 2.0](LICENSE-MPL)** - Copyleft alternative

**Choose the license that best fits your project!**

## πŸŽ“ Research & References

### πŸ“š Core Research
- **[mimalloc: Free List Sharding in Action](https://www.microsoft.com/en-us/research/uploads/prod/2019/06/mimalloc-tr-v1.pdf)** - Microsoft Research
- **[A Scalable Concurrent malloc(3) Implementation](https://people.freebsd.org/~jasone/jemalloc/bsdcan2006/jemalloc.pdf)** - Jason Evans (Facebook)

### 🏒 Platform Documentation
- **[Android Scudo Hardened Allocator](https://source.android.com/docs/security/test/scudo)** - Android AOSP
- **[Apple Memory Management Guidelines](https://developer.apple.com/library/archive/documentation/Performance/Conceptual/ManagingMemory/Articles/MemoryAlloc.html)** - Apple Developer