diff options
author | andy finch <andyfinch7@gmail.com> | 2019-06-13 23:43:54 -0400 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2019-06-13 20:43:54 -0700 |
commit | dc60fe9f300043f191286ef804a365e16e455f87 (patch) | |
tree | c6b74e9faa6f26745b8770a18d0ae46ee34f3774 /core/examples | |
parent | fdd2eb538327ee3f50fe2869320411191830c985 (diff) |
Refactor dispatch handling (#2452)
Promise id is now created in core and passed back to JS.
Diffstat (limited to 'core/examples')
-rw-r--r-- | core/examples/http_bench.js | 16 | ||||
-rw-r--r-- | core/examples/http_bench.rs | 16 |
2 files changed, 15 insertions, 17 deletions
diff --git a/core/examples/http_bench.js b/core/examples/http_bench.js index 8eb764b55..7e678aecd 100644 --- a/core/examples/http_bench.js +++ b/core/examples/http_bench.js @@ -13,7 +13,6 @@ const responseBuf = new Uint8Array( .map(c => c.charCodeAt(0)) ); const promiseMap = new Map(); -let nextPromiseId = 1; function assert(cond) { if (!cond) { @@ -37,8 +36,8 @@ const scratchBytes = new Uint8Array( ); assert(scratchBytes.byteLength === 4 * 4); -function send(promiseId, opId, arg, zeroCopy = null) { - scratch32[0] = promiseId; +function send(isSync, opId, arg, zeroCopy = null) { + scratch32[0] = isSync; scratch32[1] = opId; scratch32[2] = arg; scratch32[3] = -1; @@ -47,10 +46,9 @@ function send(promiseId, opId, arg, zeroCopy = null) { /** Returns Promise<number> */ function sendAsync(opId, arg, zeroCopy = null) { - const promiseId = nextPromiseId++; + const promiseId = send(false, opId, arg, zeroCopy); const p = createResolvable(); promiseMap.set(promiseId, p); - send(promiseId, opId, arg, zeroCopy); return p; } @@ -58,7 +56,7 @@ function recordFromBuf(buf) { assert(buf.byteLength === 16); const buf32 = new Int32Array(buf.buffer, buf.byteOffset, buf.byteLength / 4); return { - promiseId: buf32[0], + isSync: !!buf32[0], opId: buf32[1], arg: buf32[2], result: buf32[3] @@ -67,14 +65,14 @@ function recordFromBuf(buf) { /** Returns i32 number */ function sendSync(opId, arg) { - const buf = send(0, opId, arg); + const buf = send(true, opId, arg); const record = recordFromBuf(buf); return record.result; } -function handleAsyncMsgFromRust(buf) { +function handleAsyncMsgFromRust(promiseId, buf) { const record = recordFromBuf(buf); - const { promiseId, result } = record; + const { result } = record; const p = promiseMap.get(promiseId); promiseMap.delete(promiseId); p.resolve(result); diff --git a/core/examples/http_bench.rs b/core/examples/http_bench.rs index e8c5ec1b7..0f0cd6a4b 100644 --- a/core/examples/http_bench.rs +++ b/core/examples/http_bench.rs @@ -44,7 +44,7 @@ const OP_CLOSE: i32 = 5; #[derive(Clone, Debug, PartialEq)] pub struct Record { - pub promise_id: i32, + pub is_sync: i32, pub op_id: i32, pub arg: i32, pub result: i32, @@ -52,8 +52,8 @@ pub struct Record { impl Into<Buf> for Record { fn into(self) -> Buf { - let buf32 = vec![self.promise_id, self.op_id, self.arg, self.result] - .into_boxed_slice(); + let buf32 = + vec![self.is_sync, self.op_id, self.arg, self.result].into_boxed_slice(); let ptr = Box::into_raw(buf32) as *mut [u8; 16]; unsafe { Box::from_raw(ptr) } } @@ -65,7 +65,7 @@ impl From<&[u8]> for Record { let ptr = s.as_ptr() as *const i32; let ints = unsafe { std::slice::from_raw_parts(ptr, 4) }; Record { - promise_id: ints[0], + is_sync: ints[0], op_id: ints[1], arg: ints[2], result: ints[3], @@ -81,7 +81,7 @@ impl From<Buf> for Record { let ints: Box<[i32]> = unsafe { Box::from_raw(ptr) }; assert_eq!(ints.len(), 4); Record { - promise_id: ints[0], + is_sync: ints[0], op_id: ints[1], arg: ints[2], result: ints[3], @@ -92,7 +92,7 @@ impl From<Buf> for Record { #[test] fn test_record_from() { let r = Record { - promise_id: 1, + is_sync: 1, op_id: 2, arg: 3, result: 4, @@ -111,9 +111,9 @@ fn test_record_from() { pub type HttpBenchOp = dyn Future<Item = i32, Error = std::io::Error> + Send; -fn dispatch(control: &[u8], zero_copy_buf: Option<PinnedBuf>) -> Op { +fn dispatch(control: &[u8], zero_copy_buf: Option<PinnedBuf>) -> CoreOp { let record = Record::from(control); - let is_sync = record.promise_id == 0; + let is_sync = record.is_sync == 1; let http_bench_op = match record.op_id { OP_LISTEN => { assert!(is_sync); |