pub struct SharedChunks { /* private fields */ }Expand description
A process-wide store that lets the worlds of many bots hold one copy of each identical chunk section (16×16×16 blocks) instead of one copy per bot.
Bots standing in the same area receive the same chunks, and at view distance 10 a world’s
section storage is almost all of a bot’s memory. Give the same handle to every
Client (its
shared_chunks option) or to every world (v26_1::World::set_shared_chunks) and the
section storage is held once per distinct section.
- Opt-in. A world without a handle stores its sections exactly as before.
- Per section, copy on write, and shared again. A shared section is immutable and
reference counted. A block change (a server block update or a local
set_block_state) never touches storage another world can read: the changing world moves to the section with the new content, which is looked up in the store first and only copied when no world holds it yet. So bots that receive the same block updates keep holding one copy, and no world ever sees another world’s packets: every world reads exactly the blocks its own packets produced. - Whole columns too. The per-column array of section handles is shared the same way between worlds whose columns hold the same sections.
- Reads do not change. A block read is the same match and the same pointer hops as an unshared section: no lock, no atomic, no extra indirection. The handle’s lock is taken while a chunk packet is applied (once per column that is already in the store), for a block change in a shared section, and when the last holder of a section drops it.
- Decoded once. A section’s key is a 128-bit keyed hash of its exact bytes in the chunk packet, found with a cheap skip pass; on a hit the section is not decoded at all.
- Freed with the last holder. The store holds weak references only: when the last world unloads a section (chunk unload, a later full chunk for the same column, a dimension change, a dropped world, a block change) its memory is freed and its entry is removed at once.
What is shared: the block states of non-uniform sections and the columns’ section arrays. Uniform sections (all air, all stone) take no heap memory and are kept inline as before. Biomes (off by default in the client, at most 128 bytes per section) and block entities stay per world; the modern worlds do not keep light. Only the modern worlds (26.1 – 26.3) use the store: the 1.8.9, 1.12.2 and 1.16.5 worlds ignore it (their sections are byte-per-block and are not shared). Worlds of different protocols can share one handle; their sections never mix.
A section that changed is found again by its content: its key is the XOR, over the 4096 blocks, of a keyed 128-bit hash of (protocol, block index, state), so a block change moves the key by two hashes and the order of the changes does not matter. A section a world decoded without a store, or changed while it had none, stays that world’s own.
The handle is cheap to clone (Arc) and Send + Sync: bots on different threads (for
example on the workers of a Swarm) can share it.
let cache = ghast_world::SharedChunks::new();
let for_second_bot = cache.clone();
assert_eq!(cache.sections(), 0);Implementations§
Sourcepub fn sections(&self) -> usize
pub fn sections(&self) -> usize
Number of distinct sections currently held by at least one world.
Sourcepub fn columns(&self) -> usize
pub fn columns(&self) -> usize
Number of distinct section arrays (one per column whose sections are all uniform or shared) currently held by at least one world.
Sourcepub fn heap_bytes(&self) -> usize
pub fn heap_bytes(&self) -> usize
Heap bytes of the shared section storage and the shared section arrays (each distinct
one counted once) plus the store’s own tables. A world’s
heap_bytes does not count what it shares.
Sourcepub fn same_store(&self, other: &SharedChunks) -> bool
pub fn same_store(&self, other: &SharedChunks) -> bool
true when both handles are the same store.