Merge pull request #1235 from napi-rs/buffer-leak
fix(napi): memory leak in Buffer/ArrayBuffer
This commit is contained in:
commit
40c132aefc
6 changed files with 127 additions and 72 deletions
|
@ -2,7 +2,10 @@ use std::ffi::c_void;
|
||||||
use std::mem;
|
use std::mem;
|
||||||
use std::ops::{Deref, DerefMut};
|
use std::ops::{Deref, DerefMut};
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
use std::sync::Arc;
|
use std::sync::{
|
||||||
|
atomic::{AtomicBool, Ordering},
|
||||||
|
Arc,
|
||||||
|
};
|
||||||
|
|
||||||
pub use crate::js_values::TypedArrayType;
|
pub use crate::js_values::TypedArrayType;
|
||||||
use crate::{check_status, sys, Error, Result, Status};
|
use crate::{check_status, sys, Error, Result, Status};
|
||||||
|
@ -24,13 +27,14 @@ trait Finalizer {
|
||||||
macro_rules! impl_typed_array {
|
macro_rules! impl_typed_array {
|
||||||
($name:ident, $rust_type:ident, $typed_array_type:expr) => {
|
($name:ident, $rust_type:ident, $typed_array_type:expr) => {
|
||||||
pub struct $name {
|
pub struct $name {
|
||||||
/// Used for `clone_reference` Owned | External TypedArray
|
|
||||||
data_reference: Option<Arc<()>>,
|
|
||||||
data: *mut $rust_type,
|
data: *mut $rust_type,
|
||||||
length: usize,
|
length: usize,
|
||||||
data_managed_type: DataManagedType,
|
data_managed_type: DataManagedType,
|
||||||
byte_offset: usize,
|
byte_offset: usize,
|
||||||
raw: Option<(crate::sys::napi_ref, crate::sys::napi_env)>,
|
raw: Option<(crate::sys::napi_ref, crate::sys::napi_env)>,
|
||||||
|
// Use `Arc` for ref count
|
||||||
|
// Use `AtomicBool` for flag to indicate whether the value is dropped in VM
|
||||||
|
drop_in_vm: Arc<AtomicBool>,
|
||||||
finalizer_notify: Box<dyn FnOnce(*mut $rust_type, usize)>,
|
finalizer_notify: Box<dyn FnOnce(*mut $rust_type, usize)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,22 +56,35 @@ macro_rules! impl_typed_array {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ref_count(&self) -> usize {
|
fn ref_count(&self) -> usize {
|
||||||
if let Some(inner) = &self.data_reference {
|
Arc::strong_count(&self.drop_in_vm)
|
||||||
Arc::strong_count(inner)
|
|
||||||
} else {
|
|
||||||
0
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for $name {
|
impl Drop for $name {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
if let Some((ref_, env)) = self.raw {
|
if Arc::strong_count(&self.drop_in_vm) == 1 {
|
||||||
crate::check_status_or_throw!(
|
if let Some((ref_, env)) = self.raw {
|
||||||
env,
|
crate::check_status_or_throw!(
|
||||||
unsafe { sys::napi_reference_unref(env, ref_, &mut 0) },
|
env,
|
||||||
"Failed to delete Buffer reference in drop"
|
unsafe { sys::napi_reference_unref(env, ref_, &mut 0) },
|
||||||
);
|
"Failed to delete Buffer reference in drop"
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if !self.drop_in_vm.load(Ordering::Acquire) {
|
||||||
|
match &self.data_managed_type {
|
||||||
|
DataManagedType::Owned => {
|
||||||
|
let length = self.length;
|
||||||
|
unsafe { Vec::from_raw_parts(self.data, length, length) };
|
||||||
|
}
|
||||||
|
DataManagedType::External => {
|
||||||
|
let mut finalizer: Box<dyn FnOnce(*mut $rust_type, usize)> = Box::new(|_a, _b| {});
|
||||||
|
std::mem::swap(&mut finalizer, &mut self.finalizer_notify);
|
||||||
|
(finalizer)(self.data, self.length);
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -77,12 +94,12 @@ macro_rules! impl_typed_array {
|
||||||
|
|
||||||
pub fn new(mut data: Vec<$rust_type>) -> Self {
|
pub fn new(mut data: Vec<$rust_type>) -> Self {
|
||||||
let ret = $name {
|
let ret = $name {
|
||||||
data_reference: Some(Arc::new(())),
|
|
||||||
data: data.as_mut_ptr(),
|
data: data.as_mut_ptr(),
|
||||||
length: data.len(),
|
length: data.len(),
|
||||||
data_managed_type: DataManagedType::Owned,
|
data_managed_type: DataManagedType::Owned,
|
||||||
byte_offset: 0,
|
byte_offset: 0,
|
||||||
raw: None,
|
raw: None,
|
||||||
|
drop_in_vm: Arc::new(AtomicBool::new(false)),
|
||||||
finalizer_notify: Box::new(Self::noop_finalize),
|
finalizer_notify: Box::new(Self::noop_finalize),
|
||||||
};
|
};
|
||||||
mem::forget(data);
|
mem::forget(data);
|
||||||
|
@ -95,12 +112,12 @@ macro_rules! impl_typed_array {
|
||||||
{
|
{
|
||||||
let mut data_copied = data.as_ref().to_vec();
|
let mut data_copied = data.as_ref().to_vec();
|
||||||
let ret = $name {
|
let ret = $name {
|
||||||
data_reference: Some(Arc::new(())),
|
|
||||||
data: data_copied.as_mut_ptr(),
|
data: data_copied.as_mut_ptr(),
|
||||||
length: data.as_ref().len(),
|
length: data.as_ref().len(),
|
||||||
data_managed_type: DataManagedType::Owned,
|
data_managed_type: DataManagedType::Owned,
|
||||||
finalizer_notify: Box::new(Self::noop_finalize),
|
finalizer_notify: Box::new(Self::noop_finalize),
|
||||||
raw: None,
|
raw: None,
|
||||||
|
drop_in_vm: Arc::new(AtomicBool::new(false)),
|
||||||
byte_offset: 0,
|
byte_offset: 0,
|
||||||
};
|
};
|
||||||
mem::forget(data_copied);
|
mem::forget(data_copied);
|
||||||
|
@ -115,39 +132,29 @@ macro_rules! impl_typed_array {
|
||||||
F: 'static + FnOnce(*mut $rust_type, usize),
|
F: 'static + FnOnce(*mut $rust_type, usize),
|
||||||
{
|
{
|
||||||
$name {
|
$name {
|
||||||
data_reference: Some(Arc::new(())),
|
|
||||||
data,
|
data,
|
||||||
length,
|
length,
|
||||||
data_managed_type: DataManagedType::External,
|
data_managed_type: DataManagedType::External,
|
||||||
finalizer_notify: Box::new(notify),
|
finalizer_notify: Box::new(notify),
|
||||||
raw: None,
|
raw: None,
|
||||||
|
drop_in_vm: Arc::new(AtomicBool::new(false)),
|
||||||
byte_offset: 0,
|
byte_offset: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Clone for $name {
|
||||||
/// Clone reference, the inner data is not copied nor moved
|
/// Clone reference, the inner data is not copied nor moved
|
||||||
pub fn clone(&mut self, env: crate::Env) -> crate::Result<$name> {
|
fn clone(&self) -> $name {
|
||||||
let raw = if let Some((ref_, _)) = self.raw {
|
Self {
|
||||||
crate::check_status!(
|
|
||||||
unsafe { sys::napi_reference_ref(env.0, ref_, &mut 0) },
|
|
||||||
"Failed to ref Buffer reference in TypedArray::clone"
|
|
||||||
)?;
|
|
||||||
Some((ref_, env.0))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
|
||||||
Ok(Self {
|
|
||||||
data_reference: match self.data_managed_type {
|
|
||||||
DataManagedType::Owned | DataManagedType::External => self.data_reference.clone(),
|
|
||||||
_ => None,
|
|
||||||
},
|
|
||||||
data: self.data,
|
data: self.data,
|
||||||
length: self.length,
|
length: self.length,
|
||||||
data_managed_type: self.data_managed_type,
|
data_managed_type: self.data_managed_type,
|
||||||
finalizer_notify: Box::new(Self::noop_finalize),
|
finalizer_notify: Box::new(Self::noop_finalize),
|
||||||
raw,
|
raw: self.raw,
|
||||||
|
drop_in_vm: self.drop_in_vm.clone(),
|
||||||
byte_offset: self.byte_offset,
|
byte_offset: self.byte_offset,
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -244,11 +251,11 @@ macro_rules! impl_typed_array {
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
Ok($name {
|
Ok($name {
|
||||||
data_reference: None,
|
|
||||||
data: data as *mut $rust_type,
|
data: data as *mut $rust_type,
|
||||||
length,
|
length,
|
||||||
byte_offset,
|
byte_offset,
|
||||||
raw: Some((ref_, env)),
|
raw: Some((ref_, env)),
|
||||||
|
drop_in_vm: Arc::new(AtomicBool::new(true)),
|
||||||
data_managed_type: DataManagedType::Vm,
|
data_managed_type: DataManagedType::Vm,
|
||||||
finalizer_notify: Box::new(Self::noop_finalize),
|
finalizer_notify: Box::new(Self::noop_finalize),
|
||||||
})
|
})
|
||||||
|
@ -256,14 +263,13 @@ macro_rules! impl_typed_array {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ToNapiValue for $name {
|
impl ToNapiValue for $name {
|
||||||
unsafe fn to_napi_value(env: sys::napi_env, mut val: Self) -> Result<sys::napi_value> {
|
unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> Result<sys::napi_value> {
|
||||||
if let Some((ref_, _)) = val.raw {
|
if let Some((ref_, _)) = val.raw {
|
||||||
let mut napi_value = std::ptr::null_mut();
|
let mut napi_value = std::ptr::null_mut();
|
||||||
check_status!(
|
check_status!(
|
||||||
unsafe { sys::napi_get_reference_value(env, ref_, &mut napi_value) },
|
unsafe { sys::napi_get_reference_value(env, ref_, &mut napi_value) },
|
||||||
"Failed to delete reference from Buffer"
|
"Failed to delete reference from Buffer"
|
||||||
)?;
|
)?;
|
||||||
val.raw = None;
|
|
||||||
return Ok(napi_value);
|
return Ok(napi_value);
|
||||||
}
|
}
|
||||||
let mut arraybuffer_value = ptr::null_mut();
|
let mut arraybuffer_value = ptr::null_mut();
|
||||||
|
@ -271,6 +277,7 @@ macro_rules! impl_typed_array {
|
||||||
let length = val.length * ratio;
|
let length = val.length * ratio;
|
||||||
let val_data = val.data;
|
let val_data = val.data;
|
||||||
let val_length = val.length;
|
let val_length = val.length;
|
||||||
|
val.drop_in_vm.store(true, Ordering::Release);
|
||||||
let hint_ptr = Box::into_raw(Box::new(val));
|
let hint_ptr = Box::into_raw(Box::new(val));
|
||||||
check_status!(
|
check_status!(
|
||||||
if length == 0 {
|
if length == 0 {
|
||||||
|
@ -323,10 +330,7 @@ macro_rules! impl_typed_array {
|
||||||
Ok(napi_value)
|
Ok(napi_value)
|
||||||
} else {
|
} else {
|
||||||
let cloned_value = $name {
|
let cloned_value = $name {
|
||||||
data_reference: match val.data_managed_type {
|
drop_in_vm: val.drop_in_vm.clone(),
|
||||||
DataManagedType::Owned | DataManagedType::External => val.data_reference.clone(),
|
|
||||||
_ => None,
|
|
||||||
},
|
|
||||||
data: val.data,
|
data: val.data,
|
||||||
length: val.length,
|
length: val.length,
|
||||||
data_managed_type: val.data_managed_type,
|
data_managed_type: val.data_managed_type,
|
||||||
|
@ -355,13 +359,13 @@ unsafe extern "C" fn finalizer<Data, T: Finalizer<RustType = Data>>(
|
||||||
// do nothing
|
// do nothing
|
||||||
}
|
}
|
||||||
DataManagedType::Owned => {
|
DataManagedType::Owned => {
|
||||||
if data.ref_count() == 0 {
|
if data.ref_count() == 1 {
|
||||||
let length = length;
|
let length = length;
|
||||||
unsafe { Vec::from_raw_parts(finalize_data as *mut Data, length, length) };
|
unsafe { Vec::from_raw_parts(finalize_data as *mut Data, length, length) };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
DataManagedType::External => {
|
DataManagedType::External => {
|
||||||
if data.ref_count() == 0 {
|
if data.ref_count() == 1 {
|
||||||
(finalizer_notify)(finalize_data as *mut Data, length);
|
(finalizer_notify)(finalize_data as *mut Data, length);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ use std::mem;
|
||||||
use std::ops::{Deref, DerefMut};
|
use std::ops::{Deref, DerefMut};
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
use std::slice;
|
use std::slice;
|
||||||
|
use std::sync::atomic::{AtomicBool, Ordering};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
|
@ -21,20 +22,33 @@ thread_local! {
|
||||||
/// So it is safe to use it in `async fn`, the `&[u8]` under the hood will not be dropped until the `drop` called.
|
/// So it is safe to use it in `async fn`, the `&[u8]` under the hood will not be dropped until the `drop` called.
|
||||||
/// Clone will create a new `Reference` to the same underlying `JavaScript Buffer`.
|
/// Clone will create a new `Reference` to the same underlying `JavaScript Buffer`.
|
||||||
pub struct Buffer {
|
pub struct Buffer {
|
||||||
pub(crate) data_reference: Option<Arc<()>>,
|
|
||||||
pub(crate) inner: &'static mut [u8],
|
pub(crate) inner: &'static mut [u8],
|
||||||
pub(crate) capacity: usize,
|
pub(crate) capacity: usize,
|
||||||
raw: Option<(sys::napi_ref, sys::napi_env)>,
|
raw: Option<(sys::napi_ref, sys::napi_env)>,
|
||||||
|
// use it as ref count
|
||||||
|
pub(crate) drop_in_vm: Arc<AtomicBool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for Buffer {
|
impl Drop for Buffer {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
if let Some((ref_, env)) = self.raw {
|
if Arc::strong_count(&self.drop_in_vm) == 1 {
|
||||||
check_status_or_throw!(
|
if let Some((ref_, env)) = self.raw {
|
||||||
env,
|
check_status_or_throw!(
|
||||||
unsafe { sys::napi_reference_unref(env, ref_, &mut 0) },
|
env,
|
||||||
"Failed to delete Buffer reference in drop"
|
unsafe { sys::napi_reference_unref(env, ref_, &mut 0) },
|
||||||
);
|
"Failed to unref Buffer reference in drop"
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// Drop in Rust side
|
||||||
|
// ```rust
|
||||||
|
// #[napi]
|
||||||
|
// fn buffer_len() -> u32 {
|
||||||
|
// Buffer::from(vec![1, 2, 3]).len() as u32
|
||||||
|
// }
|
||||||
|
if !self.drop_in_vm.load(Ordering::Acquire) {
|
||||||
|
unsafe { Vec::from_raw_parts(self.inner.as_mut_ptr(), self.inner.len(), self.capacity) };
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -53,10 +67,10 @@ impl Buffer {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
data_reference: self.data_reference.clone(),
|
|
||||||
inner: unsafe { slice::from_raw_parts_mut(self.inner.as_mut_ptr(), self.inner.len()) },
|
inner: unsafe { slice::from_raw_parts_mut(self.inner.as_mut_ptr(), self.inner.len()) },
|
||||||
capacity: self.capacity,
|
capacity: self.capacity,
|
||||||
raw,
|
raw,
|
||||||
|
drop_in_vm: self.drop_in_vm.clone(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -78,10 +92,10 @@ impl From<Vec<u8>> for Buffer {
|
||||||
let capacity = data.capacity();
|
let capacity = data.capacity();
|
||||||
mem::forget(data);
|
mem::forget(data);
|
||||||
Buffer {
|
Buffer {
|
||||||
data_reference: Some(Arc::new(())),
|
|
||||||
inner: unsafe { slice::from_raw_parts_mut(inner_ptr, len) },
|
inner: unsafe { slice::from_raw_parts_mut(inner_ptr, len) },
|
||||||
capacity,
|
capacity,
|
||||||
raw: None,
|
raw: None,
|
||||||
|
drop_in_vm: Arc::new(AtomicBool::new(false)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -149,16 +163,16 @@ impl FromNapiValue for Buffer {
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
data_reference: None,
|
|
||||||
inner: unsafe { slice::from_raw_parts_mut(buf as *mut _, len) },
|
inner: unsafe { slice::from_raw_parts_mut(buf as *mut _, len) },
|
||||||
capacity: len,
|
capacity: len,
|
||||||
raw: Some((ref_, env)),
|
raw: Some((ref_, env)),
|
||||||
|
drop_in_vm: Arc::new(AtomicBool::new(true)),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ToNapiValue for Buffer {
|
impl ToNapiValue for Buffer {
|
||||||
unsafe fn to_napi_value(env: sys::napi_env, mut val: Self) -> Result<sys::napi_value> {
|
unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> Result<sys::napi_value> {
|
||||||
// From Node.js value, not from `Vec<u8>`
|
// From Node.js value, not from `Vec<u8>`
|
||||||
if let Some((ref_, _)) = val.raw {
|
if let Some((ref_, _)) = val.raw {
|
||||||
let mut buf = ptr::null_mut();
|
let mut buf = ptr::null_mut();
|
||||||
|
@ -166,14 +180,10 @@ impl ToNapiValue for Buffer {
|
||||||
unsafe { sys::napi_get_reference_value(env, ref_, &mut buf) },
|
unsafe { sys::napi_get_reference_value(env, ref_, &mut buf) },
|
||||||
"Failed to get Buffer value from reference"
|
"Failed to get Buffer value from reference"
|
||||||
)?;
|
)?;
|
||||||
check_status!(
|
|
||||||
unsafe { sys::napi_delete_reference(env, ref_) },
|
|
||||||
"Failed to delete Buffer reference"
|
|
||||||
)?;
|
|
||||||
val.raw = None; // Prevent double free
|
|
||||||
return Ok(buf);
|
return Ok(buf);
|
||||||
}
|
}
|
||||||
let len = val.inner.len();
|
let len = val.inner.len();
|
||||||
|
val.drop_in_vm.store(true, Ordering::Release);
|
||||||
let mut ret = ptr::null_mut();
|
let mut ret = ptr::null_mut();
|
||||||
check_status!(
|
check_status!(
|
||||||
if len == 0 {
|
if len == 0 {
|
||||||
|
@ -210,17 +220,13 @@ impl ToNapiValue for &mut Buffer {
|
||||||
unsafe { sys::napi_get_reference_value(env, ref_, &mut buf) },
|
unsafe { sys::napi_get_reference_value(env, ref_, &mut buf) },
|
||||||
"Failed to get Buffer value from reference"
|
"Failed to get Buffer value from reference"
|
||||||
)?;
|
)?;
|
||||||
check_status!(
|
|
||||||
unsafe { sys::napi_delete_reference(env, ref_) },
|
|
||||||
"Failed to delete Buffer reference"
|
|
||||||
)?;
|
|
||||||
Ok(buf)
|
Ok(buf)
|
||||||
} else {
|
} else {
|
||||||
let buf = Buffer {
|
let buf = Buffer {
|
||||||
data_reference: val.data_reference.clone(),
|
|
||||||
inner: unsafe { slice::from_raw_parts_mut(val.inner.as_mut_ptr(), val.capacity) },
|
inner: unsafe { slice::from_raw_parts_mut(val.inner.as_mut_ptr(), val.capacity) },
|
||||||
capacity: val.capacity,
|
capacity: val.capacity,
|
||||||
raw: None,
|
raw: None,
|
||||||
|
drop_in_vm: Arc::new(AtomicBool::new(true)),
|
||||||
};
|
};
|
||||||
unsafe { ToNapiValue::to_napi_value(env, buf) }
|
unsafe { ToNapiValue::to_napi_value(env, buf) }
|
||||||
}
|
}
|
||||||
|
|
|
@ -76,14 +76,12 @@ pub unsafe extern "C" fn drop_buffer(
|
||||||
}
|
}
|
||||||
unsafe {
|
unsafe {
|
||||||
let buf = Box::from_raw(finalize_hint as *mut Buffer);
|
let buf = Box::from_raw(finalize_hint as *mut Buffer);
|
||||||
if let Some(data_reference) = buf.data_reference.as_ref() {
|
if Arc::strong_count(&buf.drop_in_vm) == 1 {
|
||||||
if Arc::strong_count(data_reference) == 0 {
|
mem::drop(Vec::from_raw_parts(
|
||||||
mem::drop(Vec::from_raw_parts(
|
finalize_data as *mut u8,
|
||||||
finalize_data as *mut u8,
|
buf.inner.len(),
|
||||||
buf.inner.len(),
|
buf.capacity,
|
||||||
buf.capacity,
|
));
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
26
memory-testing/buffer.mjs
Normal file
26
memory-testing/buffer.mjs
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
import { createRequire } from 'module'
|
||||||
|
import { setTimeout } from 'timers/promises'
|
||||||
|
|
||||||
|
import { displayMemoryUsageFromNode } from './util.mjs'
|
||||||
|
|
||||||
|
const initialMemoryUsage = process.memoryUsage()
|
||||||
|
|
||||||
|
const require = createRequire(import.meta.url)
|
||||||
|
|
||||||
|
const api = require(`./index.node`)
|
||||||
|
|
||||||
|
let i = 1
|
||||||
|
// eslint-disable-next-line no-constant-condition
|
||||||
|
while (true) {
|
||||||
|
api.bufferLen()
|
||||||
|
api.arrayBufferLen()
|
||||||
|
api.bufferConvert(Buffer.from(Array.from({ length: 1024 * 10240 }).fill(1)))
|
||||||
|
api.arrayBufferConvert(
|
||||||
|
Uint8Array.from(Array.from({ length: 1024 * 10240 }).fill(1)),
|
||||||
|
)
|
||||||
|
if (i % 10 === 0) {
|
||||||
|
await setTimeout(100)
|
||||||
|
displayMemoryUsageFromNode(initialMemoryUsage)
|
||||||
|
}
|
||||||
|
i++
|
||||||
|
}
|
|
@ -4,3 +4,4 @@ await createSuite('reference')
|
||||||
await createSuite('tokio-future')
|
await createSuite('tokio-future')
|
||||||
await createSuite('serde')
|
await createSuite('serde')
|
||||||
await createSuite('tsfn')
|
await createSuite('tsfn')
|
||||||
|
await createSuite('buffer')
|
||||||
|
|
|
@ -141,3 +141,23 @@ pub fn leaking_func(env: Env, func: JsFunction) -> napi::Result<()> {
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[napi]
|
||||||
|
pub fn buffer_convert(buffer: Buffer) -> Buffer {
|
||||||
|
Buffer::from(vec![0; buffer.len()])
|
||||||
|
}
|
||||||
|
|
||||||
|
#[napi]
|
||||||
|
pub fn buffer_len() -> u32 {
|
||||||
|
Buffer::from(vec![0; 1024 * 10240]).len() as u32
|
||||||
|
}
|
||||||
|
|
||||||
|
#[napi]
|
||||||
|
pub fn array_buffer_convert(array_buffer: Uint8Array) -> Uint8Array {
|
||||||
|
Uint8Array::new(vec![1; array_buffer.len()])
|
||||||
|
}
|
||||||
|
|
||||||
|
#[napi]
|
||||||
|
pub fn array_buffer_len() -> u32 {
|
||||||
|
Uint8Array::new(vec![1; 1024 * 10240]).len() as u32
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue