Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(napi): optimize primitive napi functions #16163

Merged
merged 3 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
perf(napi): optimize primitive functions
  • Loading branch information
littledivy committed Oct 5, 2022
commit 3b612963942d77b8ba67f89aa243319fa894c8b0
2 changes: 0 additions & 2 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 @@ -159,6 +159,8 @@ opt-level = 3
opt-level = 3
[profile.release.package.deno_napi]
opt-level = 3
[profile.release.package.test_napi]
opt-level = 3
[profile.release.package.num-bigint-dig]
opt-level = 3
[profile.release.package.v8]
Expand Down
6 changes: 6 additions & 0 deletions cli/bench/napi/bench.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { loadTestLibrary } from "../../../test_napi/common.js";

const lib = loadTestLibrary();

Deno.bench("warmup", () => {});
Deno.bench("napi_get_undefined", () => lib.test_get_undefined(0));
10 changes: 10 additions & 0 deletions cli/bench/napi/bench_node.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { run, bench } from "mitata";
import { createRequire } from "module";

const require = createRequire(import.meta.url);
const lib = require("../../../test_napi.node");

bench("warmup", () => {});
bench("napi_get_undefined", () => lib.test_get_undefined(0));

run();
17 changes: 9 additions & 8 deletions cli/napi/js_native_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1324,9 +1324,9 @@ fn napi_get_boolean(
result: *mut napi_value,
) -> Result {
let env: &mut Env = env.as_mut().ok_or(Error::InvalidArg)?;
let value: v8::Local<v8::Value> =
v8::Boolean::new(&mut env.scope(), value).into();
*result = transmute::<v8::Local<v8::Value>, napi_value>(value);
*result = transmute::<v8::Local<v8::Value>, napi_value>(
v8::Boolean::new(env.isolate(), value).into(),
);
Ok(())
}

Expand Down Expand Up @@ -1511,9 +1511,9 @@ fn napi_get_new_target(
#[napi_sym::napi_sym]
fn napi_get_null(env: *mut Env, result: *mut napi_value) -> Result {
let env: &mut Env = env.as_mut().ok_or(Error::InvalidArg)?;

let value: v8::Local<v8::Value> = v8::null(&mut env.scope()).into();
*result = transmute::<v8::Local<v8::Value>, napi_value>(value);
*result = transmute::<v8::Local<v8::Value>, napi_value>(
v8::null(env.isolate()).into(),
);
Ok(())
}

Expand Down Expand Up @@ -1603,8 +1603,9 @@ fn napi_get_typedarray_info(
#[napi_sym::napi_sym]
fn napi_get_undefined(env: *mut Env, result: *mut napi_value) -> Result {
let env: &mut Env = env.as_mut().ok_or(Error::InvalidArg)?;
let value: v8::Local<v8::Value> = v8::undefined(&mut env.scope()).into();
*result = transmute::<v8::Local<v8::Value>, napi_value>(value);
*result = transmute::<v8::Local<v8::Value>, napi_value>(
v8::undefined(env.isolate()).into(),
);
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ serde_json = { version = "1.0.79", features = ["preserve_order"] }
serde_v8 = { version = "0.64.0", path = "../serde_v8" }
sourcemap = "6.1"
url = { version = "2.3.1", features = ["serde", "expose_internals"] }
v8 = { version = "0.51.0", default-features = false }
v8 = { path = "../../rusty_v8", version = "0.51.0", default-features = false }

[[example]]
name = "http_bench_json_ops"
Expand Down
2 changes: 1 addition & 1 deletion serde_v8/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ derive_more = "0.99.17"
serde = { version = "1.0.136", features = ["derive"] }
serde_bytes = "0.11"
smallvec = { version = "1.8", features = ["union"] }
v8 = { version = "0.51.0", default-features = false }
v8 = { path = "../../rusty_v8", version = "0.51.0", default-features = false }

[dev-dependencies]
bencher = "0.1"
Expand Down
16 changes: 11 additions & 5 deletions serde_v8/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use serde::ser;
use serde::ser::Serialize;

use std::cell::RefCell;
use std::ops::DerefMut;

use crate::error::{Error, Result};
use crate::keys::v8_struct_key;
Expand Down Expand Up @@ -435,11 +436,13 @@ impl<'a, 'b, 'c> ser::Serializer for Serializer<'a, 'b, 'c> {
}

fn serialize_f64(self, v: f64) -> JsResult<'a> {
Ok(v8::Number::new(&mut self.scope.borrow_mut(), v).into())
let scope = &mut self.scope.borrow_mut();
Ok(v8::Number::new(scope.deref_mut(), v).into())
}

fn serialize_bool(self, v: bool) -> JsResult<'a> {
Ok(v8::Boolean::new(&mut self.scope.borrow_mut(), v).into())
let scope = &mut self.scope.borrow_mut();
Ok(v8::Boolean::new(scope.deref_mut(), v).into())
}

fn serialize_char(self, v: char) -> JsResult<'a> {
Expand All @@ -464,19 +467,22 @@ impl<'a, 'b, 'c> ser::Serializer for Serializer<'a, 'b, 'c> {
}

fn serialize_none(self) -> JsResult<'a> {
Ok(v8::null(&mut self.scope.borrow_mut()).into())
let scope = &mut self.scope.borrow_mut();
Ok(v8::null(scope.deref_mut()).into())
}

fn serialize_some<T: ?Sized + Serialize>(self, value: &T) -> JsResult<'a> {
value.serialize(self)
}

fn serialize_unit(self) -> JsResult<'a> {
Ok(v8::null(&mut self.scope.borrow_mut()).into())
let scope = &mut self.scope.borrow_mut();
Ok(v8::null(scope.deref_mut()).into())
}

fn serialize_unit_struct(self, _name: &'static str) -> JsResult<'a> {
Ok(v8::null(&mut self.scope.borrow_mut()).into())
let scope = &mut self.scope.borrow_mut();
Ok(v8::null(scope.deref_mut()).into())
}

/// For compatibility with serde-json, serialises unit variants as "Variant" strings.
Expand Down
2 changes: 2 additions & 0 deletions test_napi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod callback;
pub mod coerce;
pub mod numbers;
pub mod object_wrap;
pub mod primitives;
pub mod promise;
pub mod properties;
pub mod strings;
Expand Down Expand Up @@ -67,6 +68,7 @@ unsafe extern "C" fn napi_register_module_v1(
numbers::init(env, exports);
typedarray::init(env, exports);
array::init(env, exports);
primitives::init(env, exports);
properties::init(env, exports);
promise::init(env, exports);
coerce::init(env, exports);
Expand Down
23 changes: 23 additions & 0 deletions test_napi/src/primitives.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use napi_sys::*;
use std::ptr;

extern "C" fn test_get_undefined(
env: napi_env,
_: napi_callback_info,
) -> napi_value {
let mut result = ptr::null_mut();
unsafe { napi_get_undefined(env, &mut result) };
result
}

pub fn init(env: napi_env, exports: napi_value) {
let properties = &[crate::new_property!(
env,
"test_get_undefined\0",
test_get_undefined
)];

unsafe {
napi_define_properties(env, exports, properties.len(), properties.as_ptr())
};
}