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