From 28b96164bace7b5b41f89b6d1acb754840af7e61 Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Sat, 28 Sep 2024 15:52:12 +0400 Subject: [PATCH] Replace usages of sha2 with xxhash --- Cargo.lock | 138 +++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------- Cargo.toml | 5 +++-- src/main.rs | 19 +++++++++++-------- templates/base.html | 2 +- src/methods/filters.rs | 4 ++-- 5 files changed, 96 insertions(+), 72 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4139f0a..d2b63e4 100644 --- a/Cargo.lock +++ a/Cargo.lock @@ -411,15 +411,6 @@ checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" [[package]] -name = "block-buffer" -version = "0.10.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" -dependencies = [ - "generic-array", -] - -[[package]] name = "bstr" version = "1.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -664,6 +655,19 @@ "tracing", "tracing-core", "tracing-subscriber", +] + +[[package]] +name = "const-hex" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94fb8a24a26d37e1ffd45343323dc9fe6654ceea44c12f2fcb3d7ac29e610bc6" +dependencies = [ + "cfg-if", + "cpufeatures", + "hex", + "proptest", + "serde", ] [[package]] @@ -684,6 +688,26 @@ "getrandom", "once_cell", "tiny-keccak", +] + +[[package]] +name = "const_format" +version = "0.2.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50c655d81ff1114fb0dcdea9225ea9f0cc712a6f8d189378e82bdf62a473a64b" +dependencies = [ + "const_format_proc_macros", +] + +[[package]] +name = "const_format_proc_macros" +version = "0.2.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eff1a44b93f47b1bac19a27932f5c591e43d1ba357ee4f61526c8a25603f0eb1" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", ] [[package]] @@ -742,16 +766,6 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" - -[[package]] -name = "crypto-common" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" -dependencies = [ - "generic-array", - "typenum", -] [[package]] name = "darling" @@ -848,16 +862,6 @@ version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "339544cc9e2c4dc3fc7149fd630c5f22263a4fdf18a98afd0075784968b5cf00" - -[[package]] -name = "digest" -version = "0.10.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" -dependencies = [ - "block-buffer", - "crypto-common", -] [[package]] name = "dlv-list" @@ -1084,16 +1088,6 @@ "pin-project-lite", "pin-utils", "slab", -] - -[[package]] -name = "generic-array" -version = "0.14.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" -dependencies = [ - "typenum", - "version_check", ] [[package]] @@ -2541,6 +2535,7 @@ checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" dependencies = [ "autocfg", + "libm", ] [[package]] @@ -2735,6 +2730,22 @@ dependencies = [ "bytesize", "human_format", +] + +[[package]] +name = "proptest" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4c2511913b88df1637da85cc8d96ec8e43a3f8bb8ccb71ee1ac240d6f3df58d" +dependencies = [ + "bitflags 2.6.0", + "lazy_static", + "num-traits", + "rand", + "rand_chacha", + "rand_xorshift", + "regex-syntax 0.8.4", + "unarray", ] [[package]] @@ -2830,6 +2841,15 @@ checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" dependencies = [ "getrandom", +] + +[[package]] +name = "rand_xorshift" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d25bf25ec5ae4a3f1b92f929810509a2f53d7dca2f50b794ff57e3face536c8f" +dependencies = [ + "rand_core", ] [[package]] @@ -2917,10 +2937,11 @@ "clap", "comrak", "console-subscriber", + "const-hex", + "const_format", "flate2", "futures", "gix", - "hex", "httparse", "humantime", "itertools 0.13.0", @@ -2932,7 +2953,6 @@ "rsass", "rust-ini", "serde", - "sha2", "syntect", "tar", "time", @@ -2948,6 +2968,7 @@ "tracing-subscriber", "unix_mode", "uuid", + "xxhash-rust", "yoke", ] @@ -3128,17 +3149,6 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bbfa15b3dddfee50a0fff136974b3e1bde555604ba463834a7eb7deb6417705d" - -[[package]] -name = "sha2" -version = "0.10.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8" -dependencies = [ - "cfg-if", - "cpufeatures", - "digest", -] [[package]] name = "sharded-slab" @@ -3661,12 +3671,6 @@ version = "2.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6af6ae20167a9ece4bcb41af5b80f8a1f1df981f6391189ce00fd257af04126a" - -[[package]] -name = "typenum" -version = "1.17.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" [[package]] name = "uluru" @@ -3676,6 +3680,12 @@ dependencies = [ "arrayvec", ] + +[[package]] +name = "unarray" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eaea85b334db583fe3274d12b4cd1880032beab409c0d774be044d4480ab9a94" [[package]] name = "unicase" @@ -3720,6 +3730,12 @@ checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" [[package]] +name = "unicode-xid" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" + +[[package]] name = "unicode_categories" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -4075,6 +4091,12 @@ version = "2.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "213b7324336b53d2414b2db8537e56544d981803139155afa84f76eeebb7a546" + +[[package]] +name = "xxhash-rust" +version = "0.8.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a5cbf750400958819fb6178eaa83bee5cd9c29a26a40cc241df8c70fdd46984" [[package]] name = "yaml-rust" diff --git a/Cargo.toml b/Cargo.toml index e05198b..1c914a2 100644 --- a/Cargo.toml +++ a/Cargo.toml @@ -19,12 +19,13 @@ bincode = "1.3" bytes = "1.5" clap = { version = "4.4.10", features = ["cargo", "derive"] } +const-hex = "1.12" +const_format = "0.2" comrak = "0.28.0" console-subscriber = { version = "0.4", features = ["parking_lot"] } flate2 = "1.0" futures = "0.3" gix = "0.66" -hex = "0.4" httparse = "1.7" humantime = "2.1" itertools = "0.13.0" @@ -35,7 +36,6 @@ rocksdb = { version = "0.22", default-features = false, features = ["snappy"] } rust-ini = "0.21.1" serde = { version = "1.0", features = ["derive", "rc"] } -sha2 = "0.10" syntect = "5" tar = "0.4" time = { version = "0.3", features = ["serde"] } @@ -52,6 +52,7 @@ unix_mode = "0.1" uuid = { version = "1.7", features = ["v4"] } yoke = { version = "0.7.1", features = ["derive"] } +xxhash-rust = { version = "0.8.12", features = ["const_xxh3"] } [build-dependencies] anyhow = "1.0" diff --git a/src/main.rs b/src/main.rs index 28e4ba7..0b6d1fc 100644 --- a/src/main.rs +++ a/src/main.rs @@ -7,7 +7,7 @@ net::SocketAddr, path::PathBuf, str::FromStr, - sync::{Arc, LazyLock, OnceLock}, + sync::{Arc, OnceLock}, time::Duration, }; @@ -23,9 +23,9 @@ }; use bat::assets::HighlightingAssets; use clap::Parser; +use const_format::formatcp; use database::schema::SCHEMA_VERSION; use rocksdb::{Options, SliceTransform}; -use sha2::{digest::FixedOutput, Digest}; use syntect::html::ClassStyle; use tokio::{ net::TcpListener, @@ -36,6 +36,7 @@ use tower_layer::layer_fn; use tracing::{error, info, instrument, warn}; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter}; +use xxhash_rust::const_xxh3; use crate::{ database::schema::prefixes::{ @@ -54,8 +55,10 @@ const CRATE_VERSION: &str = clap::crate_version!(); -static GLOBAL_CSS: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/statics/css/style.css",)); -static GLOBAL_CSS_HASH: LazyLock> = LazyLock::new(|| build_asset_hash(GLOBAL_CSS)); +const GLOBAL_CSS: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/statics/css/style.css")); +const GLOBAL_CSS_HASH: &str = const_hex::Buffer::<16, false>::new() + .const_format(&const_xxh3::xxh3_128(GLOBAL_CSS).to_be_bytes()) + .as_str(); static HIGHLIGHT_CSS_HASH: OnceLock> = OnceLock::new(); static DARK_HIGHLIGHT_CSS_HASH: OnceLock> = OnceLock::new(); @@ -187,7 +190,7 @@ let app = Router::new() .route("/", get(methods::index::handle)) .route( - &format!("/style-{}.css", *GLOBAL_CSS_HASH), + formatcp!("/style-{}.css", GLOBAL_CSS_HASH), get(static_css(GLOBAL_CSS)), ) .route( @@ -325,10 +328,8 @@ #[must_use] pub fn build_asset_hash(v: &[u8]) -> Box { - let mut hasher = sha2::Sha256::default(); - hasher.update(v); - let mut out = hex::encode(hasher.finalize_fixed()); - out.truncate(10); + let hasher = xxhash_rust::const_xxh3::xxh3_128(v); + let out = const_hex::encode(&hasher.to_be_bytes()); Box::from(out) } diff --git a/templates/base.html b/templates/base.html index ac8be2e..477cb47 100644 --- a/templates/base.html +++ a/templates/base.html @@ -1,10 +1,10 @@ {% block title %}rgit{% endblock %} - + {%- block head -%}{%- endblock %} diff --git a/src/methods/filters.rs b/src/methods/filters.rs index 9d6748b..37368a4 100644 --- a/src/methods/filters.rs +++ a/src/methods/filters.rs @@ -22,11 +22,11 @@ } pub fn hex(s: &[u8]) -> Result { - Ok(hex::encode(s)) + Ok(const_hex::encode(s)) } pub fn md5(s: &str) -> Result { - Ok(hex::encode(md5::compute(s).0)) + Ok(const_hex::encode(md5::compute(s).0)) } #[allow(dead_code)] -- rgit 0.1.3