[PATCH] Minor perf improvements via smartstring
Export this patch
---
Cargo.lock | 30 ++++++++++++++++++++++++++++++
pgn/Cargo.toml | 2 ++
pgn/src/parse/file.rs | 2 +-
pgn/src/parse/mod.rs | 7 ++++---
pgn/src/parse/moves.rs | 4 ++--
pgn/src/parse/san.rs | 2 +-
pgn/src/parse/tag.rs | 9 ++-------
7 files changed, 42 insertions(+), 14 deletions(-)
diff --git a/Cargo.lock b/Cargo.lock
index 86e7764..fa492bc 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2,6 +2,12 @@
# It is not intended for manual editing.
version = 3
+[[package]]
+name = "autocfg"
+version = "1.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
+
[[package]]
name = "isabella-db"
version = "0.1.0"
@@ -36,6 +42,7 @@ name = "pgn"
version = "0.1.0"
dependencies = [
"nom",
+ "smartstring",
"thiserror",
]
@@ -57,6 +64,23 @@ dependencies = [
"proc-macro2",
]
+[[package]]
+name = "smartstring"
+version = "1.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3fb72c633efbaa2dd666986505016c32c3044395ceaf881518399d2f4127ee29"
+dependencies = [
+ "autocfg",
+ "static_assertions",
+ "version_check",
+]
+
+[[package]]
+name = "static_assertions"
+version = "1.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
+
[[package]]
name = "syn"
version = "1.0.103"
@@ -93,3 +117,9 @@ name = "unicode-ident"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3"
+
+[[package]]
+name = "version_check"
+version = "0.9.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
diff --git a/pgn/Cargo.toml b/pgn/Cargo.toml
index e47a3f7..0a30ecc 100644
--- a/pgn/Cargo.toml
+++ b/pgn/Cargo.toml
@@ -12,3 +12,5 @@ license = "MPL-2.0"
nom = "7.1.1"
thiserror = { workspace = true }
+
+smartstring = "1.0.1"
diff --git a/pgn/src/parse/file.rs b/pgn/src/parse/file.rs
index f4c7ca7..2e71d7d 100644
--- a/pgn/src/parse/file.rs
+++ b/pgn/src/parse/file.rs
@@ -9,7 +9,7 @@ pub fn load_from_file<P: AsRef<Path>>(filename: P) -> Result<Vec<GameRecord>, Pa
let mut buf = String::new();
- let mut games = vec![];
+ let mut games = Vec::with_capacity(6_000_000);
let mut num_errors = 0;
let mut num_total = 0;
diff --git a/pgn/src/parse/mod.rs b/pgn/src/parse/mod.rs
index a0756b4..6167052 100644
--- a/pgn/src/parse/mod.rs
+++ b/pgn/src/parse/mod.rs
@@ -10,11 +10,12 @@ use moves::{parse_moves, parse_result, GameResult, Moves};
use nom::{bytes::complete::take_while, IResult};
use tag::{parse_tags, Tags};
use thiserror::Error;
+use smartstring::alias::String;
use self::util::is_bom;
/// Contains the tags, move list, and result of a game parsed from PGN format.
-#[derive(Debug)]
+#[derive(Clone, Debug)]
pub struct GameRecord {
pub tags: Tags,
pub moves: Moves,
@@ -28,12 +29,12 @@ pub enum ParseError {
IOError(#[from] std::io::Error),
- NomError(String),
+ NomError(nom::Err<nom::error::Error<std::string::String>>),
}
impl From<nom::Err<nom::error::Error<&str>>> for ParseError {
fn from(value: nom::Err<nom::error::Error<&str>>) -> Self {
- Self::NomError(format!("{value:?}"))
+ Self::NomError(value.to_owned())
}
}
diff --git a/pgn/src/parse/moves.rs b/pgn/src/parse/moves.rs
index 5baa074..83c0805 100644
--- a/pgn/src/parse/moves.rs
+++ b/pgn/src/parse/moves.rs
@@ -11,12 +11,12 @@ use nom::{
use super::{san::parse_san, san::SANString, util::is_whitespace};
use crate::parse::ParseError;
-#[derive(Debug)]
+#[derive(Clone, Debug)]
pub struct Moves {
pub moves: Vec<SANString>,
}
-#[derive(Debug, Eq, PartialEq)]
+#[derive(Clone, Debug, Eq, PartialEq)]
pub enum GameResult {
WhiteWins,
BlackWins,
diff --git a/pgn/src/parse/san.rs b/pgn/src/parse/san.rs
index b2dd883..41f0535 100644
--- a/pgn/src/parse/san.rs
+++ b/pgn/src/parse/san.rs
@@ -13,7 +13,7 @@ use super::util::{is_san_annotation, is_san_char};
const MAX_SAN: usize = 8;
/// A string representing a move in Standard Algebraic Notation.
-#[derive(Debug, Eq, PartialEq)]
+#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SANString {
raw: [u8; MAX_SAN],
len: usize,
diff --git a/pgn/src/parse/tag.rs b/pgn/src/parse/tag.rs
index f4be547..edef2f8 100644
--- a/pgn/src/parse/tag.rs
+++ b/pgn/src/parse/tag.rs
@@ -1,4 +1,5 @@
use std::collections::HashMap;
+use smartstring::alias::String;
use nom::{
bytes::complete::{escaped, is_not, take_till, take_while},
@@ -15,7 +16,7 @@ use super::util::is_whitespace;
///
/// If a tag contains multiple values, they are **not** split; that is done by
/// the consumer of the tags, optionally.
-#[derive(Debug, Default)]
+#[derive(Clone, Debug, Default)]
pub struct Tags {
pub tags: HashMap<String, String>,
}
@@ -54,12 +55,6 @@ impl Tags {
}
}
-#[derive(Debug)]
-pub struct Tag {
- pub key: String,
- pub value: String,
-}
-
/// parses a list of tags until there are no more tags available to parse
pub fn parse_tags(input: &str) -> IResult<&str, Tags> {
let whitespace_stripped_parse = preceded(take_while(is_whitespace), parse_tag);
--
2.37.3