Skip to content

Commit 52ec981

Browse files
committed
perf(safari): cache emulators per OS to avoid Arc::make_mut clones
Split the cached BrowserEmulator by OS (iOS/macOS) so os_type is set at initialization time. This avoids a deep clone on every build_safari_settings call via Arc::make_mut.
1 parent 0b0b2d1 commit 52ec981

1 file changed

Lines changed: 45 additions & 13 deletions

File tree

  • crates/primp/src/imp/safari

crates/primp/src/imp/safari/mod.rs

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ pub(crate) fn build_safari_settings(
4848
};
4949

5050
// Get cached browser emulator for Safari (Arc clone = cheap refcount increment)
51-
let mut browser_emulator = safari_emulator(safari, browser_os);
52-
// Set OS type on our unique clone (cached emulator retains None)
53-
Arc::make_mut(&mut browser_emulator).os_type = Some(browser_os);
51+
let browser_emulator = safari_emulator(safari, browser_os);
5452

5553
let http2 = build_http2_settings();
5654

@@ -118,16 +116,50 @@ fn build_http2_settings() -> crate::imp::Http2Data {
118116

119117
fn safari_emulator(safari: Impersonate, browser_os: BrowserEmulatorOS) -> Arc<BrowserEmulator> {
120118
match safari {
121-
Impersonate::SafariV18_5 => {
122-
static EMU: OnceLock<Arc<BrowserEmulator>> = OnceLock::new();
123-
EMU.get_or_init(|| Arc::new(new_safari_18_5_emulator()))
124-
.clone()
125-
}
126-
Impersonate::SafariV26 => {
127-
static EMU: OnceLock<Arc<BrowserEmulator>> = OnceLock::new();
128-
EMU.get_or_init(|| Arc::new(new_safari_26_emulator()))
129-
.clone()
130-
}
119+
Impersonate::SafariV18_5 => match browser_os {
120+
BrowserEmulatorOS::IOS => {
121+
static EMU_IOS: OnceLock<Arc<BrowserEmulator>> = OnceLock::new();
122+
EMU_IOS
123+
.get_or_init(|| {
124+
let mut emu = new_safari_18_5_emulator();
125+
emu.os_type = Some(BrowserEmulatorOS::IOS);
126+
Arc::new(emu)
127+
})
128+
.clone()
129+
}
130+
_ => {
131+
static EMU_MACOS: OnceLock<Arc<BrowserEmulator>> = OnceLock::new();
132+
EMU_MACOS
133+
.get_or_init(|| {
134+
let mut emu = new_safari_18_5_emulator();
135+
emu.os_type = Some(BrowserEmulatorOS::MacOS);
136+
Arc::new(emu)
137+
})
138+
.clone()
139+
}
140+
},
141+
Impersonate::SafariV26 => match browser_os {
142+
BrowserEmulatorOS::IOS => {
143+
static EMU_IOS: OnceLock<Arc<BrowserEmulator>> = OnceLock::new();
144+
EMU_IOS
145+
.get_or_init(|| {
146+
let mut emu = new_safari_26_emulator();
147+
emu.os_type = Some(BrowserEmulatorOS::IOS);
148+
Arc::new(emu)
149+
})
150+
.clone()
151+
}
152+
_ => {
153+
static EMU_MACOS: OnceLock<Arc<BrowserEmulator>> = OnceLock::new();
154+
EMU_MACOS
155+
.get_or_init(|| {
156+
let mut emu = new_safari_26_emulator();
157+
emu.os_type = Some(BrowserEmulatorOS::MacOS);
158+
Arc::new(emu)
159+
})
160+
.clone()
161+
}
162+
},
131163
Impersonate::SafariV26_3 => {
132164
if browser_os == BrowserEmulatorOS::IOS {
133165
static EMU_IOS: OnceLock<Arc<BrowserEmulator>> = OnceLock::new();

0 commit comments

Comments
 (0)