Skip to content

Commit c14d17d

Browse files
committed
web/statics, replace phf with sorted array & binary_search
1 parent 1decfa5 commit c14d17d

4 files changed

Lines changed: 24 additions & 20 deletions

File tree

Cargo.lock

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

crates/bin/docs_rs_web/Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ build = "build.rs"
99

1010
[package.metadata.cargo-machete]
1111
ignored = [
12-
"phf",
1312
# used in build script output
1413
"slug", # used in askama templates, can be moved to web binary
1514
]
@@ -51,7 +50,6 @@ lol_html = "2.0.0"
5150
mime = { workspace = true }
5251
num_cpus = "1.15.0"
5352
opentelemetry = { workspace = true }
54-
phf = "0.13.1"
5553
postcard = { workspace = true }
5654
rayon-core = "1.13.0"
5755
regex = { workspace = true }
@@ -76,7 +74,6 @@ url = { workspace = true }
7674
anyhow = { version = "1.0.42", features = ["backtrace"] }
7775
grass = { version = "0.13.1", default-features = false }
7876
md5 = "0.8.0"
79-
phf_codegen = "0.13"
8077
syntect = { version = "5.0.0", default-features = false, features = ["parsing", "dump-create", "yaml-load", "regex-onig"] }
8178
walkdir = { workspace = true }
8279

crates/bin/docs_rs_web/build.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use anyhow::{Context as _, Error, Result};
2-
use std::{env, fs::File, io::Write as _, path::Path};
2+
use std::{collections::BTreeMap, env, fs::File, io::Write as _, path::Path};
33

44
mod tracked {
55
use std::{
@@ -68,7 +68,7 @@ mod tracked {
6868
}
6969
}
7070

71-
type ETagMap<'a> = phf_codegen::Map<'a, String>;
71+
type ETagMap = BTreeMap<String, String>;
7272

7373
fn main() -> Result<()> {
7474
let out_dir = env::var("OUT_DIR").context("missing OUT_DIR")?;
@@ -81,11 +81,13 @@ fn main() -> Result<()> {
8181
calculate_static_etags(&mut etag_map)?;
8282

8383
let mut etag_file = File::create(out_dir.join("static_etag_map.rs"))?;
84-
writeln!(
85-
&mut etag_file,
86-
"pub static STATIC_ETAG_MAP: ::phf::Map<&'static str, &'static str> = {};",
87-
etag_map.build()
88-
)?;
84+
writeln!(etag_file, "pub const STATIC_ETAG_MAP: &[(&str, &str)] = &[")?;
85+
for (path, etag) in etag_map.iter() {
86+
// the debug repr of a `str` is also a valid escaped string literal in the code
87+
writeln!(etag_file, r#" ({:?}, {:?}), "#, path, etag)?;
88+
}
89+
writeln!(etag_file, "];")?;
90+
8991
etag_file.sync_all()?;
9092

9193
// trigger recompilation when a new migration is added
@@ -100,7 +102,7 @@ fn etag_from_path(path: impl AsRef<Path>) -> Result<String> {
100102
fn etag_from_content(content: impl AsRef<[u8]>) -> String {
101103
let digest = md5::compute(content);
102104
let md5_hex = format!("{:x}", digest);
103-
format!(r#""\"{md5_hex}\"""#)
105+
format!(r#""{md5_hex}""#)
104106
}
105107

106108
fn compile_sass_file(src: &Path, dest: &Path) -> Result<()> {
@@ -137,7 +139,7 @@ fn compile_sass(out_dir: &Path, etag_map: &mut ETagMap) -> Result<()> {
137139
})?;
138140

139141
let dest_str = dest.file_name().unwrap().to_str().unwrap().to_owned();
140-
etag_map.entry(dest_str, etag_from_path(&dest)?);
142+
etag_map.insert(dest_str, etag_from_path(&dest)?);
141143
}
142144
}
143145
}
@@ -148,7 +150,7 @@ fn compile_sass(out_dir: &Path, etag_map: &mut ETagMap) -> Result<()> {
148150
let vendored = pure + &grids;
149151
std::fs::write(out_dir.join("vendored").with_extension("css"), &vendored)?;
150152

151-
etag_map.entry(
153+
etag_map.insert(
152154
"vendored.css".to_owned(),
153155
etag_from_content(vendored.as_bytes()),
154156
);
@@ -169,7 +171,7 @@ fn calculate_static_etags(etag_map: &mut ETagMap) -> Result<()> {
169171

170172
let partial_path = path.strip_prefix(static_dir).unwrap();
171173
let partial_path_str = partial_path.to_string_lossy().to_string();
172-
etag_map.entry(partial_path_str, etag_from_path(path)?);
174+
etag_map.insert(partial_path_str, etag_from_path(path)?);
173175
}
174176
}
175177

crates/bin/docs_rs_web/src/handlers/statics.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,19 @@ async fn conditional_get(
102102
req: Request,
103103
next: Next,
104104
) -> Response {
105+
debug_assert!(STATIC_ETAG_MAP.is_sorted());
106+
105107
let if_none_match = if_none_match.map(|th| th.0);
106108
let resource_path = partial_uri.path().trim_start_matches('/');
107-
let Some(etag) = STATIC_ETAG_MAP.get(resource_path).map(|etag| {
108-
etag.parse::<ETag>()
109-
.expect("compile time generated, should always pass")
110-
}) else {
109+
let Some(etag) = STATIC_ETAG_MAP
110+
.binary_search_by_key(&resource_path, |(path, _)| *path)
111+
.ok()
112+
.map(|pos| {
113+
let etag = STATIC_ETAG_MAP[pos].1;
114+
etag.parse::<ETag>()
115+
.expect("compile time generated, should always pass")
116+
})
117+
else {
111118
let res = next.run(req).await;
112119

113120
debug_assert!(

0 commit comments

Comments
 (0)