summaryrefslogtreecommitdiff
path: root/core/examples/http_bench_json_ops.rs
diff options
context:
space:
mode:
authorJared Beller <25756846+jbellerb@users.noreply.github.com>2021-02-13 11:56:56 -0500
committerGitHub <noreply@github.com>2021-02-13 11:56:56 -0500
commitb50691efed482496e241857921b66b65bec61655 (patch)
tree63acea6b2e2b1ae9ca43585f22800cade8458e27 /core/examples/http_bench_json_ops.rs
parentaf460fc464562566dc1534c0f61f53c2976b9bd7 (diff)
refactor(core): Strongly typed deserialization of JSON ops (#9423)
This PR makes json_op_sync/async generic to all Deserialize/Serialize types instead of the loosely-typed serde_json::Value. Since serde_json::Value implements Deserialize/Serialize, very little existing code needs to be updated, however as json_op_sync/async are now generic, type inference is broken in some cases (see cli/build.rs:146). I've found this reduces a good bit of boilerplate, as seen in the updated deno_core examples. This change may also reduce serialization and deserialization overhead as serde has a better idea of what types it is working with. I am currently working on benchmarks to confirm this and I will update this PR with my findings.
Diffstat (limited to 'core/examples/http_bench_json_ops.rs')
-rw-r--r--core/examples/http_bench_json_ops.rs74
1 files changed, 26 insertions, 48 deletions
diff --git a/core/examples/http_bench_json_ops.rs b/core/examples/http_bench_json_ops.rs
index 4fd25d6b8..c24175747 100644
--- a/core/examples/http_bench_json_ops.rs
+++ b/core/examples/http_bench_json_ops.rs
@@ -14,10 +14,11 @@ use deno_core::OpState;
use deno_core::RcRef;
use deno_core::Resource;
use deno_core::ZeroCopyBuf;
+use serde::Deserialize;
+use serde::Serialize;
use serde_json::Value;
use std::cell::RefCell;
use std::convert::TryFrom;
-use std::convert::TryInto;
use std::env;
use std::io::Error;
use std::net::SocketAddr;
@@ -124,83 +125,67 @@ fn create_js_runtime() -> JsRuntime {
runtime
}
+#[derive(Deserialize, Serialize)]
+struct ResourceId {
+ rid: u32,
+}
+
fn op_listen(
state: &mut OpState,
- _args: Value,
+ _args: (),
_bufs: &mut [ZeroCopyBuf],
-) -> Result<Value, AnyError> {
+) -> Result<ResourceId, AnyError> {
debug!("listen");
let addr = "127.0.0.1:4544".parse::<SocketAddr>().unwrap();
let std_listener = std::net::TcpListener::bind(&addr)?;
std_listener.set_nonblocking(true)?;
let listener = TcpListener::try_from(std_listener)?;
let rid = state.resource_table.add(listener);
- Ok(serde_json::json!({ "rid": rid }))
+ Ok(ResourceId { rid })
}
fn op_close(
state: &mut OpState,
- args: Value,
+ args: ResourceId,
_buf: &mut [ZeroCopyBuf],
-) -> Result<Value, AnyError> {
- let rid: u32 = args
- .get("rid")
- .unwrap()
- .as_u64()
- .unwrap()
- .try_into()
- .unwrap();
- debug!("close rid={}", rid);
+) -> Result<(), AnyError> {
+ debug!("close rid={}", args.rid);
state
.resource_table
- .close(rid)
- .map(|_| serde_json::json!(()))
+ .close(args.rid)
+ .map(|_| ())
.ok_or_else(bad_resource_id)
}
async fn op_accept(
state: Rc<RefCell<OpState>>,
- args: Value,
+ args: ResourceId,
_bufs: BufVec,
-) -> Result<Value, AnyError> {
- let rid: u32 = args
- .get("rid")
- .unwrap()
- .as_u64()
- .unwrap()
- .try_into()
- .unwrap();
- debug!("accept rid={}", rid);
+) -> Result<ResourceId, AnyError> {
+ debug!("accept rid={}", args.rid);
let listener = state
.borrow()
.resource_table
- .get::<TcpListener>(rid)
+ .get::<TcpListener>(args.rid)
.ok_or_else(bad_resource_id)?;
let stream = listener.accept().await?;
let rid = state.borrow_mut().resource_table.add(stream);
- Ok(serde_json::json!({ "rid": rid }))
+ Ok(ResourceId { rid })
}
async fn op_read(
state: Rc<RefCell<OpState>>,
- args: Value,
+ args: ResourceId,
mut bufs: BufVec,
) -> Result<Value, AnyError> {
assert_eq!(bufs.len(), 1, "Invalid number of arguments");
- let rid: u32 = args
- .get("rid")
- .unwrap()
- .as_u64()
- .unwrap()
- .try_into()
- .unwrap();
- debug!("read rid={}", rid);
+ debug!("read rid={}", args.rid);
let stream = state
.borrow()
.resource_table
- .get::<TcpStream>(rid)
+ .get::<TcpStream>(args.rid)
.ok_or_else(bad_resource_id)?;
let nread = stream.read(&mut bufs[0]).await?;
Ok(serde_json::json!({ "nread": nread }))
@@ -208,23 +193,16 @@ async fn op_read(
async fn op_write(
state: Rc<RefCell<OpState>>,
- args: Value,
+ args: ResourceId,
bufs: BufVec,
) -> Result<Value, AnyError> {
assert_eq!(bufs.len(), 1, "Invalid number of arguments");
- let rid: u32 = args
- .get("rid")
- .unwrap()
- .as_u64()
- .unwrap()
- .try_into()
- .unwrap();
- debug!("write rid={}", rid);
+ debug!("write rid={}", args.rid);
let stream = state
.borrow()
.resource_table
- .get::<TcpStream>(rid)
+ .get::<TcpStream>(args.rid)
.ok_or_else(bad_resource_id)?;
let nwritten = stream.write(&bufs[0]).await?;
Ok(serde_json::json!({ "nwritten": nwritten }))