Skip to content

Commit d5b5535

Browse files
committed
perf(main): store STYLE_CSS as Bytes for O(1) clone per request
1 parent 286e782 commit d5b5535

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

src/main.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#![forbid(unsafe_code)]
33
#![allow(clippy::cmp_owned)]
44

5+
use bytes::Bytes;
56
use cached::proc_macro::cached;
67
use clap::{Arg, ArgAction, Command};
78
use std::sync::LazyLock;
@@ -101,14 +102,18 @@ async fn resource(body: &str, content_type: &str, cache: bool) -> Result<Respons
101102
Ok(res)
102103
}
103104

104-
static STYLE_CSS: LazyLock<String> = LazyLock::new(|| {
105+
static STYLE_CSS: LazyLock<Bytes> = LazyLock::new(|| {
105106
let mut res = include_str!("../static/style.css").to_string();
106107
for file in ThemeAssets::iter() {
107108
res.push('\n');
108-
let theme = ThemeAssets::get(file.as_ref()).unwrap();
109-
res.push_str(std::str::from_utf8(theme.data.as_ref()).unwrap());
109+
let theme = ThemeAssets::get(file.as_ref())
110+
.expect("ThemeAssets::iter produced a key not recognised by ThemeAssets::get");
111+
res.push_str(
112+
std::str::from_utf8(theme.data.as_ref())
113+
.expect("Theme CSS asset is not valid UTF-8"),
114+
);
110115
}
111-
res
116+
Bytes::from(res)
112117
});
113118

114119
async fn style() -> Result<Response<Body>, String> {

0 commit comments

Comments
 (0)