在PGN符号中,作为rust库中的国际象棋游戏的快速非分配和流式读者。
Reader
解析了用户提供Visitor
的游戏和调用方法。实施自定义访问者可以最大程度的灵活性:
在每个游戏的主线中计算句法有效移动数量的访问者。
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 ( ( ) )
}
阅读文档
与旧版本的基于mmap
的方法相比,API可能更清洁,性能可能会略有回归(#12)。这需要一些关注。直到我到处走动之前,我只按照要求进行最小的shakmaty
。
但是,它可能仍然是周围最快的PGN解析器之一。
使用LICHESS_DB_STANDARD_2018-10.PGN(24,784,600游戏,52,750 MB未压缩)在SSD上(三星850),Intel I7-6850K CPU @ 3.60 GHz:
基准 | 时间 | 吞吐量 |
---|---|---|
示例/Stats.rs | 111.9s | 471.4 MB/s |
示例/validate.rs | 237.1s | 222.5 MB/s |
示例/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
:
压缩 | 文件大小 | 时间 | 吞吐量 |
---|---|---|---|
没有任何 | 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.7 | 31.7 MB/s |
LZ4 | 16,428 MB | 180.0s | 91.3 MB/s |
PGN阅读器已根据GPL-3.0(或您选项的任何以后版本)获得许可。请参阅复制文件以获取完整的许可文本。