Skip to content

Commit 64fce54

Browse files
refactor(profiling): simplify Sapi::from_name() with match
Replace OnceLock<HashMap> with a simple match expression. The function is called exactly once per process (via LazyLock<SAPI>), so the HashMap was unnecessary overhead. Changes: - Remove std::collections::HashMap and OnceLock imports - 10-arm match instead of HashMap lookup - Zero heap allocation, faster, clearer
1 parent 698986d commit 64fce54

1 file changed

Lines changed: 12 additions & 21 deletions

File tree

profiling/src/sapi.rs

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
use crate::zend::sapi_request_info;
22
use log::warn;
33
use std::borrow::Cow;
4-
use std::collections::HashMap;
54
use std::ffi::{CStr, OsStr};
65
use std::fmt::{Display, Formatter};
76
use std::os::unix::ffi::OsStrExt;
87
use std::path::Path;
9-
use std::sync::OnceLock;
108

119
// todo: unify with ../component/sapi
1210
#[derive(Copy, Clone, Eq, PartialEq)]
@@ -27,25 +25,18 @@ pub enum Sapi {
2725

2826
impl Sapi {
2927
pub fn from_name(name: &str) -> Sapi {
30-
static SAPIS: OnceLock<HashMap<&str, Sapi>> = OnceLock::new();
31-
let sapis = SAPIS.get_or_init(|| {
32-
HashMap::from_iter([
33-
("apache2handler", Sapi::Apache2Handler),
34-
("cgi-fcgi", Sapi::CgiFcgi),
35-
("cli", Sapi::Cli),
36-
("cli-server", Sapi::CliServer),
37-
("embed", Sapi::Embed),
38-
("frankenphp", Sapi::FrankenPHP),
39-
("fpm-fcgi", Sapi::FpmFcgi),
40-
("litespeed", Sapi::Litespeed),
41-
("phpdbg", Sapi::PhpDbg),
42-
("tea", Sapi::Tea),
43-
])
44-
});
45-
46-
match sapis.get(name) {
47-
None => Sapi::Unknown,
48-
Some(sapi) => *sapi,
28+
match name {
29+
"apache2handler" => Sapi::Apache2Handler,
30+
"cgi-fcgi" => Sapi::CgiFcgi,
31+
"cli" => Sapi::Cli,
32+
"cli-server" => Sapi::CliServer,
33+
"embed" => Sapi::Embed,
34+
"frankenphp" => Sapi::FrankenPHP,
35+
"fpm-fcgi" => Sapi::FpmFcgi,
36+
"litespeed" => Sapi::Litespeed,
37+
"phpdbg" => Sapi::PhpDbg,
38+
"tea" => Sapi::Tea,
39+
_ => Sapi::Unknown,
4940
}
5041
}
5142

0 commit comments

Comments
 (0)