Skip to content

Commit

Permalink
Bug 1877380 - Use AtomicU32::from_ptr implementation instead of a ref…
Browse files Browse the repository at this point in the history
…erence 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
  • Loading branch information
canova committed Feb 2, 2024
1 parent cb84186 commit fc66871
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions tools/profiler/rust-api/src/profiler_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,17 @@ 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
// function to return this value. Because, calling an FFI function is much
// 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)
}

0 comments on commit fc66871

Please sign in to comment.