Skip to content

Commit

Permalink
Add msgpack to comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
RReverser committed Dec 29, 2022
1 parent 0d3f579 commit d8e4f71
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 3 deletions.
29 changes: 29 additions & 0 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion benchmarks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ publish = false

[dependencies]
console_error_panic_hook = "0.1.6"
rmp-serde = { version = "1.1.1", optional = true }
serde = { version = "^1.0", features = ["derive"] }
serde-wasm-bindgen = { path = "..", optional = true }
wasm-bindgen = { version = "^0.2" }
Expand All @@ -15,8 +16,9 @@ wasm-bindgen = { version = "^0.2" }
crate-type = ["cdylib", "rlib"]

[features]
default = ["serde-json", "serde-wasm-bindgen"]
default = ["serde-json", "serde-wasm-bindgen", "msgpack"]
serde-json = ["wasm-bindgen/serde-serialize"]
msgpack = ["rmp-serde"]

[package.metadata.wasm-pack.profile.profiling]
wasm-opt = ['-O', '-g']
2 changes: 1 addition & 1 deletion benchmarks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let suites = {
for (let input of ['canada', 'citm_catalog', 'twitter']) {
const json = require(`./data/${input}.json`);

for (const lib of ['serde_json', 'serde_wasm_bindgen']) {
for (const lib of ['serde_json', 'serde_wasm_bindgen', 'msgpack']) {
const parse = benches[`parse_${input}_with_${lib}`];
suites.parse.add(`${input} x ${lib}`, () => parse(json).free());

Expand Down
43 changes: 42 additions & 1 deletion benchmarks/package-lock.json

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

1 change: 1 addition & 0 deletions benchmarks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "@serde-wasm-bindgen/benches",
"private": true,
"dependencies": {
"@msgpack/msgpack": "^2.8.0",
"benchmark": "^2.1.4"
},
"scripts": {
Expand Down
58 changes: 58 additions & 0 deletions benchmarks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,28 @@ fn serde_wasm_bindgen_to_value(
value.serialize(&serializer)
}

#[cfg(feature = "msgpack")]
mod msgpack_js {
use super::*;
use serde::de::DeserializeOwned;

#[wasm_bindgen(module = "@msgpack/msgpack")]
extern "C" {
fn encode(input: &JsValue) -> Vec<u8>;
fn decode(input: &[u8]) -> JsValue;
}

pub fn parse<T: DeserializeOwned>(input: JsValue) -> Result<T, rmp_serde::decode::Error> {
let input = encode(&input);
rmp_serde::from_slice(&input)
}

pub fn serialize<T: Serialize>(input: &T) -> Result<JsValue, rmp_serde::encode::Error> {
let input = rmp_serde::to_vec(input)?;
Ok(decode(&input))
}
}

#[cfg(feature = "serde-wasm-bindgen")]
#[wasm_bindgen]
pub fn parse_canada_with_serde_wasm_bindgen(input: JsValue) -> Canada {
Expand All @@ -47,6 +69,12 @@ pub fn parse_canada_with_serde_json(input: JsValue) -> Canada {
input.into_serde().unwrap()
}

#[cfg(feature = "msgpack")]
#[wasm_bindgen]
pub fn parse_canada_with_msgpack(input: JsValue) -> Canada {
msgpack_js::parse(input).unwrap()
}

#[cfg(feature = "serde-wasm-bindgen")]
#[wasm_bindgen]
pub fn serialize_canada_with_serde_wasm_bindgen(input: &Canada) -> JsValue {
Expand All @@ -60,6 +88,12 @@ pub fn serialize_canada_with_serde_json(input: &Canada) -> JsValue {
JsValue::from_serde(input).unwrap()
}

#[cfg(feature = "msgpack")]
#[wasm_bindgen]
pub fn serialize_canada_with_msgpack(input: &Canada) -> JsValue {
msgpack_js::serialize(input).unwrap()
}

#[cfg(feature = "serde-wasm-bindgen")]
#[wasm_bindgen]
pub fn parse_citm_catalog_with_serde_wasm_bindgen(input: JsValue) -> CitmCatalog {
Expand All @@ -73,6 +107,12 @@ pub fn parse_citm_catalog_with_serde_json(input: JsValue) -> CitmCatalog {
input.into_serde().unwrap()
}

#[cfg(feature = "msgpack")]
#[wasm_bindgen]
pub fn parse_citm_catalog_with_msgpack(input: JsValue) -> CitmCatalog {
msgpack_js::parse(input).unwrap()
}

#[cfg(feature = "serde-wasm-bindgen")]
#[wasm_bindgen]
pub fn serialize_citm_catalog_with_serde_wasm_bindgen(input: &CitmCatalog) -> JsValue {
Expand All @@ -86,6 +126,12 @@ pub fn serialize_citm_catalog_with_serde_json(input: &CitmCatalog) -> JsValue {
JsValue::from_serde(input).unwrap()
}

#[cfg(feature = "msgpack")]
#[wasm_bindgen]
pub fn serialize_citm_catalog_with_msgpack(input: &CitmCatalog) -> JsValue {
msgpack_js::serialize(input).unwrap()
}

#[cfg(feature = "serde-wasm-bindgen")]
#[wasm_bindgen]
pub fn parse_twitter_with_serde_wasm_bindgen(input: JsValue) -> Twitter {
Expand All @@ -99,6 +145,12 @@ pub fn parse_twitter_with_serde_json(input: JsValue) -> Twitter {
input.into_serde().unwrap()
}

#[cfg(feature = "msgpack")]
#[wasm_bindgen]
pub fn parse_twitter_with_msgpack(input: JsValue) -> Twitter {
msgpack_js::parse(input).unwrap()
}

#[cfg(feature = "serde-wasm-bindgen")]
#[wasm_bindgen]
pub fn serialize_twitter_with_serde_wasm_bindgen(input: &Twitter) -> JsValue {
Expand All @@ -111,3 +163,9 @@ pub fn serialize_twitter_with_serde_wasm_bindgen(input: &Twitter) -> JsValue {
pub fn serialize_twitter_with_serde_json(input: &Twitter) -> JsValue {
JsValue::from_serde(input).unwrap()
}

#[cfg(feature = "msgpack")]
#[wasm_bindgen]
pub fn serialize_twitter_with_msgpack(input: &Twitter) -> JsValue {
msgpack_js::serialize(input).unwrap()
}

0 comments on commit d8e4f71

Please sign in to comment.