Skip to content

Commit

Permalink
perf: use jemalloc as global allocator (#18957)
Browse files Browse the repository at this point in the history
Follow up to #18875 that enables
`jemalloc` as a global allocator for the Deno CLI.
  • Loading branch information
bartlomieju authored and levex committed May 4, 2023
1 parent 6f36535 commit 3b36c36
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 11 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ tar = "=0.4.38"
tempfile = "3.4.0"
thiserror = "=1.0.38"
tokio = { version = "1.25.0", features = ["full"] }
tikv-jemallocator = "0.5.0"
tikv-jemalloc-sys = "0.5.3"
tokio-rustls = "0.23.3"
tokio-util = "0.7.4"
tower-lsp = { version = "=0.17.0", features = ["proposed"] }
Expand Down
3 changes: 3 additions & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ winapi = { workspace = true, features = ["knownfolders", "mswsock", "objbase", "
[target.'cfg(unix)'.dependencies]
nix.workspace = true

[target.'cfg(not(target_env = "msvc"))'.dependencies]
tikv-jemallocator.workspace = true

[dev-dependencies]
deno_bench_util.workspace = true
dotenv = "=0.15.0"
Expand Down
7 changes: 7 additions & 0 deletions cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ mod version;
mod watcher;
mod worker;

#[cfg(not(target_env = "msvc"))]
use tikv_jemallocator::Jemalloc;

#[cfg(not(target_env = "msvc"))]
#[global_allocator]
static GLOBAL: Jemalloc = Jemalloc;

use crate::args::flags_from_vec;
use crate::args::DenoSubcommand;
use crate::args::Flags;
Expand Down
3 changes: 2 additions & 1 deletion cli/napi/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ fn napi_create_async_work(
execute,
complete,
};
*result = transmute::<Box<AsyncWork>, _>(Box::new(work));
let work_box = Box::new(work);
*result = transmute::<*mut AsyncWork, _>(Box::into_raw(work_box));
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ url.workspace = true
v8.workspace = true

[target.'cfg(not(target_env = "msvc"))'.dependencies]
tikv-jemalloc-sys = "0.5"
tikv-jemalloc-sys.workspace = true

[[example]]
name = "http_bench_json_ops"
Expand Down
10 changes: 7 additions & 3 deletions test_napi/src/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ unsafe extern "C" fn complete(
ptr::null(),
&mut _result
));

assert_napi_ok!(napi_delete_reference(env, baton.func));
assert_napi_ok!(napi_delete_async_work(env, baton.task));
}
Expand All @@ -73,7 +72,7 @@ extern "C" fn test_async_work(
&mut resource_name,
));

let mut async_work: napi_async_work = ptr::null_mut();
let async_work: napi_async_work = ptr::null_mut();

let mut func: napi_ref = ptr::null_mut();
assert_napi_ok!(napi_create_reference(env, args[0], 1, &mut func));
Expand All @@ -82,16 +81,21 @@ extern "C" fn test_async_work(
func,
task: async_work,
});
let mut async_work = baton.task;
let baton_ptr = Box::into_raw(baton) as *mut c_void;

assert_napi_ok!(napi_create_async_work(
env,
ptr::null_mut(),
resource_name,
Some(execute),
Some(complete),
Box::into_raw(baton) as *mut c_void,
baton_ptr,
&mut async_work,
));
let mut baton = unsafe { Box::from_raw(baton_ptr as *mut Baton) };
baton.task = async_work;
Box::into_raw(baton);
assert_napi_ok!(napi_queue_async_work(env, async_work));

ptr::null_mut()
Expand Down
15 changes: 9 additions & 6 deletions test_napi/typedarray_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ Deno.test("napi typedarray float64", function () {
assertEquals(Math.round(10 * doubleResult[2]) / 10, -6.6);
});

Deno.test("napi typedarray external", function () {
assertEquals(
new Uint8Array(typedarray.test_external()),
new Uint8Array([0, 1, 2, 3]),
);
});
// TODO(bartlomieju): this test causes segfaults when used with jemalloc.
// Node documentation provides a hint that this function is not supported by
// other runtime like electron.
// Deno.test("napi typedarray external", function () {
// assertEquals(
// new Uint8Array(typedarray.test_external()),
// new Uint8Array([0, 1, 2, 3]),
// );
// });

0 comments on commit 3b36c36

Please sign in to comment.