Bug 1877380 - Use AtomicU32::from_ptr implementation instead of a reference for RacyFeatures_sActiveAndFeatures r=aabh,profiler-reviewers,glandium

This variable is an atomic variable here:
https://searchfox.org/mozilla-central/rev/2a867dd1ab015c3ef24b774a57709fb3b3dc4961/tools/profiler/core/platform.cpp#1631

We are getting this C++ atomic variable here directly instead of using FFIs
because this is much faster and this function has to be as fast as possible
(because it will be used before adding each marker to make sure the profiler is
running.). It's used by `is_active` and `can_accept_markers` functions above
this file.

It looks like Rust 1.77 starts to give some warnings/errors for these cases
where we have shared references. This should be safe for us, because we already
know that this function can be racy. So this is something we considered and
decided that it's better to be faster.

Differential Revision: https://phabricator.services.mozilla.com/D199968
This commit is contained in:
Nazım Can Altınova 2024-02-02 15:33:28 +00:00
parent cb84186b54
commit fc66871215

View File

@ -65,7 +65,6 @@ pub fn can_accept_markers() -> bool {
#[inline]
fn get_active_and_features() -> u32 {
use crate::gecko_bindings::structs::mozilla::profiler::detail;
use std::mem;
use std::sync::atomic::{AtomicU32, Ordering};
// This is reaching for the C++ atomic value instead of calling an FFI
@ -73,6 +72,10 @@ fn get_active_and_features() -> u32 {
// more expensive compared to this method. That's why it's worth to go with
// this solution for performance. But it's crucial to keep the implementation
// of this and the callers in sync with the C++ counterparts.
unsafe { mem::transmute::<_, &AtomicU32>(&detail::RacyFeatures_sActiveAndFeatures) }
.load(Ordering::Relaxed)
let active_and_features: &AtomicU32 = unsafe {
let ptr: *const u32 = std::ptr::addr_of!(detail::RacyFeatures_sActiveAndFeatures);
// TODO: Switch this to use `AtomicU32::from_ptr` once our Rust MSRV is at least 1.75.0
&*ptr.cast()
};
active_and_features.load(Ordering::Relaxed)
}