Skip to content

Commit 28b9616

Browse files
committed
Replace usages of sha2 with xxhash
1 parent 37f9347 commit 28b9616

File tree

5 files changed

+96
-72
lines changed

5 files changed

+96
-72
lines changed

Cargo.lock

Lines changed: 80 additions & 58 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ bat = { version = "0.24.0", default-features = false, features = [
1919
bincode = "1.3"
2020
bytes = "1.5"
2121
clap = { version = "4.4.10", features = ["cargo", "derive"] }
22+
const-hex = "1.12"
23+
const_format = "0.2"
2224
comrak = "0.28.0"
2325
console-subscriber = { version = "0.4", features = ["parking_lot"] }
2426
flate2 = "1.0"
2527
futures = "0.3"
2628
gix = "0.66"
27-
hex = "0.4"
2829
httparse = "1.7"
2930
humantime = "2.1"
3031
itertools = "0.13.0"
@@ -35,7 +36,6 @@ rand = "0.8.5"
3536
rocksdb = { version = "0.22", default-features = false, features = ["snappy"] }
3637
rust-ini = "0.21.1"
3738
serde = { version = "1.0", features = ["derive", "rc"] }
38-
sha2 = "0.10"
3939
syntect = "5"
4040
tar = "0.4"
4141
time = { version = "0.3", features = ["serde"] }
@@ -52,6 +52,7 @@ tracing-subscriber = { version = "0.3", features = ["env-filter"] }
5252
unix_mode = "0.1"
5353
uuid = { version = "1.7", features = ["v4"] }
5454
yoke = { version = "0.7.1", features = ["derive"] }
55+
xxhash-rust = { version = "0.8.12", features = ["const_xxh3"] }
5556

5657
[build-dependencies]
5758
anyhow = "1.0"

src/main.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::{
77
net::SocketAddr,
88
path::PathBuf,
99
str::FromStr,
10-
sync::{Arc, LazyLock, OnceLock},
10+
sync::{Arc, OnceLock},
1111
time::Duration,
1212
};
1313

@@ -23,9 +23,9 @@ use axum::{
2323
};
2424
use bat::assets::HighlightingAssets;
2525
use clap::Parser;
26+
use const_format::formatcp;
2627
use database::schema::SCHEMA_VERSION;
2728
use rocksdb::{Options, SliceTransform};
28-
use sha2::{digest::FixedOutput, Digest};
2929
use syntect::html::ClassStyle;
3030
use tokio::{
3131
net::TcpListener,
@@ -36,6 +36,7 @@ use tower_http::{cors::CorsLayer, timeout::TimeoutLayer};
3636
use tower_layer::layer_fn;
3737
use tracing::{error, info, instrument, warn};
3838
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};
39+
use xxhash_rust::const_xxh3;
3940

4041
use crate::{
4142
database::schema::prefixes::{
@@ -54,8 +55,10 @@ mod unified_diff_builder;
5455

5556
const CRATE_VERSION: &str = clap::crate_version!();
5657

57-
static GLOBAL_CSS: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/statics/css/style.css",));
58-
static GLOBAL_CSS_HASH: LazyLock<Box<str>> = LazyLock::new(|| build_asset_hash(GLOBAL_CSS));
58+
const GLOBAL_CSS: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/statics/css/style.css"));
59+
const GLOBAL_CSS_HASH: &str = const_hex::Buffer::<16, false>::new()
60+
.const_format(&const_xxh3::xxh3_128(GLOBAL_CSS).to_be_bytes())
61+
.as_str();
5962

6063
static HIGHLIGHT_CSS_HASH: OnceLock<Box<str>> = OnceLock::new();
6164
static DARK_HIGHLIGHT_CSS_HASH: OnceLock<Box<str>> = OnceLock::new();
@@ -187,7 +190,7 @@ async fn main() -> Result<(), anyhow::Error> {
187190
let app = Router::new()
188191
.route("/", get(methods::index::handle))
189192
.route(
190-
&format!("/style-{}.css", *GLOBAL_CSS_HASH),
193+
formatcp!("/style-{}.css", GLOBAL_CSS_HASH),
191194
get(static_css(GLOBAL_CSS)),
192195
)
193196
.route(
@@ -325,10 +328,8 @@ async fn run_indexer(
325328

326329
#[must_use]
327330
pub fn build_asset_hash(v: &[u8]) -> Box<str> {
328-
let mut hasher = sha2::Sha256::default();
329-
hasher.update(v);
330-
let mut out = hex::encode(hasher.finalize_fixed());
331-
out.truncate(10);
331+
let hasher = xxhash_rust::const_xxh3::xxh3_128(v);
332+
let out = const_hex::encode(&hasher.to_be_bytes());
332333
Box::from(out)
333334
}
334335

src/methods/filters.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ pub fn file_perms(s: &u16) -> Result<String, askama::Error> {
2222
}
2323

2424
pub fn hex(s: &[u8]) -> Result<String, askama::Error> {
25-
Ok(hex::encode(s))
25+
Ok(const_hex::encode(s))
2626
}
2727

2828
pub fn md5(s: &str) -> Result<String, askama::Error> {
29-
Ok(hex::encode(md5::compute(s).0))
29+
Ok(const_hex::encode(md5::compute(s).0))
3030
}
3131

3232
#[allow(dead_code)]

templates/base.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<meta charset="UTF-8">
55
<meta name="viewport" content="width=device-width,initial-scale=1">
66
<title>{% block title %}rgit{% endblock %}</title>
7-
<link rel="stylesheet" type="text/css" href="/style-{{ crate::GLOBAL_CSS_HASH.as_ref() }}.css" />
7+
<link rel="stylesheet" type="text/css" href="/style-{{ crate::GLOBAL_CSS_HASH }}.css" />
88
{%- block head -%}{%- endblock %}
99
</head>
1010

0 commit comments

Comments
 (0)