Skip to content

Commit

Permalink
Avoid futex in init_typerefs()
Browse files Browse the repository at this point in the history
  • Loading branch information
ijl committed Aug 16, 2023
1 parent 6e28e49 commit 97f7ebb
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/typeref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

use crate::ffi::orjson_fragmenttype_new;
use ahash::RandomState;
use once_cell::race::OnceBox;
use once_cell::race::{OnceBool, OnceBox};
use pyo3_ffi::*;
use std::cell::UnsafeCell;
use std::mem::MaybeUninit;
use std::os::raw::c_char;
use std::ptr::{null_mut, NonNull};
use std::sync::Once;

pub struct NumpyTypes {
pub array: *mut PyTypeObject,
Expand Down Expand Up @@ -124,12 +123,18 @@ pub static mut JsonEncodeError: *mut PyObject = null_mut();
#[allow(non_upper_case_globals)]
pub static mut JsonDecodeError: *mut PyObject = null_mut();

static INIT: Once = Once::new();
static INIT: OnceBool = OnceBool::new();

#[cold]
#[cfg_attr(feature = "optimize", optimize(size))]
pub fn init_typerefs() {
INIT.call_once(|| unsafe {
INIT.get_or_init(_init_typerefs_impl);
}

#[cold]
#[cfg_attr(feature = "optimize", optimize(size))]
fn _init_typerefs_impl() -> bool {
unsafe {
assert!(crate::deserialize::KEY_MAP
.set(crate::deserialize::KeyMap::default())
.is_ok());
Expand Down Expand Up @@ -194,7 +199,8 @@ pub fn init_typerefs() {

// after all type lookups
HASH_BUILDER.get_or_init(ahash_init);
});
};
true
}

#[cold]
Expand Down

0 comments on commit 97f7ebb

Please sign in to comment.