perf(es): Add fast memory deallocator (#3910)

This commit is contained in:
Donny/강동윤 2022-03-08 07:36:06 +09:00 committed by GitHub
parent d72a2f32af
commit f3da3499c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 135 additions and 1 deletions

8
Cargo.lock generated
View File

@ -773,6 +773,13 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7"
[[package]]
name = "fastmem"
version = "0.1.0"
dependencies = [
"rayon",
]
[[package]]
name = "fixedbitset"
version = "0.2.0"
@ -3574,6 +3581,7 @@ dependencies = [
name = "swc_node_base"
version = "0.5.1"
dependencies = [
"fastmem",
"mimalloc-rust",
]

14
crates/fastmem/Cargo.toml Normal file
View File

@ -0,0 +1,14 @@
[package]
authors = ["강동윤 <kdy1997.dev@gmail.com>"]
description = "Configurable utilities for fast memory operations"
edition = "2021"
license = "Apache-2.0"
name = "fastmem"
repository = "https://github.com/swc-project/swc.git"
version = "0.1.0"
[features]
enable = ["rayon"]
[dependencies]
rayon = {version = "1", optional = true}

View File

@ -0,0 +1,97 @@
use std::ops::{Deref, DerefMut};
pub fn drop_fast<T>(v: T)
where
T: Send + 'static,
{
FastDrop::new(v);
}
/// Utility type to deallocate memory in another thread.
///
///
///
/// If ths feature `enable` is turned on, the value will be deallocated in other
/// thread.
///
/// This type uses [rayon::spawn]
#[repr(transparent)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct FastDrop<T>
where
T: Send + 'static,
{
value: Option<T>,
}
impl<T> Default for FastDrop<T>
where
T: Send + 'static,
T: Default,
{
#[inline(always)]
fn default() -> Self {
Self::new(T::default())
}
}
impl<T> FastDrop<T>
where
T: Send,
{
#[inline(always)]
pub fn new(value: T) -> Self {
let value = Some(value);
Self { value }
}
}
impl<T> From<T> for FastDrop<T>
where
T: Send,
{
#[inline(always)]
fn from(value: T) -> Self {
Self::new(value)
}
}
impl<T> Deref for FastDrop<T>
where
T: Send,
{
type Target = T;
#[inline(always)]
fn deref(&self) -> &Self::Target {
debug_assert!(self.value.is_some());
// Safety: self.value can be None only if drop is called.
unsafe { self.value.as_ref().unwrap_unchecked() }
}
}
impl<T> DerefMut for FastDrop<T>
where
T: Send,
{
#[inline(always)]
fn deref_mut(&mut self) -> &mut Self::Target {
debug_assert!(self.value.is_some());
// Safety: self.value can be None only if drop is called.
unsafe { self.value.as_mut().unwrap_unchecked() }
}
}
impl<T> Drop for FastDrop<T>
where
T: Send,
{
fn drop(&mut self) {
#[cfg(feature = "enable")]
if let Some(value) = self.value.take() {
rayon::spawn(move || {
std::mem::drop(value);
});
}
}
}

14
crates/fastmem/src/lib.rs Normal file
View File

@ -0,0 +1,14 @@
//! Utilities to unblock main thread.
//!
//! # Cargo Features
//!
//! ## `enable`
//!
//! This enables fast-mode of this crate.
#![deny(clippy::all)]
#![deny(unused)]
pub use self::fast_drop::*;
mod fast_drop;

View File

@ -1,4 +1,4 @@
#!/usr/bin/env bash
set -eu
cargo profile instruments --release -t time --features tracing/release_max_level_info --features swc_common/concurrent --features swc_common/parking_lot --bench parser -- --bench
cargo profile instruments --release -t time --features tracing/release_max_level_info --features swc_common/concurrent --features swc_common/parking_lot --bench parser -- --bench

View File

@ -11,6 +11,7 @@ version = "0.5.1"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
fastmem = {version = "0.1.0", path = "../fastmem", features = ["enable"]}
[target.'cfg(not(all(target_os = "linux", target_arch = "aarch64", target_env = "musl")))'.dependencies]
mimalloc-rust = "=0.1.5"