Skip to content

Commit

Permalink
Merge pull request #1395 from napi-rs/tweaks-code
Browse files Browse the repository at this point in the history
chore(napi): reduce Mutex usage while loading addon
  • Loading branch information
Brooooooklyn committed Dec 16, 2022
2 parents d253135 + e370dee commit f8d1dce
Show file tree
Hide file tree
Showing 19 changed files with 166 additions and 345 deletions.
27 changes: 0 additions & 27 deletions crates/napi/src/bindgen_runtime/js_values/arraybuffer.rs
Expand Up @@ -7,8 +7,6 @@ use std::sync::{
Arc,
};

#[cfg(feature = "napi4")]
use crate::bindgen_prelude::{CUSTOM_GC_TSFN, CUSTOM_GC_TSFN_CLOSED, MAIN_THREAD_ID};
pub use crate::js_values::TypedArrayType;
use crate::{check_status, sys, Error, Result, Status};

Expand Down Expand Up @@ -66,31 +64,6 @@ macro_rules! impl_typed_array {
fn drop(&mut self) {
if Arc::strong_count(&self.drop_in_vm) == 1 {
if let Some((ref_, env)) = self.raw {
#[cfg(feature = "napi4")]
{
if CUSTOM_GC_TSFN_CLOSED.load(std::sync::atomic::Ordering::SeqCst) {
return;
}
if !MAIN_THREAD_ID
.get()
.map(|id| &std::thread::current().id() == id)
.unwrap_or(false)
{
let status = unsafe {
sys::napi_call_threadsafe_function(
CUSTOM_GC_TSFN.load(std::sync::atomic::Ordering::SeqCst),
ref_ as *mut c_void,
1,
)
};
assert!(
status == sys::Status::napi_ok,
"Call custom GC in ArrayBuffer::drop failed {:?}",
Status::from(status)
);
return;
}
}
crate::check_status_or_throw!(
env,
unsafe { sys::napi_reference_unref(env, ref_, &mut 0) },
Expand Down
6 changes: 3 additions & 3 deletions crates/napi/src/bindgen_runtime/js_values/bigint.rs
Expand Up @@ -52,7 +52,7 @@ impl FromNapiValue for BigInt {
ptr::null_mut(),
)
})?;
let mut words: Vec<u64> = Vec::with_capacity(word_count as usize);
let mut words: Vec<u64> = Vec::with_capacity(word_count);
let mut sign_bit = 0;

unsafe {
Expand All @@ -64,7 +64,7 @@ impl FromNapiValue for BigInt {
words.as_mut_ptr(),
))?;

words.set_len(word_count as usize);
words.set_len(word_count);
}
if word_count == 0 {
words = vec![0];
Expand Down Expand Up @@ -155,7 +155,7 @@ impl ToNapiValue for BigInt {
impl ToNapiValue for i128 {
unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> crate::Result<sys::napi_value> {
let mut raw_value = ptr::null_mut();
let sign_bit = if val > 0 { 0 } else { 1 };
let sign_bit = i32::from(val <= 0);
let words = &val as *const i128 as *const u64;
check_status!(unsafe {
sys::napi_create_bigint_words(env, sign_bit, 2, words, &mut raw_value)
Expand Down
33 changes: 4 additions & 29 deletions crates/napi/src/bindgen_runtime/js_values/buffer.rs
@@ -1,17 +1,17 @@
#[cfg(debug_assertions)]
#[cfg(all(debug_assertions, not(windows)))]
use std::collections::HashSet;
use std::ffi::c_void;
use std::mem;
use std::ops::{Deref, DerefMut};
use std::ptr::{self, NonNull};
use std::slice;
use std::sync::Arc;
#[cfg(debug_assertions)]
#[cfg(all(debug_assertions, not(windows)))]
use std::sync::Mutex;

use crate::{bindgen_prelude::*, check_status, sys, Result, ValueType};

#[cfg(debug_assertions)]
#[cfg(all(debug_assertions, not(windows)))]
thread_local! {
pub (crate) static BUFFER_DATA: Mutex<HashSet<*mut u8>> = Default::default();
}
Expand All @@ -32,31 +32,6 @@ impl Drop for Buffer {
fn drop(&mut self) {
if Arc::strong_count(&self.ref_count) == 1 {
if let Some((ref_, env)) = self.raw {
#[cfg(feature = "napi4")]
{
if CUSTOM_GC_TSFN_CLOSED.load(std::sync::atomic::Ordering::SeqCst) {
return;
}
if !MAIN_THREAD_ID
.get()
.map(|id| &std::thread::current().id() == id)
.unwrap_or(false)
{
let status = unsafe {
sys::napi_call_threadsafe_function(
CUSTOM_GC_TSFN.load(std::sync::atomic::Ordering::SeqCst),
ref_ as *mut c_void,
1,
)
};
assert!(
status == sys::Status::napi_ok,
"Call custom GC in Buffer::drop failed {:?}",
Status::from(status)
);
return;
}
}
let mut ref_count = 0;
check_status_or_throw!(
env,
Expand Down Expand Up @@ -94,7 +69,7 @@ impl Clone for Buffer {
impl From<Vec<u8>> for Buffer {
fn from(mut data: Vec<u8>) -> Self {
let inner_ptr = data.as_mut_ptr();
#[cfg(debug_assertions)]
#[cfg(all(debug_assertions, not(windows)))]
{
let is_existed = BUFFER_DATA.with(|buffer_data| {
let buffer = buffer_data.lock().expect("Unlock buffer data failed");
Expand Down
2 changes: 1 addition & 1 deletion crates/napi/src/bindgen_runtime/js_values/promise.rs
Expand Up @@ -77,7 +77,7 @@ impl<T: FromNapiValue> ValidateNapiValue for Promise<T> {
}
}

unsafe impl<T: FromNapiValue> Send for Promise<T> {}
unsafe impl<T: FromNapiValue + Send> Send for Promise<T> {}

impl<T: FromNapiValue> FromNapiValue for Promise<T> {
unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> crate::Result<Self> {
Expand Down
5 changes: 3 additions & 2 deletions crates/napi/src/bindgen_runtime/js_values/value_ref.rs
Expand Up @@ -6,7 +6,7 @@ use std::rc::{Rc, Weak};
use once_cell::sync::Lazy;

use crate::{
bindgen_runtime::{PersistedSingleThreadHashMap, ToNapiValue},
bindgen_runtime::{PersistedPerInstanceHashMap, ToNapiValue},
check_status, Env, Error, Result, Status,
};

Expand All @@ -16,7 +16,7 @@ type RefInformation = (
*const Cell<*mut dyn FnOnce()>,
);

pub(crate) static REFERENCE_MAP: Lazy<PersistedSingleThreadHashMap<*mut c_void, RefInformation>> =
pub(crate) static REFERENCE_MAP: Lazy<PersistedPerInstanceHashMap<*mut c_void, RefInformation>> =
Lazy::new(Default::default);

/// ### Experimental feature
Expand Down Expand Up @@ -59,6 +59,7 @@ impl<T> Drop for Reference<T> {

impl<T: 'static> Reference<T> {
#[doc(hidden)]
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub fn add_ref(env: crate::sys::napi_env, t: *mut c_void, value: RefInformation) {
REFERENCE_MAP.borrow_mut(|map| {
if let Some((_, previous_ref, previous_rc)) = map.insert(t, value) {
Expand Down
2 changes: 1 addition & 1 deletion crates/napi/src/bindgen_runtime/mod.rs
Expand Up @@ -76,7 +76,7 @@ pub unsafe extern "C" fn drop_buffer(
#[allow(unused)] finalize_data: *mut c_void,
finalize_hint: *mut c_void,
) {
#[cfg(debug_assertions)]
#[cfg(all(debug_assertions, not(windows)))]
{
js_values::BUFFER_DATA.with(|buffer_data| {
let mut buffer = buffer_data.lock().expect("Unlock Buffer data failed");
Expand Down

0 comments on commit f8d1dce

Please sign in to comment.