JFLib

game/src/sparse.rs#

Source: lib/game/src/sparse.rs

sparse -- a component column: the values of one type in dense arrays, found by entity through a
paged sparse index (a sparse set)

  SparseSet<T>      insert / remove / get / get_mut / contains in O(1); iter, iter_mut in the order of
                    the dense arrays; the version of every entry's last change (changed_since)
  Mut<'a, T>        what get_mut and iter_mut hand out: reading leaves the entry as it was, writing
                    through it (DerefMut) stamps the entry with a new version

The order of iteration is the order of the dense arrays: an insert appends, a remove moves the last
entry into the hole (swap remove).  It follows from the sequence of operations alone, never from
addresses or hashes, so two columns with the same history iterate alike on every target; sort()
puts the entries in ascending entity order (a column rebuilt another way, from a snapshot, iterates
as the original after both are sorted).  The sparse index is paged (1024 entity indices a page,
allocated when an entity of the page gets a value): a column of a few entities among a million
costs a few pages, not four bytes per entity.

Versions: every column counts its changes (version()); an insert, a get_mut written through, a
touch, and the entries iter_mut writes through take a new version.  A consumer (the bridge to
render3d, a network snapshot) remembers the version it saw last and asks for what changed since
(changed_since); removals() counts the entries that left, so a consumer knows when to look for
gone ones.
  • struct SparseSet<T> the values of one component type, by entity (see the module header)

  • struct Mut<'a, T> a value of a column handed out for writing: reads leave it unchanged, a write through it (DerefMut) stamps its entry with the version the access took

impl<'a, T> Mut<'a, T>

  • fn untracked(self) -> &'a mut T the value for writing without stamping it (a change no consumer needs to see, such as a cache inside the component)

impl<T> SparseSet<T>

  • const fn new() -> SparseSet<T> an empty column
  • fn len(&self) -> usize entities with a value
  • fn is_empty(&self) -> bool no entity has a value
  • fn position(&self, e: Entity) -> Option<usize> the position of e's value in the dense arrays (the order of iteration), None without one. A stale id (an older generation of the index) has none
  • fn contains(&self, e: Entity) -> bool e has a value
  • fn get(&self, e: Entity) -> Option<&T> e's value
  • fn get_mut(&mut self, e: Entity) -> Option<Mut<'_, T>> e's value for writing; a write through it takes a new version
  • fn insert(&mut self, e: Entity, value: T) -> Option<T> e gets the value (a new version); the value it had, if any. The column does not know which entities live (World::insert refuses dead ones): another generation of the same index loses its value to e, and that value comes back
  • fn remove(&mut self, e: Entity) -> Option<T> e's value goes (the last entry takes its place in the order)
  • fn touch(&mut self, e: Entity) -> bool stamp e's value with a new version without writing it (true: e has one)
  • fn clear(&mut self) every value goes
  • fn entities(&self) -> &[Entity] the entities with a value, in the order of iteration
  • fn values(&self) -> &[T] the values, in the order of iteration
  • fn iter(&self) -> impl Iterator<Item = (Entity, &T)> + '_ (entity, value) in the order of iteration
  • fn iter_mut(&mut self) -> impl Iterator<Item = (Entity, Mut<'_, T>)> + '_ (entity, value for writing) in the order of iteration; the entries written through take one new version
  • fn version(&self) -> u64 the column's version: the newest any of its entries can have
  • fn changed_at(&self, e: Entity) -> Option<u64> the version of e's last change (None: no value)
  • fn versions(&self) -> &[u64] the versions of the entries' last changes, in the order of iteration
  • fn changed_since(&self, v: u64) -> impl Iterator<Item = (Entity, &T)> + '_ (entity, value) of the entries changed after version v (inserted, written, touched), in the order of iteration. The whole column is looked at: a scan of one u64 per entry
  • fn changed_mut_since(&mut self, v: u64) -> impl Iterator<Item = (Entity, Mut<'_, T>)> + '_ (entity, value for writing) of the entries changed after version v, in the order of iteration; the ones written through take one new version (newer than every entry the walk skips)
  • fn removals(&self) -> u64 how many values have left the column so far (remove, clear, despawned entities): a consumer that sees the same number as last time has nothing gone to look for
  • fn sort(&mut self) the entries in ascending order of their entity index: the canonical order, the same for every history that ends with the same entities (versions are kept)
  • fn index_bytes(&self) -> usize the bytes of the sparse index's pages (a measure for tests)

Source: docs/API.md in the JFLib repository.