Advancement triggers test only the criteria that can pass; the log writes on its own thread

Two main-thread costs from the hub tick (found on the companion branch with
HORIZON_SPIKES, commit 060c795e; its companion and metering parts stay there):

- Every inventory change tested all ~1,900 inventory_changed criteria, and
  every recipe unlocked (an iron ingot unlocks dozens) all ~1,300
  recipe_unlocked ones: 1.5-5 ms on such a tick. The tree now indexes
  inventory_changed criteria by the items that can pass them (tags resolved;
  the few without an item list are always tried) and recipe_unlocked
  criteria by recipe, so a change tests the 20-40 that could match. Same
  answers: a criterion with an item-limited predicate can only pass with one
  of those items held.
- Each log line was an unbuffered stderr write plus a latest.log write under
  a mutex on the calling thread; a full pipe or a slow disk held the tick.
  The logger now formats the line and keeps it for crash reports on the
  calling thread and hands the I/O to a "Log writer" thread; flush() waits
  for it (crash report, exit).

(cherry picked from commit 060c795e, companion parts left out)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-20 23:56:08 +02:00
parent b31cb42242
commit 815625e547
3 changed files with 362 additions and 46 deletions
+191
View File
@@ -53,6 +53,10 @@ pub struct Tree {
pub by_id: HashMap<String, usize>,
/// trigger id -> (advancement index, criterion index).
pub by_trigger: HashMap<String, Vec<(usize, usize)>>,
/// `recipe_unlocked` criteria by the recipe they name (`index_recipes`);
/// those naming none in `recipes_any`.
by_recipe: HashMap<String, Vec<(usize, usize)>>,
recipes_any: Vec<(usize, usize)>,
}
fn frame(v: &Value) -> Frame {
match v.as_str() {
@@ -93,12 +97,111 @@ impl Tree {
tree.list.push(a);
}
}
tree.index_recipes();
tree
}
/// Index the `recipe_unlocked` criteria by recipe: a grant that unlocks
/// twenty recipes tested every one of the ~1,300 recipe advancements'
/// criteria for each (several ms in one tick).
pub fn index_recipes(&mut self) {
self.by_recipe.clear();
self.recipes_any.clear();
for &(ai, ci) in self
.by_trigger
.get("minecraft:recipe_unlocked")
.into_iter()
.flatten()
{
match self.list[ai].criteria[ci].conditions["recipes"].as_str() {
Some(r) => self
.by_recipe
.entry(r.to_owned())
.or_default()
.push((ai, ci)),
None => self.recipes_any.push((ai, ci)),
}
}
}
/// The `recipe_unlocked` criteria that could pass for `recipe`, in tree
/// order (the others name another recipe).
pub fn recipe_criteria(&self, recipe: &str) -> Vec<(usize, usize)> {
let mut out = self.recipes_any.clone();
out.extend(self.by_recipe.get(recipe).into_iter().flatten());
out.sort_unstable();
out
}
pub fn get(&self, id: &str) -> Option<&Advancement> {
self.by_id.get(id).map(|&i| &self.list[i])
}
}
/// The `inventory_changed` criteria by the items they need. A criterion whose
/// item predicates include one limited to some items (an id, a `#tag`, a
/// list) can only pass with one of those in the inventory, so an inventory
/// change tests only the criteria of the items it holds, plus those that
/// need no particular item. The same answers as testing them all: the ~1,300
/// recipe advancements each have one, and testing every one of them cost a
/// player 1–4 ms on every tick its inventory changed.
#[derive(Default)]
pub struct InventoryIndex {
by_item: HashMap<String, Vec<(usize, usize)>>,
any: Vec<(usize, usize)>,
}
impl InventoryIndex {
/// `tag` gives a `#tag`'s item ids (none for a tag that doesn't exist,
/// which matches nothing).
pub fn build<'t>(tree: &Tree, tag: impl Fn(&str) -> Vec<&'t str>) -> Self {
fn ids<'t>(v: &Value, tag: &impl Fn(&str) -> Vec<&'t str>, out: &mut Vec<String>) {
match v {
Value::String(s) => match s.strip_prefix('#') {
Some(t) => out.extend(tag(t).into_iter().map(str::to_owned)),
None => out.push(s.clone()),
},
Value::Array(a) => a.iter().for_each(|x| ids(x, tag, out)),
_ => {}
}
}
let mut index = Self::default();
let Some(list) = tree.by_trigger.get("minecraft:inventory_changed") else {
return index;
};
for &(ai, ci) in list {
let items = &tree.list[ai].criteria[ci].conditions["items"];
// `id_matches`: only a string or a list limits the items.
let limited = items.as_array().and_then(|a| {
a.iter()
.map(|p| &p["items"])
.find(|v| v.is_string() || v.is_array())
});
match limited {
Some(v) => {
let mut out = Vec::new();
ids(v, &tag, &mut out);
out.sort();
out.dedup();
for id in out {
index.by_item.entry(id).or_default().push((ai, ci));
}
}
None => index.any.push((ai, ci)),
}
}
index
}
/// The criteria an inventory holding these item ids could pass, in
/// tree order.
pub fn candidates<'a>(&self, held: impl IntoIterator<Item = &'a str>) -> Vec<(usize, usize)> {
let mut out = self.any.clone();
for id in held {
if let Some(list) = self.by_item.get(id) {
out.extend_from_slice(list);
}
}
out.sort_unstable();
out.dedup();
out
}
}
fn collect(root: &Path, dir: &Path, ns: &str, out: &mut Vec<(String, std::path::PathBuf)>) {
let Ok(entries) = std::fs::read_dir(dir) else {
return;
@@ -1090,6 +1193,94 @@ mod tests {
stack.id = "minecraft:tnt".into();
}
}
/// Only the criteria the held items could pass are tested; those that
/// need no particular item always are.
#[test]
fn inventory_index_keeps_the_criteria_the_items_could_pass() {
let criteria = [
serde_json::json!({"items": [{"items": "minecraft:diamond"}]}),
serde_json::json!({"items": [{"items": "#minecraft:planks"}]}),
serde_json::json!({"items": [{"count": {"min": 2}}]}),
serde_json::json!({"items": [{"count": 1}, {"items": ["minecraft:stick", "minecraft:apple"]}]}),
serde_json::json!({"slots": {"empty": 0}}),
serde_json::json!({"items": [{"items": "#minecraft:no_such_tag"}]}),
];
let mut tree = Tree::default();
for (i, conditions) in criteria.into_iter().enumerate() {
tree.list.push(Advancement {
id: format!("test:{i}"),
parent: None,
display: None,
criteria: vec![Criterion {
name: "c".into(),
trigger: "minecraft:inventory_changed".into(),
conditions,
}],
requirements: vec![vec!["c".into()]],
rewards: Rewards::default(),
});
tree.by_trigger
.entry("minecraft:inventory_changed".into())
.or_default()
.push((i, 0));
}
let index = InventoryIndex::build(&tree, |tag| match tag {
"minecraft:planks" => vec!["minecraft:oak_planks", "minecraft:birch_planks"],
_ => Vec::new(),
});
let found = |held: &[&str]| -> Vec<usize> {
index
.candidates(held.iter().copied())
.into_iter()
.map(|(ai, _)| ai)
.collect()
};
assert_eq!(found(&[]), [2, 4]);
assert_eq!(found(&["minecraft:dirt"]), [2, 4]);
assert_eq!(
found(&["minecraft:oak_planks", "minecraft:diamond"]),
[0, 1, 2, 4]
);
assert_eq!(found(&["minecraft:apple", "minecraft:apple"]), [2, 3, 4]);
}
/// A recipe unlocked tests only the criteria that name it.
#[test]
fn recipe_index_keeps_the_criteria_of_the_recipe() {
let mut tree = Tree::default();
for (i, conditions) in [
serde_json::json!({"recipes": "minecraft:oak_planks"}),
serde_json::json!({"recipes": "minecraft:stick"}),
serde_json::json!({}),
serde_json::json!({"recipes": "minecraft:oak_planks"}),
]
.into_iter()
.enumerate()
{
tree.list.push(Advancement {
id: format!("test:{i}"),
parent: None,
display: None,
criteria: vec![Criterion {
name: "c".into(),
trigger: "minecraft:recipe_unlocked".into(),
conditions,
}],
requirements: vec![vec!["c".into()]],
rewards: Rewards::default(),
});
tree.by_trigger
.entry("minecraft:recipe_unlocked".into())
.or_default()
.push((i, 0));
}
tree.index_recipes();
assert_eq!(
tree.recipe_criteria("minecraft:oak_planks"),
[(0, 0), (2, 0), (3, 0)]
);
assert_eq!(tree.recipe_criteria("minecraft:stick"), [(1, 0), (2, 0)]);
assert_eq!(tree.recipe_criteria("minecraft:torch"), [(2, 0)]);
}
#[test]
fn bucket_axolotl_block_advancements_require_the_placed_block() {
let ctx = Entities {
+52 -7
View File
@@ -91,6 +91,8 @@ pub(super) struct AdvState {
/// The tree being read on another thread since the world opened
/// (`prefetch_advancement_tree`), taken by the first `advancement_tree`.
pub(super) pending_tree: Option<std::thread::JoinHandle<Tree>>,
/// `inventory_changed`'s criteria by item, built for `tree`.
inventory_index: Option<(Arc<Tree>, Arc<adv::InventoryIndex>)>,
}
/// The game's world for predicates.
struct View<'a, 'g>(&'a Game<'g>);
@@ -229,9 +231,31 @@ impl Game<'_> {
_ => Tree::load(&self.options.resources.join("data")),
};
self.adv.tree = Arc::new(tree);
// With the tree, while the world loads: built on the first
// inventory change it cost that tick ~10 ms.
self.inventory_index();
}
self.adv.tree.clone()
}
/// `inventory_changed`'s criteria by item, for the tree in place.
fn inventory_index(&mut self) -> Arc<adv::InventoryIndex> {
let tree = self.adv.tree.clone();
if let Some((built, index)) = &self.adv.inventory_index
&& Arc::ptr_eq(built, &tree)
{
return index.clone();
}
let items = self.res.items();
let index = Arc::new(adv::InventoryIndex::build(&tree, |tag| {
items
.item_tags
.get(tag)
.map(|t| t.iter().map(String::as_str).collect())
.unwrap_or_default()
}));
self.adv.inventory_index = Some((tree, index.clone()));
index
}
/// Start reading the advancement files on another thread while the world
/// loads. The first tick's first criterion trigger used to read and parse
/// all of them on the game thread (about 60 ms).
@@ -394,6 +418,16 @@ impl Game<'_> {
let Some(list) = tree.by_trigger.get(name) else {
return;
};
self.trigger_criteria(&tree, list, test);
}
/// `trigger` for some of a trigger's criteria: (advancement, criterion)
/// indexes in `tree`, in tree order.
fn trigger_criteria(
&mut self,
tree: &Tree,
list: &[(usize, usize)],
test: impl Fn(&Value, &dyn adv::World) -> bool,
) {
let mut hits = Vec::new();
{
let view = View(self);
@@ -437,9 +471,14 @@ impl Game<'_> {
} else {
self.recipe_toast(unlocked);
}
if unlocked.is_empty() || self.remote.is_some() || self.no_player_here() {
return;
}
self.load_advancements();
let tree = self.adv.tree.clone();
for r in unlocked {
let r = r.clone();
self.trigger("minecraft:recipe_unlocked", move |c, _| {
let list = tree.recipe_criteria(r);
self.trigger_criteria(&tree, &list, |c, _| {
c["recipes"].as_str() == Some(r.as_str())
});
}
@@ -560,7 +599,15 @@ impl Game<'_> {
.is_some_and(|i| s.count >= i.max_stack)
})
.count() as f64;
self.trigger("minecraft:inventory_changed", move |c, w| {
// Only the criteria the items held could pass (`InventoryIndex`).
if self.remote.is_some() || self.no_player_here() {
return;
}
self.load_advancements();
let tree = self.adv.tree.clone();
let index = self.inventory_index();
let list = index.candidates(stacks.iter().map(|s| s.id.as_str()));
self.trigger_criteria(&tree, &list, move |c, w| {
let slots = &c["slots"];
let range = |k: &str, v: f64| {
let r = &slots[k];
@@ -965,10 +1012,8 @@ impl Game<'_> {
});
positions.push((a.x, a.y));
}
let mut tree = Tree {
list,
..Tree::default()
};
let mut tree = Tree::default();
tree.list = list;
for (i, a) in tree.list.iter().enumerate() {
tree.by_id.insert(a.id.clone(), i);
}
+119 -39
View File
@@ -12,7 +12,7 @@ use std::collections::VecDeque;
use std::fs::{self, File, OpenOptions};
use std::io::{self, Read, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};
use std::sync::Mutex;
use std::sync::{Mutex, mpsc};
use chrono::{DateTime, Local, NaiveDate};
@@ -201,16 +201,74 @@ pub fn open_latest(dir: &Path) -> io::Result<Option<Latest>> {
Ok(Some(Latest { file, _lock: lock }))
}
struct State {
/// What the writer thread is told.
enum Out {
/// Lines for the file, dated, and the message for stderr (Horizon's own).
Write {
date: NaiveDate,
lines: Vec<String>,
stderr: Option<String>,
},
/// Answer once everything before is written.
Flush(mpsc::SyncSender<()>),
}
/// The file side of the log, on its own thread: a line costs the thread that logs it a
/// format and a channel send, never a write that can wait (a full stderr pipe, a slow disk):
/// written on the game thread, a line could hold the tick for milliseconds while the CPU sat
/// idle.
struct Writer {
file: Option<Latest>,
dir: PathBuf,
/// The day the open file's lines belong to.
date: NaiveDate,
recent: VecDeque<String>,
}
impl Writer {
fn run(mut self, out: mpsc::Receiver<Out>) {
let stderr = io::stderr();
for msg in out {
match msg {
Out::Write {
date,
lines,
stderr: message,
} => {
if let Some(message) = message {
let _ = writeln!(stderr.lock(), "{message}");
}
if date != self.date {
if let Some(latest) = &mut self.file
&& let Err(e) = roll(&mut latest.file, &self.dir, self.date)
{
let _ = writeln!(
stderr.lock(),
"Horizon: could not roll over the log: {e}"
);
}
self.date = date;
}
if let Some(latest) = &mut self.file {
let mut text = lines.join("\n");
text.push('\n');
let _ = latest.file.write_all(text.as_bytes());
}
}
Out::Flush(done) => {
if let Some(latest) = &mut self.file {
let _ = latest.file.flush();
}
let _ = done.send(());
}
}
}
}
}
struct Logger {
state: Mutex<State>,
out: mpsc::Sender<Out>,
/// The last lines, kept here (not on the writer) so a crash report has them at once.
recent: Mutex<VecDeque<String>>,
/// What the main thread is called in the log: "Render thread", or "Server thread" for the
/// dedicated server, as in vanilla.
main_thread: &'static str,
@@ -233,53 +291,66 @@ impl log::Log for Logger {
return;
}
let message = record.args().to_string();
if ours(record.target()) {
eprintln!("{message}");
}
let current = std::thread::current();
let thread = match current.name() {
Some("main") => self.main_thread,
Some(name) => name,
None => "unnamed",
};
self.write(thread, record.level(), &message);
let stderr = ours(record.target()).then(|| message.clone());
self.send(thread, record.level(), &message, stderr);
}
/// Waits (a second at most) until every line logged before is in the file.
fn flush(&self) {
if let Some(latest) = &mut self.state.lock().unwrap_or_else(|e| e.into_inner()).file {
let _ = latest.file.flush();
let (done, wait) = mpsc::sync_channel(1);
if self.out.send(Out::Flush(done)).is_ok() {
let _ = wait.recv_timeout(std::time::Duration::from_secs(1));
}
}
}
impl Logger {
fn write(&self, thread: &str, level: log::Level, message: &str) {
self.send(thread, level, message, None);
}
fn send(&self, thread: &str, level: log::Level, message: &str, stderr: Option<String>) {
let now = Local::now();
let mut state = self.state.lock().unwrap_or_else(|e| e.into_inner());
let state = &mut *state;
if now.date_naive() != state.date {
if let Some(latest) = &mut state.file
&& let Err(e) = roll(&mut latest.file, &state.dir, state.date)
{
eprintln!("Horizon: could not roll over the log: {e}");
}
state.date = now.date_naive();
}
// As log4j writes a multi-line message: the pattern once, the rest as it is.
for (i, line) in message.lines().enumerate() {
let line = if i == 0 {
format_line(&now, thread, level, line)
} else {
line.to_owned()
};
if let Some(latest) = &mut state.file {
let _ = writeln!(latest.file, "{line}");
}
state.recent.push_back(line);
if state.recent.len() > RECENT {
state.recent.pop_front();
let lines: Vec<String> = message
.lines()
.enumerate()
.map(|(i, line)| {
if i == 0 {
format_line(&now, thread, level, line)
} else {
line.to_owned()
}
})
.collect();
{
let mut recent = self.recent.lock().unwrap_or_else(|e| e.into_inner());
for line in &lines {
if recent.len() == RECENT {
recent.pop_front();
}
recent.push_back(line.clone());
}
}
let msg = Out::Write {
date: now.date_naive(),
lines,
stderr,
};
if let Err(mpsc::SendError(Out::Write {
stderr: Some(message),
..
})) = self.out.send(msg)
{
// No writer (it could not start): the terminal still gets Horizon's messages.
eprintln!("{message}");
}
}
}
@@ -304,14 +375,24 @@ pub fn init(home: &Path, main_thread: &'static str) {
None
}
};
let logger = LOGGER.get_or_init(|| Logger {
state: Mutex::new(State {
let logger = LOGGER.get_or_init(|| {
let (out, receive) = mpsc::channel();
let writer = Writer {
file,
dir,
date: Local::now().date_naive(),
recent: VecDeque::new(),
}),
main_thread,
};
if let Err(e) = std::thread::Builder::new()
.name("Log writer".into())
.spawn(move || writer.run(receive))
{
eprintln!("Horizon: could not start the log writer: {e}");
}
Logger {
out,
recent: Mutex::new(VecDeque::new()),
main_thread,
}
});
if log::set_logger(logger).is_ok() {
log::set_max_level(log::LevelFilter::Info);
@@ -341,10 +422,9 @@ pub fn write_as(thread: &str, level: log::Level, message: &str) {
/// The last lines written, oldest first (for crash reports).
pub fn recent_lines() -> Vec<String> {
LOGGER.get().map_or_else(Vec::new, |l| {
l.state
l.recent
.lock()
.unwrap_or_else(|e| e.into_inner())
.recent
.iter()
.cloned()
.collect()