From 936e0fda9528a00faacab6fe064d4db112f4e9a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=AE=87=E9=80=B8?= Date: Mon, 22 Jan 2024 22:46:50 +0800 Subject: [PATCH] Wrap `GetProcessHeap` and make it never inline --- library/std/src/sys/pal/windows/alloc.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/library/std/src/sys/pal/windows/alloc.rs b/library/std/src/sys/pal/windows/alloc.rs index d53ea16005fab..5abe8361cdaf7 100644 --- a/library/std/src/sys/pal/windows/alloc.rs +++ b/library/std/src/sys/pal/windows/alloc.rs @@ -94,9 +94,10 @@ static HEAP: AtomicPtr = AtomicPtr::new(ptr::null_mut()); // a non-null handle returned by `GetProcessHeap`. #[inline] fn init_or_get_process_heap() -> c::HANDLE { - let heap = HEAP.load(Ordering::Relaxed); - if heap.is_null() { - // `HEAP` has not yet been successfully initialized + // The method is marked never inline to shrink the binary size. It won't be called many times. + #[cold] + #[inline(never)] + fn init_process_heap() -> c::HANDLE { let heap = unsafe { GetProcessHeap() }; if !heap.is_null() { // SAFETY: No locking is needed because within the same process, @@ -109,6 +110,11 @@ fn init_or_get_process_heap() -> c::HANDLE { // Could not get the current process heap. ptr::null_mut() } + } + + let heap = HEAP.load(Ordering::Relaxed); + if heap.is_null() { + init_process_heap() } else { // SAFETY: `HEAP` contains a non-null handle returned by `GetProcessHeap` heap