apecs
Async-friendly and Pleasant Entity Component System
apecs is an entity-component system written in Rust that can share world resources with
futures run in any async runtime. This makes it great for general applications,
quick game prototypes, DIY engines and any simulation that has discrete steps in time.
Why
Most ECS libraries (and game main-loops in general) are polling based. This is great for certain tasks, but things get complicated when programming in the time domain. Async / await is great for programming in the time domain without explicitly spawning new threads or blocking, but it isn't supported by ECS libraries.
apecs was designed to to be an ECS that plays nice with async / await.
What and How
At its core apecs is a library for sharing resources across disparate polling and async loops.
It uses derivable traits and channels to orchestrate systems' access to resources and uses rayon
(where available) for concurrency.
Goals
- productivity
- flexibility
- observability
- very well rounded performance, competitive with inspirational ECS libraries
- like
specs,bevy_ecs,hecs,legion,shipyard,planck_ecs - backed by criterion benchmarks
- like
Features
Here is a quick table of features compared to other ECSs.
| Feature | apecs | bevy_ecs | hecs | legion | planck_ecs | shipyard | specs |
|---|---|---|---|---|---|---|---|
| storage | archetypal | hybrid | archetypal | archetypal | separated | sparse | separated |
| system scheduling | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | |
| early exit systems | ✔️ | ||||||
| parallel systems | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | |
| change tracking | ✔️ | ✔️ | kinda | ✔️ | ✔️ | ||
| async support | ✔️ |
Feature examples
- systems with early exit and failure
use *;
;
let mut world = default;
world.add_subgraph;
world.run.unwrap;
assert_eq!;
- async support
- futures visit world resources through
Facadeusing a closure. - resources are acquired without lifetimes
- plays well with any async runtime
- futures visit world resources through
use *;
;
let mut world = default;
let mut facade = world.facade;
let task = spawn;
while !task.is_finished
assert_eq!;
- system data derive macros
use *;
let mut world = default;
world
.visit
.unwrap;
- system scheduling
- compatible systems are placed in parallel batches (a batch is a group of systems that can run in parallel, ie they don't have conflicting borrows)
- systems may depend on other systems running before or after
- barriers
use *; let mut world = default; world.add_subgraph; assert_eq!; world.tick.unwrap; assert_eq!; world.tick.unwrap; world.tick.unwrap; assert!; - component storage
- optimized for space and iteration time as archetypes
- queries with "maybe" and "without" semantics
- queries can find a single entity without iteration or filtering
- add and modified time tracking
- parallel queries (inner parallelism)
use *; // Make a type for tracking changes ; // Entities and Components (which stores components) are default // resources let mut world = default; world.add_subgraph; assert_eq!; world.tick.unwrap; // entities are created, components applied lazily world.tick.unwrap; // f32s are modified, u32s and strings are synced world.tick.unwrap; // f32s are modified, u32s and strings are synced world .visit .unwrap; - outer parallelism (running systems in parallel)
- parallel system scheduling
- parallel execution of async futures
- parallelism is configurable (can be automatic or a requested number of threads, including 1)
use *;
;
let mut world = default;
world
.add_subgraph
.with_parallelism;
world.tick.unwrap;
- fully compatible with WASM and runs in the browser
Roadmap
- your ideas go here
Tests
I like firefox, but you can use different browsers for the wasm tests. The tests make sure apecs works on wasm.
Benchmarks
The apecs benchmarks measure itself against my favorite ECS libs:
specs, bevy, hecs, legion, shipyard and planck_ecs.
Minimum supported Rust version 1.65
apecs uses generic associated types for its component iteration traits.