Some checks are pending
CI / windows-build (push) Waiting to run
CI / check (push) Waiting to run
Release / version (push) Waiting to run
Release / linux (push) Blocked by required conditions
Release / flatpak (push) Blocked by required conditions
Release / windows (push) Blocked by required conditions
Release / publish (push) Blocked by required conditions
The "Board" pane next to the game list (Ctrl+B): moves played on it filter the list to the games that reach the position by any move order, together with the filters; the moves played next are listed with games, a results bar, average rating and last year. Games reached by another order are marked and "only this order" narrows the list; strongest games first. A game opens at the found position; "Find in database" (Ctrl+Shift+F) searches the position of the game on screen. Large ChessBase databases: a fast main-line walk (moves built from the board), pruning by material and pawns, the .cbb search booster when valid; Mega Database: 0.4 s for the starting position, under 0.1 s deeper. PGN databases and books: positions of all games, variations included, kept in memory once built. Also: × to clear filter and search fields; "Open as a new game" opens at the last move; the "Moves" column is now "Length". Docs: decision 13, design (docs/design/board-search.md), research (position-search, position-index), dev notes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
116 lines
3.6 KiB
Rust
116 lines
3.6 KiB
Rust
//! Measures the position search of a PGN database: building the table and queries.
|
|
//!
|
|
//! cargo run --release -p chess-import --example position_search -- "Book.pgn" [copies]
|
|
//!
|
|
//! `copies` > 1 joins that many copies of the database's games (a synthetic large
|
|
//! database).
|
|
|
|
use std::sync::atomic::{AtomicBool, AtomicUsize};
|
|
use std::time::Instant;
|
|
|
|
use chess_core::PgnDatabase;
|
|
use chess_import::pgn_positions::PositionTable;
|
|
use chess_import::position::PositionQuery;
|
|
use shakmaty::san::San;
|
|
use shakmaty::{Chess, Position};
|
|
|
|
fn rss_mb() -> f64 {
|
|
std::fs::read_to_string("/proc/self/statm")
|
|
.ok()
|
|
.and_then(|s| s.split_whitespace().nth(1)?.parse::<f64>().ok())
|
|
.map_or(0.0, |pages| pages * 4096.0 / 1e6)
|
|
}
|
|
|
|
/// Peak resident memory of the process.
|
|
fn peak_mb() -> f64 {
|
|
std::fs::read_to_string("/proc/self/status")
|
|
.ok()
|
|
.and_then(|s| {
|
|
let l = s.lines().find(|l| l.starts_with("VmHWM:"))?;
|
|
l.split_whitespace().nth(1)?.parse::<f64>().ok()
|
|
})
|
|
.map_or(0.0, |kb| kb * 1024.0 / 1e6)
|
|
}
|
|
|
|
fn line(moves: &str) -> PositionQuery {
|
|
let mut pos = Chess::default();
|
|
let mut line = Vec::new();
|
|
for s in moves.split_whitespace() {
|
|
let m = s.parse::<San>().unwrap().to_move(&pos).unwrap();
|
|
line.push(m);
|
|
pos.play_unchecked(m);
|
|
}
|
|
PositionQuery::new(pos, Some(line))
|
|
}
|
|
|
|
fn main() {
|
|
let mut args = std::env::args().skip(1);
|
|
let Some(path) = args.next() else {
|
|
eprintln!("usage: position_search <database> [copies]");
|
|
std::process::exit(2);
|
|
};
|
|
let copies: usize = args.next().and_then(|s| s.parse().ok()).unwrap_or(1);
|
|
let never = AtomicBool::new(false);
|
|
let opened =
|
|
chess_import::open(std::path::Path::new(&path), &mut |_| {}, &never).expect("opens");
|
|
let mut text = String::new();
|
|
for i in 0..opened.db.len() {
|
|
if let Some(t) = opened.db.raw_text(i) {
|
|
text.push_str(t.trim_end());
|
|
text.push_str("\n\n");
|
|
}
|
|
}
|
|
let one = text.clone();
|
|
for _ in 1..copies {
|
|
text.push_str(&one);
|
|
}
|
|
drop(opened);
|
|
let db = PgnDatabase::from_bytes(text.into_bytes(), None, &mut |_| {}, &never).unwrap();
|
|
let before = rss_mb();
|
|
let peak_before = peak_mb();
|
|
let t0 = Instant::now();
|
|
let table = PositionTable::build(&db, &AtomicUsize::new(0), &never).unwrap();
|
|
println!(
|
|
"{} games, {} positions; build {:.2?}; table {:.1} MB (RSS +{:.1} MB, peak +{:.1} MB)",
|
|
table.games(),
|
|
table.positions(),
|
|
t0.elapsed(),
|
|
table.memory() as f64 / 1e6,
|
|
rss_mb() - before,
|
|
peak_mb() - peak_before,
|
|
);
|
|
let queries = [
|
|
"",
|
|
"e4",
|
|
"d4 Nf6 c4 g6",
|
|
"e4 e5 Nf3 Nc6 Bb5 a6 Ba4 Nf6 O-O Be7",
|
|
"e4 c5 Nf3 d6 d4 cxd4 Nxd4 Nf6 Nc3 a6",
|
|
];
|
|
for q in queries {
|
|
let q = line(q);
|
|
let t0 = Instant::now();
|
|
let runs = 20;
|
|
let mut out = None;
|
|
for _ in 0..runs {
|
|
out = table.search(&db, None, &q, &never);
|
|
}
|
|
let out = out.unwrap();
|
|
println!(
|
|
"{:>4} matches, {:>2} moves: {:.2?} [{}]",
|
|
out.matches.len(),
|
|
out.moves.len(),
|
|
t0.elapsed() / runs,
|
|
q.line.as_ref().map_or(0, |l| l.len()),
|
|
);
|
|
}
|
|
// Candidates: every other game.
|
|
let half: Vec<u32> = (0..db.len() as u32).step_by(2).collect();
|
|
let q = line("e4");
|
|
let t0 = Instant::now();
|
|
let out = table.search(&db, Some(&half), &q, &never).unwrap();
|
|
println!(
|
|
"1.e4 among half the games: {} matches, {:.2?}",
|
|
out.matches.len(),
|
|
t0.elapsed()
|
|
);
|
|
}
|