From d0bde327c2fa1992a9f66f18a2ec9bcd4556c061 Mon Sep 17 00:00:00 2001 From: Oliver Hamlet Date: Sat, 13 Jun 2020 23:17:32 +0100 Subject: [PATCH] Speed up CRC32 calculation It turns out that passing in one byte at a time is really inefficient (which really should have been obvious...). For small files the difference is insignificant, but for Skyrim SE's Update.esm (18 MB) feeding in the buffered file content slice made the calculation twice as fast, and this seems to have non-linear effect, as the benchmark against Dragonborn.esm (63 MB) would have taken ~ 1137 seconds to run when passing one byte at a time (the benchmark using a buffer took ~ 5 seconds). On top of that, switching to the crc32fast crate has an insignificant effect on benchmarking with Blank.esm, but Update.esm's benchmark was twice as fast again, and when benchmarking with Dragonborn.esm, using crc32fast was ~ 170x faster than using the crc crate! --- Cargo.toml | 2 +- src/function/eval.rs | 22 +++++++++++++--------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 80da386..3d692dc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ license = "MIT" edition = "2018" [dependencies] -crc = "1.0.0" +crc32fast = "1.2.0" esplugin = "3.0.0" nom = "5.0.0" pelite = "0.8.0" diff --git a/src/function/eval.rs b/src/function/eval.rs index 04d3dd2..32e397d 100644 --- a/src/function/eval.rs +++ b/src/function/eval.rs @@ -1,10 +1,9 @@ use std::ffi::OsStr; use std::fs::{read_dir, File}; use std::hash::Hasher; -use std::io::{BufReader, Read}; +use std::io::{BufRead, BufReader}; use std::path::{Path, PathBuf}; -use crc::{crc32, Hasher32}; use regex::Regex; use super::{ComparisonOperator, Function}; @@ -157,16 +156,21 @@ fn evaluate_checksum(state: &State, file_path: &Path, crc: u32) -> Result