All Projects → niklasf → rust-pgn-reader

niklasf / rust-pgn-reader

Licence: GPL-3.0 License
Fast non-allocating and streaming reader for chess games in PGN notation

Programming Languages

rust
11053 projects

Labels

Projects that are alternatives of or similar to rust-pgn-reader

kokopu
A JavaScript/TypeScript library implementing the chess game rules and providing tools to read/write the standard chess file formats.
Stars: ✭ 18 (-60.87%)
Mutual labels:  chess, pgn
php-chess
A chess library for PHP.
Stars: ✭ 42 (-8.7%)
Mutual labels:  chess, pgn
chess-puzzle-maker
Creates chess puzzles from chess games and positions
Stars: ✭ 34 (-26.09%)
Mutual labels:  chess, pgn
liPGN
This program uses the lichess REST API to retrieve all the games of a user and transform them into a big PGN file that you can then import into your favourite game analysis tool (ie. scid)
Stars: ✭ 14 (-69.57%)
Mutual labels:  chess, pgn
cm-pgn
Parse and create PGNs (Portable Game Notation for chess games)
Stars: ✭ 16 (-65.22%)
Mutual labels:  chess, pgn
chess
An R package to read, write, create and explore chess games
Stars: ✭ 60 (+30.43%)
Mutual labels:  chess, pgn
chessalyzer.js
A JavaScript library for batch analyzing chess games
Stars: ✭ 14 (-69.57%)
Mutual labels:  chess, pgn
ChessVisionBot
Chessbot using computer vision to play on any chess website
Stars: ✭ 32 (-30.43%)
Mutual labels:  chess
Minic
A simple chess engine to learn and play with
Stars: ✭ 65 (+41.3%)
Mutual labels:  chess
realtimechess
In Real-time Chess (or Kung-Fu Chess, Ninja Chess), all pieces can be moved simultaneously!
Stars: ✭ 16 (-65.22%)
Mutual labels:  chess
ChessLiteGUI
A lightweight Chess GUI for playing Chess on a personal computer.
Stars: ✭ 2 (-95.65%)
Mutual labels:  chess
littlewing
Chess engine written in Rust ♛
Stars: ✭ 27 (-41.3%)
Mutual labels:  chess
stockfish-chess-web-gui
Responsive chess web GUI to play against the Stockfish 10 chess engine. Multiple web GUI implementations have also been included.
Stars: ✭ 21 (-54.35%)
Mutual labels:  chess
NextCommunity.github.io
Join FREE: Community of open-source programmers and software engineers.
Stars: ✭ 29 (-36.96%)
Mutual labels:  chess
liground
A free, open-source and modern Chess Variant Analysis GUI for the 21st century
Stars: ✭ 41 (-10.87%)
Mutual labels:  chess
magic-bits
A C++ header-only library for efficient move generation in Chess using "magic bitboards" technique
Stars: ✭ 26 (-43.48%)
Mutual labels:  chess
chess
Chess (game)(♟) built in C# and ASCII art.
Stars: ✭ 20 (-56.52%)
Mutual labels:  chess
Play-online-chess-with-real-chess-board
Program that enables you to play online chess using real chess board.
Stars: ✭ 288 (+526.09%)
Mutual labels:  chess
lila-gif
Webservice to render Gifs of chess positions and games, and stream them frame by frame
Stars: ✭ 63 (+36.96%)
Mutual labels:  chess
eschecs
UCI chess GUI
Stars: ✭ 32 (-30.43%)
Mutual labels:  chess

pgn-reader

A fast non-allocating and streaming reader for chess games in PGN notation, as a Rust library.

Build Status crates.io docs.rs

State of the library

⚠️ The current implementation may be very slow on Windows (#17) and has lost some performance compared to the mmap based approach from old versions (#12). It is likely that these shortcomings will require a rewrite of all internals with a completely different strategy. Until then, I intend to do minimal maintenance, following shakmaty as required.

Nonetheless, it is probably still one of the fastest PGN parsers around.

Introduction

Reader parses games and calls methods of a user provided Visitor. Implementing custom visitors allows for maximum flexibility:

  • The reader itself does not allocate (besides a single fixed-size buffer). The visitor can decide if and how to represent games in memory.
  • The reader does not validate move legality. This allows implementing support for custom chess variants, or delaying move validation.
  • The visitor can signal to the reader that it does not care about a game or variation.

Example

A visitor that counts the number of syntactically valid moves in the mainline of each game.

use std::io;
use pgn_reader::{Visitor, Skip, BufferedReader, SanPlus};

struct MoveCounter {
    moves: usize,
}

impl MoveCounter {
    fn new() -> MoveCounter {
        MoveCounter { moves: 0 }
    }
}

impl Visitor for MoveCounter {
    type Result = usize;

    fn begin_game(&mut self) {
        self.moves = 0;
    }

    fn san(&mut self, _san_plus: SanPlus) {
        self.moves += 1;
    }

    fn begin_variation(&mut self) -> Skip {
        Skip(true) // stay in the mainline
    }

    fn end_game(&mut self) -> Self::Result {
        self.moves
    }
}

fn main() -> io::Result<()> {
    let pgn = b"1. e4 e5 2. Nf3 (2. f4)
                { game paused due to bad weather }
                2... Nf6 *";

    let mut reader = BufferedReader::new_cursor(&pgn[..]);

    let mut counter = MoveCounter::new();
    let moves = reader.read_game(&mut counter)?;

    assert_eq!(moves, Some(4));
    Ok(())
}

Documentation

Read the documentation

Benchmarks (v0.12.0)

Run with lichess_db_standard_rated_2018-10.pgn (24,784,600 games, 52,750 MB uncompressed) on an SSD (Samsung 850), Intel i7-6850K CPU @ 3.60 GHz:

Benchmark Time Throughput
examples/stats.rs 111.9s 471.4 MB/s
examples/validate.rs 237.1s 222.5 MB/s
examples/parallel_validate.rs 148.6s 355.0 MB/s
scoutfish make 269.2s 196.0 MB/s
grep -F "[Event " -c 39.2s 1345.7 MB/s

examples/stats.rs with compressed files:

Compression File size Time Throughput
none 52,750 MB 111.9s 471.4 MB/s
bz2 6,226 MB 1263.1s 4.9 MB/s
xz 6,989 MB 495.9s 14.1 MB/s
gz 10,627 MB 335.7s 31.7 MB/s
lz4 16,428 MB 180.0s 91.3 MB/s

License

pgn-reader is licensed under the GPL-3.0 (or any later version at your option). See the COPYING file for the full license text.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].