fix(napi): memory issues in create_external_buffer

This commit is contained in:
LongYinan 2020-12-21 16:10:30 +08:00
parent d2b61ef01f
commit 0446e463c8
No known key found for this signature in database
GPG key ID: A3FFE134A3E20881
7 changed files with 76 additions and 10 deletions

16
bench/src/buffer.rs Normal file
View file

@ -0,0 +1,16 @@
use napi::{ContextlessResult, Env, JsBuffer, JsObject, Result};
#[contextless_function]
pub fn bench_create_buffer(env: Env) -> ContextlessResult<JsBuffer> {
let mut output = Vec::with_capacity(100000);
output.push(1);
output.push(2);
env
.create_buffer_with_data(output)
.map(|v| Some(v.into_raw()))
}
pub fn register_js(exports: &mut JsObject) -> Result<()> {
exports.create_named_method("benchCreateBuffer", bench_create_buffer)?;
Ok(())
}

View file

@ -3,7 +3,16 @@ extern crate napi_derive;
use napi::{JsObject, Result};
#[cfg(all(unix, not(target_env = "musl"), not(target_arch = "aarch64")))]
#[global_allocator]
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
#[cfg(windows)]
#[global_allocator]
static ALLOC: mimalloc::MiMalloc = mimalloc::MiMalloc;
mod async_compute;
mod buffer;
mod noop;
mod plus;
@ -12,6 +21,7 @@ fn init(mut exports: JsObject) -> Result<()> {
exports.create_named_method("noop", noop::noop)?;
async_compute::register_js(&mut exports)?;
buffer::register_js(&mut exports)?;
plus::register_js(&mut exports)?;
Ok(())