Skip to main content

SharedChunks

Struct SharedChunks 

Source
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§

Source§

impl SharedChunks

Source

pub fn new() -> Self

An empty store.

Source

pub fn sections(&self) -> usize

Number of distinct sections currently held by at least one world.

Source

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.

Source

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.

Source

pub fn same_store(&self, other: &SharedChunks) -> bool

true when both handles are the same store.

Trait Implementations§

Source§

impl Clone for SharedChunks

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SharedChunks

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for SharedChunks

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Forget for SharedChunks

§

impl Freeze for SharedChunks
where *const ArcInner<Inner> is !null: Freeze, PhantomData<ArcInner<Inner>>: Freeze,

§

impl Move for SharedChunks

§

impl RefUnwindSafe for SharedChunks

§

impl Send for SharedChunks
where Arc<Inner>: Send,

§

impl Sync for SharedChunks
where Arc<Inner>: Sync,

§

impl Unpin for SharedChunks
where Arc<Inner>: Unpin,

§

impl UnsafeUnpin for SharedChunks
where *const ArcInner<Inner> is !null: UnsafeUnpin, PhantomData<ArcInner<Inner>>: UnsafeUnpin,

§

impl UnwindSafe for SharedChunks
where Arc<Inner>: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.