Skip to content

Commit

Permalink
Replace usages of sha2 with xxhash
Browse files Browse the repository at this point in the history
  • Loading branch information
w4 committed Sep 28, 2024
1 parent 37f9347 commit 28b9616
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 72 deletions.
138 changes: 80 additions & 58 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"] }
Expand All @@ -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"
Expand Down
19 changes: 10 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
net::SocketAddr,
path::PathBuf,
str::FromStr,
sync::{Arc, LazyLock, OnceLock},
sync::{Arc, OnceLock},
time::Duration,
};

Expand All @@ -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,
Expand All @@ -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::{
Expand All @@ -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<Box<str>> = 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<Box<str>> = OnceLock::new();
static DARK_HIGHLIGHT_CSS_HASH: OnceLock<Box<str>> = OnceLock::new();
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -325,10 +328,8 @@ async fn run_indexer(

#[must_use]
pub fn build_asset_hash(v: &[u8]) -> Box<str> {
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)
}

Expand Down
4 changes: 2 additions & 2 deletions src/methods/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ pub fn file_perms(s: &u16) -> Result<String, askama::Error> {
}

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

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

#[allow(dead_code)]
Expand Down
2 changes: 1 addition & 1 deletion templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>{% block title %}rgit{% endblock %}</title>
<link rel="stylesheet" type="text/css" href="/style-{{ crate::GLOBAL_CSS_HASH.as_ref() }}.css" />
<link rel="stylesheet" type="text/css" href="/style-{{ crate::GLOBAL_CSS_HASH }}.css" />
{%- block head -%}{%- endblock %}
</head>

Expand Down

0 comments on commit 28b9616

Please sign in to comment.