Reuse the block entity ticker list between ticks
The list of ticking block entities was cloned into a new Vec on every tick, even when no slot had been freed and nothing had been added. Keep it behind an Arc and clone the Arc instead; promotions and sweeps use Arc::make_mut, so the list is copied only when it actually changes. Also skip the block lookup in tick_spawner: the caller only reaches it for a BlockEntity::Spawner, and changing the block removes or replaces that entity. Co-Authored-By: OpenCode <noreply@opencode.ai>
This commit is contained in:
@@ -87,7 +87,11 @@ pub fn main() {
|
||||
}
|
||||
"tick" => {
|
||||
out.push_str("tick");
|
||||
for (p, _) in entities.tickers(|p| states.get(&p).copied().unwrap_or(0)) {
|
||||
for (p, _) in entities
|
||||
.tickers(|p| states.get(&p).copied().unwrap_or(0))
|
||||
.iter()
|
||||
.copied()
|
||||
{
|
||||
out.push_str(&format!(" {},{},{}", p.0, p.1, p.2));
|
||||
}
|
||||
out.push('\n');
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
use crate::chunk::block_entity::BlockEntity;
|
||||
use crate::fast_hash::{FxHashMap, FxHashSet};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub type Pos = (i32, i32, i32);
|
||||
|
||||
@@ -46,7 +47,7 @@ pub enum Ticking {
|
||||
pub struct BlockEntities {
|
||||
map: HashMap<Pos, BlockEntity>,
|
||||
/// `Level.blockEntityTickers`: positions and the slot each was given.
|
||||
tickers: Vec<(Pos, u32)>,
|
||||
tickers: Arc<Vec<(Pos, u32)>>,
|
||||
/// `LevelChunk.tickersInLevel`: the live slot of each ticking position.
|
||||
live: FxHashMap<Pos, u32>,
|
||||
/// Positions that may need a slot, in the order vanilla would have made
|
||||
@@ -233,7 +234,7 @@ impl BlockEntities {
|
||||
}
|
||||
pub fn clear(&mut self) {
|
||||
self.map.clear();
|
||||
self.tickers.clear();
|
||||
self.tickers = Arc::default();
|
||||
self.live.clear();
|
||||
self.added.clear();
|
||||
self.pending.clear();
|
||||
@@ -274,7 +275,7 @@ impl BlockEntities {
|
||||
/// and freed ones leave it. `state` reads the block state at a position.
|
||||
/// Returns the slots to tick, in order; check each with `is_live` before
|
||||
/// ticking it, as the ticks before it can free it.
|
||||
pub fn tickers(&mut self, state: impl Fn(Pos) -> u32) -> Vec<(Pos, u32)> {
|
||||
pub fn tickers(&mut self, state: impl Fn(Pos) -> u32) -> Arc<Vec<(Pos, u32)>> {
|
||||
for (pos, token) in std::mem::take(&mut self.added) {
|
||||
if self.pending.get(&pos) != Some(&token) {
|
||||
continue;
|
||||
@@ -288,21 +289,21 @@ impl BlockEntities {
|
||||
}
|
||||
let slot = self.next();
|
||||
self.live.insert(pos, slot);
|
||||
self.tickers.push((pos, slot));
|
||||
Arc::make_mut(&mut self.tickers).push((pos, slot));
|
||||
}
|
||||
if !std::mem::take(&mut self.freed) && self.out.is_empty() {
|
||||
return self.tickers.clone();
|
||||
return Arc::clone(&self.tickers);
|
||||
}
|
||||
self.out.clear();
|
||||
let (live, map) = (&mut self.live, &self.map);
|
||||
self.tickers.retain(|(p, slot)| {
|
||||
Arc::make_mut(&mut self.tickers).retain(|(p, slot)| {
|
||||
let mine = live.get(p) == Some(slot);
|
||||
if mine && !map.contains_key(p) {
|
||||
live.remove(p);
|
||||
}
|
||||
mine && map.contains_key(p)
|
||||
});
|
||||
self.tickers.clone()
|
||||
Arc::clone(&self.tickers)
|
||||
}
|
||||
/// Whether a slot from `tickers` still ticks.
|
||||
pub fn is_live(&self, (pos, slot): (Pos, u32)) -> bool {
|
||||
@@ -329,8 +330,8 @@ mod tests {
|
||||
}
|
||||
fn order(b: &mut BlockEntities, states: &HashMap<Pos, u32>) -> Vec<Pos> {
|
||||
b.tickers(|p| states.get(&p).copied().unwrap_or(0))
|
||||
.into_iter()
|
||||
.map(|(p, _)| p)
|
||||
.iter()
|
||||
.map(|(p, _)| *p)
|
||||
.collect()
|
||||
}
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ impl Game<'_> {
|
||||
let mut spawners = Vec::new();
|
||||
let mut trial_spawners = Vec::new();
|
||||
self.item_columns = Some(Default::default());
|
||||
for slot in self.world.block_entity_tickers() {
|
||||
for slot in self.world.block_entity_tickers().iter().copied() {
|
||||
let pos = slot.0;
|
||||
// Ticks before this one can remove it (`TickingBlockEntity.isRemoved`).
|
||||
if !self.world.block_entities.is_live(slot)
|
||||
|
||||
@@ -64,14 +64,13 @@ impl Game<'_> {
|
||||
.unwrap_or_default();
|
||||
self.spawners.entity_types = Some(types);
|
||||
}
|
||||
/// The server tick of the spawner at `key`, if its block is still a
|
||||
/// spawner. Its client tick comes after every block entity's
|
||||
/// (`client_tick_spawners`).
|
||||
/// The server tick of the spawner at `key`. Its client tick comes after
|
||||
/// every block entity's (`client_tick_spawners`).
|
||||
///
|
||||
/// The caller only reaches this for a `BlockEntity::Spawner`, and changing
|
||||
/// the block removes or replaces that entity, so the block is not read
|
||||
/// again here.
|
||||
pub(super) fn tick_spawner(&mut self, key: Pos) -> Result<()> {
|
||||
let state = self.world.block(key.0, key.1, key.2);
|
||||
if &*self.res.facts().get(state).name != "minecraft:spawner" {
|
||||
return Ok(());
|
||||
}
|
||||
let Some(BlockEntity::Spawner(mut spawner)) = self.world.block_entities.take(&key) else {
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
@@ -355,7 +355,9 @@ impl World {
|
||||
false
|
||||
}
|
||||
/// The block entities to tick this tick, in order (`BlockEntities::tickers`).
|
||||
pub fn block_entity_tickers(&mut self) -> Vec<(crate::block_entities::Pos, u32)> {
|
||||
pub fn block_entity_tickers(
|
||||
&mut self,
|
||||
) -> std::sync::Arc<Vec<(crate::block_entities::Pos, u32)>> {
|
||||
let (chunks, air) = (&self.chunks, self.air);
|
||||
self.block_entities.tickers(|(x, y, z)| {
|
||||
chunks
|
||||
|
||||
Reference in New Issue
Block a user