Unverified Commit 26afa892 authored by Matt Mastracci's avatar Matt Mastracci Committed by GitHub

Replace Lazy<FxHashMap> static lookups with phf::Map (#93096)

## What

Trivial change: LazyLock<FxHashMap<...>> -> phf::Map.
parent 908717af
......@@ -4829,6 +4829,7 @@ dependencies = [
"next-custom-transforms",
"next-taskless",
"percent-encoding",
"phf",
"qstring",
"react_remove_properties",
"regex",
......@@ -10496,8 +10497,8 @@ dependencies = [
"bincode 2.0.1",
"image",
"mime",
"phf",
"regex",
"rustc-hash 2.1.1",
"serde",
"turbo-bincode",
"turbo-rcstr",
......
......@@ -25,6 +25,7 @@ itertools = { workspace = true }
lightningcss = { workspace = true }
mime_guess = "2.0.4"
percent-encoding = "2.3.1"
phf = { workspace = true }
qstring = { workspace = true }
react_remove_properties = { workspace = true }
regex = { workspace = true }
......
use std::{ops::Deref, sync::LazyLock};
use std::sync::LazyLock;
use anyhow::Result;
use phf::phf_map;
use regex::Regex;
use rustc_hash::FxHashMap;
use turbo_rcstr::RcStr;
use turbo_tasks_fs::FileSystemPath;
......@@ -11,28 +11,19 @@ use crate::next_app::{AppPage, AppPath, PageSegment, PageType};
pub mod image;
pub mod route;
pub static STATIC_LOCAL_METADATA: LazyLock<FxHashMap<&'static str, &'static [&'static str]>> =
LazyLock::new(|| {
FxHashMap::from_iter([
(
"icon",
&["ico", "jpg", "jpeg", "png", "svg"] as &'static [&'static str],
),
("apple-icon", &["jpg", "jpeg", "png"]),
("opengraph-image", &["jpg", "jpeg", "png", "gif"]),
("twitter-image", &["jpg", "jpeg", "png", "gif"]),
("sitemap", &["xml"]),
])
});
pub static STATIC_LOCAL_METADATA: phf::Map<&'static str, &'static [&'static str]> = phf_map! {
"icon" => &["ico", "jpg", "jpeg", "png", "svg"],
"apple-icon" => &["jpg", "jpeg", "png"],
"opengraph-image" => &["jpg", "jpeg", "png", "gif"],
"twitter-image" => &["jpg", "jpeg", "png", "gif"],
"sitemap" => &["xml"],
};
pub static STATIC_GLOBAL_METADATA: LazyLock<FxHashMap<&'static str, &'static [&'static str]>> =
LazyLock::new(|| {
FxHashMap::from_iter([
("favicon", &["ico"] as &'static [&'static str]),
("manifest", &["webmanifest", "json"]),
("robots", &["txt"]),
])
});
pub static STATIC_GLOBAL_METADATA: phf::Map<&'static str, &'static [&'static str]> = phf_map! {
"favicon" => &["ico"],
"manifest" => &["webmanifest", "json"],
"robots" => &["txt"],
};
pub struct MetadataFileMatch<'a> {
pub metadata_type: &'a str,
......@@ -56,7 +47,7 @@ fn match_numbered_metadata(stem: &str) -> Option<(&str, &str)> {
fn match_metadata_file<'a>(
filename: &'a str,
page_extensions: &[RcStr],
metadata: &FxHashMap<&str, &[&str]>,
metadata: &phf::Map<&'static str, &'static [&'static str]>,
) -> Option<MetadataFileMatch<'a>> {
let (stem, ext) = filename.split_once('.')?;
......@@ -123,7 +114,7 @@ pub fn match_local_metadata_file<'a>(
basename: &'a str,
page_extensions: &[RcStr],
) -> Option<MetadataFileMatch<'a>> {
match_metadata_file(basename, page_extensions, STATIC_LOCAL_METADATA.deref())
match_metadata_file(basename, page_extensions, &STATIC_LOCAL_METADATA)
}
pub struct GlobalMetadataFileMatch<'a> {
......@@ -135,7 +126,7 @@ pub fn match_global_metadata_file<'a>(
basename: &'a str,
page_extensions: &[RcStr],
) -> Option<GlobalMetadataFileMatch<'a>> {
match_metadata_file(basename, page_extensions, STATIC_GLOBAL_METADATA.deref()).map(|m| {
match_metadata_file(basename, page_extensions, &STATIC_GLOBAL_METADATA).map(|m| {
GlobalMetadataFileMatch {
metadata_type: m.metadata_type,
dynamic: m.dynamic,
......
use std::sync::LazyLock;
use anyhow::Result;
use async_trait::async_trait;
use rustc_hash::FxHashMap;
use phf::phf_map;
use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ResolvedVc, Vc};
use turbo_tasks_fs::{
......@@ -29,24 +27,18 @@ use crate::{next_server::ServerContextType, next_telemetry::ModuleFeatureTelemet
// Set of the features we want to track, following existing references in
// webpack/plugins/telemetry-plugin.
static FEATURE_MODULES: LazyLock<FxHashMap<&'static str, Vec<&'static str>>> =
LazyLock::new(|| {
FxHashMap::from_iter([
(
"next",
vec![
"/image",
"/future/image",
"/legacy/image",
"/script",
"/dynamic",
"/font/google",
"/font/local",
],
),
("@next/font", vec!["/google", "/local"]),
])
});
static FEATURE_MODULES: phf::Map<&'static str, &'static [&'static str]> = phf_map! {
"next" => &[
"/image",
"/future/image",
"/legacy/image",
"/script",
"/dynamic",
"/font/google",
"/font/local",
],
"@next/font" => &["/google", "/local"],
};
#[turbo_tasks::value(shared)]
pub struct InvalidImportModuleIssue {
......
......@@ -32,8 +32,8 @@ image = { workspace = true, default-features = false, features = [
"tga",
] }
mime = { workspace = true }
phf = { workspace = true }
regex = { workspace = true }
rustc-hash = { workspace = true }
serde = { workspace = true }
turbo-bincode = { workspace = true }
turbo-rcstr = { workspace = true }
......
......@@ -4,24 +4,22 @@
use std::sync::LazyLock;
use anyhow::{Result, anyhow, bail};
use phf::phf_map;
use regex::Regex;
use rustc_hash::FxHashMap;
const INCH_CM: f64 = 2.54;
static UNITS: LazyLock<FxHashMap<&str, f64>> = LazyLock::new(|| {
FxHashMap::from_iter([
("in", 96.0),
("cm", 96.0 / INCH_CM),
("em", 16.0),
("ex", 8.0),
("m", 96.0 / INCH_CM * 100.0),
("mm", 96.0 / INCH_CM / 10.0),
("pc", 96.0 / 72.0 / 12.0),
("pt", 96.0 / 72.0),
("px", 1.0),
("", 1.0),
])
});
static UNITS: phf::Map<&'static str, f64> = phf_map! {
"in" => 96.0,
"cm" => 96.0 / INCH_CM,
"em" => 16.0,
"ex" => 8.0,
"m" => 96.0 / INCH_CM * 100.0,
"mm" => 96.0 / INCH_CM / 10.0,
"pc" => 96.0 / 72.0 / 12.0,
"pt" => 96.0 / 72.0,
"px" => 1.0,
"" => 1.0,
};
static UNIT_REGEX: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"^([0-9.]+(?:e-?\d+)?)((?:in|cm|em|ex|m|mm|pc|pt|px)?)$").unwrap()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment