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 +++--- src/methods/filters.rs | 4 +- templates/base.html | 2 +- 5 files changed, 96 insertions(+), 72 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4139f0a..d2b63e4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -410,15 +410,6 @@ version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" 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" @@ -666,6 +657,19 @@ dependencies = [ "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]] name = "const-random" version = "0.1.18" @@ -686,6 +690,26 @@ dependencies = [ "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]] name = "content_inspector" version = "0.2.4" @@ -743,16 +767,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" version = "0.20.10" @@ -849,16 +863,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" version = "0.5.2" @@ -1086,16 +1090,6 @@ dependencies = [ "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]] name = "getrandom" version = "0.2.15" @@ -2541,6 +2535,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" dependencies = [ "autocfg", + "libm", ] [[package]] @@ -2737,6 +2732,22 @@ dependencies = [ "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]] name = "prost" version = "0.13.3" @@ -2832,6 +2843,15 @@ 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]] name = "raw-cpuid" version = "11.1.0" @@ -2917,10 +2937,11 @@ dependencies = [ "clap", "comrak", "console-subscriber", + "const-hex", + "const_format", "flate2", "futures", "gix", - "hex", "httparse", "humantime", "itertools 0.13.0", @@ -2932,7 +2953,6 @@ dependencies = [ "rsass", "rust-ini", "serde", - "sha2", "syntect", "tar", "time", @@ -2948,6 +2968,7 @@ dependencies = [ "tracing-subscriber", "unix_mode", "uuid", + "xxhash-rust", "yoke", ] @@ -3129,17 +3150,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" version = "0.1.7" @@ -3662,12 +3672,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" version = "3.1.0" @@ -3677,6 +3681,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" version = "2.7.0" @@ -3719,6 +3729,12 @@ version = "0.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" 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" @@ -4076,6 +4092,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" version = "0.4.5" diff --git a/Cargo.toml b/Cargo.toml index e05198b..1c914a2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,12 +19,13 @@ bat = { version = "0.24.0", default-features = false, features = [ 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 @@ rand = "0.8.5" 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 @@ tracing-subscriber = { version = "0.3", features = ["env-filter"] } 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 +++ b/src/main.rs @@ -7,7 +7,7 @@ use std::{ net::SocketAddr, path::PathBuf, str::FromStr, - sync::{Arc, LazyLock, OnceLock}, + sync::{Arc, OnceLock}, time::Duration, }; @@ -23,9 +23,9 @@ use axum::{ }; 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_http::{cors::CorsLayer, timeout::TimeoutLayer}; 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 @@ mod unified_diff_builder; 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 @@ async fn main() -> Result<(), anyhow::Error> { 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 @@ async fn run_indexer( #[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/src/methods/filters.rs b/src/methods/filters.rs index 9d6748b..37368a4 100644 --- a/src/methods/filters.rs +++ b/src/methods/filters.rs @@ -22,11 +22,11 @@ pub fn file_perms(s: &u16) -> Result { } 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)] diff --git a/templates/base.html b/templates/base.html index ac8be2e..477cb47 100644 --- a/templates/base.html +++ b/templates/base.html @@ -4,7 +4,7 @@ {% block title %}rgit{% endblock %} - + {%- block head -%}{%- endblock %}