1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-11 16:42:21 -05:00

No longer require aligned buffer for shared queue (#3935)

Fixes: #3925
This commit is contained in:
Andy Finch 2020-02-09 13:54:16 -05:00 committed by GitHub
parent 61c5bb86db
commit 1abd408770
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 23 deletions

View file

@ -32,11 +32,7 @@ fn serialize_result(promise_id: Option<u64>, result: JsonResult) -> Buf {
Ok(v) => json!({ "ok": v, "promiseId": promise_id }), Ok(v) => json!({ "ok": v, "promiseId": promise_id }),
Err(err) => json!({ "err": json_err(err), "promiseId": promise_id }), Err(err) => json!({ "err": json_err(err), "promiseId": promise_id }),
}; };
let mut vec = serde_json::to_vec(&value).unwrap(); serde_json::to_vec(&value).unwrap().into_boxed_slice()
debug!("JSON response pre-align, len={}", vec.len());
// Align to 32bit word, padding with the space character.
vec.resize((vec.len() + 3usize) & !3usize, b' ');
vec.into_boxed_slice()
} }
#[derive(Deserialize)] #[derive(Deserialize)]

View file

@ -781,7 +781,7 @@ pub mod tests {
Mode::Async => { Mode::Async => {
assert_eq!(control.len(), 1); assert_eq!(control.len(), 1);
assert_eq!(control[0], 42); assert_eq!(control[0], 42);
let buf = vec![43u8, 0, 0, 0].into_boxed_slice(); let buf = vec![43u8].into_boxed_slice();
Op::Async(futures::future::ok(buf).boxed()) Op::Async(futures::future::ok(buf).boxed())
} }
Mode::AsyncUnref => { Mode::AsyncUnref => {
@ -790,14 +790,14 @@ pub mod tests {
let fut = async { let fut = async {
// This future never finish. // This future never finish.
futures::future::pending::<()>().await; futures::future::pending::<()>().await;
let buf = vec![43u8, 0, 0, 0].into_boxed_slice(); let buf = vec![43u8].into_boxed_slice();
Ok(buf) Ok(buf)
}; };
Op::AsyncUnref(fut.boxed()) Op::AsyncUnref(fut.boxed())
} }
Mode::OverflowReqSync => { Mode::OverflowReqSync => {
assert_eq!(control.len(), 100 * 1024 * 1024); assert_eq!(control.len(), 100 * 1024 * 1024);
let buf = vec![43u8, 0, 0, 0].into_boxed_slice(); let buf = vec![43u8].into_boxed_slice();
Op::Sync(buf) Op::Sync(buf)
} }
Mode::OverflowResSync => { Mode::OverflowResSync => {
@ -811,7 +811,7 @@ pub mod tests {
} }
Mode::OverflowReqAsync => { Mode::OverflowReqAsync => {
assert_eq!(control.len(), 100 * 1024 * 1024); assert_eq!(control.len(), 100 * 1024 * 1024);
let buf = vec![43u8, 0, 0, 0].into_boxed_slice(); let buf = vec![43u8].into_boxed_slice();
Op::Async(futures::future::ok(buf).boxed()) Op::Async(futures::future::ok(buf).boxed())
} }
Mode::OverflowResAsync => { Mode::OverflowResAsync => {
@ -1007,7 +1007,7 @@ pub mod tests {
let control = new Uint8Array(100 * 1024 * 1024); let control = new Uint8Array(100 * 1024 * 1024);
let response = Deno.core.dispatch(1, control); let response = Deno.core.dispatch(1, control);
assert(response instanceof Uint8Array); assert(response instanceof Uint8Array);
assert(response.length == 4); assert(response.length == 1);
assert(response[0] == 43); assert(response[0] == 43);
assert(asyncRecv == 0); assert(asyncRecv == 0);
"#, "#,
@ -1046,7 +1046,7 @@ pub mod tests {
r#" r#"
let asyncRecv = 0; let asyncRecv = 0;
Deno.core.setAsyncHandler(1, (buf) => { Deno.core.setAsyncHandler(1, (buf) => {
assert(buf.byteLength === 4); assert(buf.byteLength === 1);
assert(buf[0] === 43); assert(buf[0] === 43);
asyncRecv++; asyncRecv++;
}); });

View file

@ -181,7 +181,6 @@ impl SharedQueue {
end, end,
record.len() record.len()
); );
assert_eq!(record.len() % 4, 0);
let index = self.num_records(); let index = self.num_records();
if end > self.bytes().len() || index >= MAX_RECORDS { if end > self.bytes().len() || index >= MAX_RECORDS {
debug!("WARNING the sharedQueue overflowed"); debug!("WARNING the sharedQueue overflowed");
@ -259,21 +258,21 @@ mod tests {
#[test] #[test]
fn overflow() { fn overflow() {
let mut q = SharedQueue::new(RECOMMENDED_SIZE); let mut q = SharedQueue::new(RECOMMENDED_SIZE);
assert!(q.push(0, &alloc_buf(RECOMMENDED_SIZE - 4))); assert!(q.push(0, &alloc_buf(RECOMMENDED_SIZE - 1)));
assert_eq!(q.size(), 1); assert_eq!(q.size(), 1);
assert!(!q.push(0, &alloc_buf(8))); assert!(!q.push(0, &alloc_buf(2)));
assert_eq!(q.size(), 1); assert_eq!(q.size(), 1);
assert!(q.push(0, &alloc_buf(4))); assert!(q.push(0, &alloc_buf(1)));
assert_eq!(q.size(), 2); assert_eq!(q.size(), 2);
let (_op_id, buf) = q.shift().unwrap(); let (_op_id, buf) = q.shift().unwrap();
assert_eq!(buf.len(), RECOMMENDED_SIZE - 4); assert_eq!(buf.len(), RECOMMENDED_SIZE - 1);
assert_eq!(q.size(), 1); assert_eq!(q.size(), 1);
assert!(!q.push(0, &alloc_buf(4))); assert!(!q.push(0, &alloc_buf(1)));
let (_op_id, buf) = q.shift().unwrap(); let (_op_id, buf) = q.shift().unwrap();
assert_eq!(buf.len(), 4); assert_eq!(buf.len(), 1);
assert_eq!(q.size(), 0); assert_eq!(q.size(), 0);
} }
@ -281,19 +280,25 @@ mod tests {
fn full_records() { fn full_records() {
let mut q = SharedQueue::new(RECOMMENDED_SIZE); let mut q = SharedQueue::new(RECOMMENDED_SIZE);
for _ in 0..MAX_RECORDS { for _ in 0..MAX_RECORDS {
assert!(q.push(0, &alloc_buf(4))) assert!(q.push(0, &alloc_buf(1)))
} }
assert_eq!(q.push(0, &alloc_buf(4)), false); assert_eq!(q.push(0, &alloc_buf(1)), false);
// Even if we shift one off, we still cannot push a new record. // Even if we shift one off, we still cannot push a new record.
let _ignored = q.shift().unwrap(); let _ignored = q.shift().unwrap();
assert_eq!(q.push(0, &alloc_buf(4)), false); assert_eq!(q.push(0, &alloc_buf(1)), false);
} }
#[test] #[test]
#[should_panic] fn allow_any_buf_length() {
fn bad_buf_length() {
let mut q = SharedQueue::new(RECOMMENDED_SIZE); let mut q = SharedQueue::new(RECOMMENDED_SIZE);
// check that `record` that has length not a multiple of 4 will cause panic // check that `record` that has length not a multiple of 4 will cause panic
q.push(0, &alloc_buf(1));
q.push(0, &alloc_buf(2));
q.push(0, &alloc_buf(3)); q.push(0, &alloc_buf(3));
q.push(0, &alloc_buf(4));
q.push(0, &alloc_buf(5));
q.push(0, &alloc_buf(6));
q.push(0, &alloc_buf(7));
q.push(0, &alloc_buf(8));
} }
} }