This directory contains examples demonstrating various features and best practices of the PECS library.
File: 01_hello_world.rs
Run: cargo run --example 01_hello_world
The simplest possible PECS example. Demonstrates:
- Creating a World
- Spawning entities
- Using stable IDs for entity identification
- Checking entity status
- Iterating over entities
File: 02_command_buffer.rs
Run: cargo run --example 02_command_buffer
Demonstrates the command buffer system for deferred operations:
- Recording operations in a command buffer
- Batching operations for better performance
- Applying commands at a safe point
- Thread-safe operation recording (conceptual)
File: 03_persistence.rs
Run: cargo run --example 03_persistence
Shows how to save and load worlds:
- Binary format serialization
- JSON format for human-readable saves
- Stable ID preservation across save/load
- In-memory and file-based persistence
File: 07_entity_persistence.rs
Run: cargo run --example 07_entity_persistence
Demonstrates entity-level persistence for database backends:
- Saving and loading individual entities
- Entity existence checks and deletion
- Batch operations for multiple entities
- Simulated database-like operations
- Using KeyValueEntityPlugin
- Best practices for entity persistence
File: 04_performance.rs
Run: cargo run --example 04_performance --release
Demonstrates performance optimization techniques:
- Pre-allocating capacity vs dynamic growth
- Batch operations with command buffers
- Entity lifecycle performance
- Stable ID lookup performance
- Performance measurement techniques
Note: Always run with --release for accurate performance measurements!
File: 05_large_scale.rs
Run: cargo run --example 05_large_scale --release
Shows how to manage large numbers of entities efficiently:
- Creating 100,000+ entities
- Batch spawning strategies
- Efficient iteration
- Selective despawning
- Persistence at scale
- Memory management tips
To run any example:
# Debug mode (slower, more checks)
cargo run --example <example_name>
# Release mode (optimized, for performance testing)
cargo run --example <example_name> --releaseFor example:
cargo run --example 01_hello_world
cargo run --example 04_performance --release01_hello_world- Start here!02_command_buffer- Learn about deferred operations
03_persistence- Save and load entire worlds07_entity_persistence- Save and load individual entities
04_performance- Optimization techniques05_large_scale- Scaling to large entity counts
Note: The current examples work with the available API. Some features are planned for future releases:
- Component Access: Direct component insertion/removal/access methods are planned for Phase 3 Week 7-8
- Query System: Full query integration with World is planned for Phase 3 Week 7-8
- Components in Examples: Examples will be expanded with component usage once the API is complete
See docs/dev/API_GAPS.md for details on planned API improvements.
When adding new examples:
- Name files with a number prefix:
XX_descriptive_name.rs - Include comprehensive doc comments at the top
- Add a section to this README
- Test in both debug and release modes
- Keep examples focused on one concept
- Include performance tips where relevant
From the examples, key performance tips include:
- Pre-allocate capacity when you know entity count
- Use command buffers for batch operations
- Run with --release for accurate performance measurements
- Batch operations reduce allocation overhead
- Stable ID lookups are O(1) but have overhead compared to direct EntityId use
After exploring these examples:
- Read the Getting Started Guide
- Review Core Concepts
- Check out Performance Guide
- Explore Advanced Features
- Learn about Persistence
- Check the documentation in
docs/ - Review the API reference:
cargo doc --open - Look at the test suite in
tests/ - See the benchmarks in
benches/