mirror of
https://github.com/servo/servo.git
synced 2025-08-05 05:30:08 +01:00
script: const initialize LIVE_REFERENCES (#37688)
With Rust 1.85 it is possible to const initialize Hashmaps if the hash algorithm does not rely on a random seed. Testing: No functional changes, covered by existing tests Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com>
This commit is contained in:
parent
d970584332
commit
50cf01cf3d
2 changed files with 19 additions and 25 deletions
|
@ -47,9 +47,16 @@ mod dummy {
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
use fnv::{FnvBuildHasher, FnvHashMap};
|
||||||
|
|
||||||
use super::LiveDOMReferences;
|
use super::LiveDOMReferences;
|
||||||
thread_local!(pub(crate) static LIVE_REFERENCES: Rc<RefCell<Option<LiveDOMReferences>>> =
|
thread_local!(pub(crate) static LIVE_REFERENCES: Rc<RefCell<LiveDOMReferences>> =
|
||||||
Rc::new(RefCell::new(None)));
|
Rc::new(RefCell::new(
|
||||||
|
LiveDOMReferences {
|
||||||
|
reflectable_table: RefCell::new(FnvHashMap::with_hasher(FnvBuildHasher::new())),
|
||||||
|
promise_table: RefCell::new(FnvHashMap::with_hasher(FnvBuildHasher::new())),
|
||||||
|
}
|
||||||
|
)));
|
||||||
}
|
}
|
||||||
pub(crate) use self::dummy::LIVE_REFERENCES;
|
pub(crate) use self::dummy::LIVE_REFERENCES;
|
||||||
|
|
||||||
|
@ -87,9 +94,8 @@ impl TrustedPromise {
|
||||||
#[cfg_attr(crown, allow(crown::unrooted_must_root))]
|
#[cfg_attr(crown, allow(crown::unrooted_must_root))]
|
||||||
pub(crate) fn new(promise: Rc<Promise>) -> TrustedPromise {
|
pub(crate) fn new(promise: Rc<Promise>) -> TrustedPromise {
|
||||||
LIVE_REFERENCES.with(|r| {
|
LIVE_REFERENCES.with(|r| {
|
||||||
let r = r.borrow();
|
let live_references = &*r.borrow();
|
||||||
let live_references = r.as_ref().unwrap();
|
let ptr = &raw const *promise;
|
||||||
let ptr = &*promise as *const Promise;
|
|
||||||
live_references.addref_promise(promise);
|
live_references.addref_promise(promise);
|
||||||
TrustedPromise {
|
TrustedPromise {
|
||||||
dom_object: ptr,
|
dom_object: ptr,
|
||||||
|
@ -103,11 +109,10 @@ impl TrustedPromise {
|
||||||
/// obtained.
|
/// obtained.
|
||||||
pub(crate) fn root(self) -> Rc<Promise> {
|
pub(crate) fn root(self) -> Rc<Promise> {
|
||||||
LIVE_REFERENCES.with(|r| {
|
LIVE_REFERENCES.with(|r| {
|
||||||
let r = r.borrow();
|
let live_references = &*r.borrow();
|
||||||
let live_references = r.as_ref().unwrap();
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
self.owner_thread,
|
self.owner_thread,
|
||||||
(live_references) as *const _ as *const libc::c_void
|
live_references as *const _ as *const libc::c_void
|
||||||
);
|
);
|
||||||
// Borrow-check error requires the redundant `let promise = ...; promise` here.
|
// Borrow-check error requires the redundant `let promise = ...; promise` here.
|
||||||
let promise = match live_references
|
let promise = match live_references
|
||||||
|
@ -184,8 +189,7 @@ impl<T: DomObject> Trusted<T> {
|
||||||
ptr: *const libc::c_void,
|
ptr: *const libc::c_void,
|
||||||
) -> (Arc<TrustedReference>, *const LiveDOMReferences) {
|
) -> (Arc<TrustedReference>, *const LiveDOMReferences) {
|
||||||
LIVE_REFERENCES.with(|r| {
|
LIVE_REFERENCES.with(|r| {
|
||||||
let r = r.borrow();
|
let live_references = &*r.borrow();
|
||||||
let live_references = r.as_ref().unwrap();
|
|
||||||
let refcount = unsafe { live_references.addref(ptr) };
|
let refcount = unsafe { live_references.addref(ptr) };
|
||||||
(refcount, live_references as *const _)
|
(refcount, live_references as *const _)
|
||||||
})
|
})
|
||||||
|
@ -206,7 +210,7 @@ impl<T: DomObject> Trusted<T> {
|
||||||
fn validate(owner_thread: *const LiveDOMReferences) {
|
fn validate(owner_thread: *const LiveDOMReferences) {
|
||||||
assert!(LIVE_REFERENCES.with(|r| {
|
assert!(LIVE_REFERENCES.with(|r| {
|
||||||
let r = r.borrow();
|
let r = r.borrow();
|
||||||
let live_references = r.as_ref().unwrap();
|
let live_references = &*r;
|
||||||
owner_thread == live_references
|
owner_thread == live_references
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
@ -235,19 +239,11 @@ pub(crate) struct LiveDOMReferences {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LiveDOMReferences {
|
impl LiveDOMReferences {
|
||||||
/// Set up the thread-local data required for storing the outstanding DOM references.
|
|
||||||
pub(crate) fn initialize() {
|
|
||||||
LIVE_REFERENCES.with(|r| {
|
|
||||||
*r.borrow_mut() = Some(LiveDOMReferences {
|
|
||||||
reflectable_table: RefCell::new(FnvHashMap::default()),
|
|
||||||
promise_table: RefCell::new(FnvHashMap::default()),
|
|
||||||
})
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn destruct() {
|
pub(crate) fn destruct() {
|
||||||
LIVE_REFERENCES.with(|r| {
|
LIVE_REFERENCES.with(|r| {
|
||||||
*r.borrow_mut() = None;
|
let live_references = r.borrow_mut();
|
||||||
|
let _ = live_references.promise_table.take();
|
||||||
|
let _ = live_references.reflectable_table.take();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -306,8 +302,7 @@ fn remove_nulls<K: Eq + Hash + Clone, V>(table: &mut FnvHashMap<K, Weak<V>>) {
|
||||||
pub(crate) unsafe fn trace_refcounted_objects(tracer: *mut JSTracer) {
|
pub(crate) unsafe fn trace_refcounted_objects(tracer: *mut JSTracer) {
|
||||||
trace!("tracing live refcounted references");
|
trace!("tracing live refcounted references");
|
||||||
LIVE_REFERENCES.with(|r| {
|
LIVE_REFERENCES.with(|r| {
|
||||||
let r = r.borrow();
|
let live_references = &*r.borrow();
|
||||||
let live_references = r.as_ref().unwrap();
|
|
||||||
{
|
{
|
||||||
let mut table = live_references.reflectable_table.borrow_mut();
|
let mut table = live_references.reflectable_table.borrow_mut();
|
||||||
remove_nulls(&mut table);
|
remove_nulls(&mut table);
|
||||||
|
|
|
@ -511,7 +511,6 @@ impl Runtime {
|
||||||
parent: Option<ParentRuntime>,
|
parent: Option<ParentRuntime>,
|
||||||
networking_task_source: Option<SendableTaskSource>,
|
networking_task_source: Option<SendableTaskSource>,
|
||||||
) -> Runtime {
|
) -> Runtime {
|
||||||
LiveDOMReferences::initialize();
|
|
||||||
let (cx, runtime) = if let Some(parent) = parent {
|
let (cx, runtime) = if let Some(parent) = parent {
|
||||||
let runtime = RustRuntime::create_with_parent(parent);
|
let runtime = RustRuntime::create_with_parent(parent);
|
||||||
let cx = runtime.cx();
|
let cx = runtime.cx();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue