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

core: introduce serde_v8 #9722

Merged
merged 24 commits into from
Mar 26, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
serde_v8: move common key code to keys.rs
  • Loading branch information
AaronO committed Mar 26, 2021
commit 90709c319fecb09e7e5ee33a52da8b61184943e4
27 changes: 1 addition & 26 deletions serde_v8/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use rusty_v8 as v8;
use serde::de::{self, Visitor};
use serde::Deserialize;

use std::collections::HashMap;
use std::convert::TryFrom;

use crate::error::{Error, Result};
use crate::keys::{v8_struct_key, KeyCache};
use crate::payload::ValueType;

use crate::magic;
Expand All @@ -15,7 +15,6 @@ pub struct Deserializer<'a, 'b, 's> {
scope: &'b mut v8::HandleScope<'s>,
_key_cache: Option<&'b mut KeyCache>,
}
pub type KeyCache = HashMap<&'static str, v8::Global<v8::String>>;

impl<'a, 'b, 's> Deserializer<'a, 'b, 's> {
pub fn new(
Expand Down Expand Up @@ -457,30 +456,6 @@ impl<'de, 'a, 'b, 's> de::MapAccess<'de> for ObjectAccess<'a, 'b, 's> {
}
}

// creates an optimized v8::String for a struct field
// TODO: experiment with external strings
// TODO: evaluate if own KeyCache is better than v8's dedupe
fn v8_struct_key<'s>(
scope: &mut v8::HandleScope<'s>,
field: &'static str,
) -> v8::Local<'s, v8::String> {
// Internalized v8 strings are significantly faster than "normal" v8 strings
// since v8 deduplicates re-used strings minimizing new allocations
// see: https://github.com/v8/v8/blob/14ac92e02cc3db38131a57e75e2392529f405f2f/include/v8.h#L3165-L3171
v8::String::new_from_utf8(
scope,
field.as_ref(),
v8::NewStringType::Internalized,
)
.unwrap()

// TODO: consider external strings later
// right now non-deduped external strings (without KeyCache)
// are slower than the deduped internalized strings by ~2.5x
// since they're a new string in v8's eyes and needs to be hashed, etc...
// v8::String::new_external_onebyte_static(scope, field).unwrap()
}

struct SeqAccess<'a, 'b, 's> {
obj: v8::Local<'a, v8::Object>,
scope: &'b mut v8::HandleScope<'s>,
Expand Down
32 changes: 32 additions & 0 deletions serde_v8/src/keys.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use rusty_v8 as v8;

use std::collections::HashMap;

// KeyCache stores a pool struct keys mapped to v8,
// to minimize allocs and speed up decoding/encoding `v8::Object`s
// TODO: experiment with in from_v8/to_v8
pub struct KeyCache(HashMap<&'static str, v8::Global<v8::String>>);

// creates an optimized v8::String for a struct field
// TODO: experiment with external strings
// TODO: evaluate if own KeyCache is better than v8's dedupe
pub fn v8_struct_key<'s>(
scope: &mut v8::HandleScope<'s>,
field: &'static str,
) -> v8::Local<'s, v8::String> {
// Internalized v8 strings are significantly faster than "normal" v8 strings
// since v8 deduplicates re-used strings minimizing new allocations
// see: https://github.com/v8/v8/blob/14ac92e02cc3db38131a57e75e2392529f405f2f/include/v8.h#L3165-L3171
v8::String::new_from_utf8(
scope,
field.as_ref(),
v8::NewStringType::Internalized,
)
.unwrap()

// TODO: consider external strings later
// right now non-deduped external strings (without KeyCache)
// are slower than the deduped internalized strings by ~2.5x
// since they're a new string in v8's eyes and needs to be hashed, etc...
// v8::String::new_external_onebyte_static(scope, field).unwrap()
}
4 changes: 3 additions & 1 deletion serde_v8/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
mod de;
mod error;
mod keys;
mod magic;
mod payload;
mod ser;
pub mod utils;

pub use de::{from_v8, from_v8_cached, Deserializer, KeyCache};
pub use de::{from_v8, from_v8_cached, Deserializer};
pub use error::{Error, Result};
pub use keys::KeyCache;
pub use magic::Value;
pub use ser::{to_v8, Serializer};
19 changes: 1 addition & 18 deletions serde_v8/src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::cell::RefCell;
use std::rc::Rc;

use crate::error::{Error, Result};
use crate::keys::v8_struct_key;
use crate::magic;

type JsValue<'s> = v8::Local<'s, v8::Value>;
Expand Down Expand Up @@ -629,21 +630,3 @@ impl<'a, 'b> ser::Serializer for MagicTransmuter<'a, 'b> {
unreachable!();
}
}

// creates an optimized v8::String for a struct field
// TODO: experiment with external strings
// TODO: evaluate if own KeyCache is better than v8's dedupe
fn v8_struct_key<'s>(
scope: &mut v8::HandleScope<'s>,
field: &'static str,
) -> v8::Local<'s, v8::String> {
// Internalized v8 strings are significantly faster than "normal" v8 strings
// since v8 deduplicates re-used strings minimizing new allocations
// see: https://github.com/v8/v8/blob/14ac92e02cc3db38131a57e75e2392529f405f2f/include/v8.h#L3165-L3171
v8::String::new_from_utf8(
scope,
field.as_ref(),
v8::NewStringType::Internalized,
)
.unwrap()
}