Skip to content

Remove dispatch optimization #2732

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

Merged
merged 3 commits into from
Aug 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
65 changes: 4 additions & 61 deletions core/isolate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,34 +269,14 @@ impl Isolate {
zero_copy_buf: deno_pinned_buf,
) {
let isolate = unsafe { Isolate::from_raw_ptr(user_data) };
let control_shared = isolate.shared.shift();

let op = if control_argv0.len() > 0 {
// The user called Deno.core.send(control)
if let Some(ref f) = isolate.dispatch {
f(control_argv0.as_ref(), PinnedBuf::new(zero_copy_buf))
} else {
panic!("isolate.dispatch not set")
}
} else if let Some(c) = control_shared {
// The user called Deno.sharedQueue.push(control)
if let Some(ref f) = isolate.dispatch {
f(&c, PinnedBuf::new(zero_copy_buf))
} else {
panic!("isolate.dispatch not set")
}
let op = if let Some(ref f) = isolate.dispatch {
f(control_argv0.as_ref(), PinnedBuf::new(zero_copy_buf))
} else {
// The sharedQueue is empty. The shouldn't happen usually, but it's also
// not technically a failure.
#[cfg(test)]
unreachable!();
#[cfg(not(test))]
return;
panic!("isolate.dispatch not set")
};

// At this point the SharedQueue should be empty.
assert_eq!(isolate.shared.size(), 0);

debug_assert_eq!(isolate.shared.size(), 0);
match op {
Op::Sync(buf) => {
// For sync messages, we always return the response via Deno.core.send's
Expand Down Expand Up @@ -871,43 +851,6 @@ pub mod tests {
});
}

#[test]
fn test_shared() {
Copy link
Member Author

@ry ry Aug 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test was testing exactly the optimization that was removed.

run_in_task(|| {
let (mut isolate, dispatch_count) = setup(Mode::AsyncImmediate);

js_check(isolate.execute(
"setup2.js",
r#"
let nrecv = 0;
Deno.core.setAsyncHandler((buf) => {
assert(buf.byteLength === 1);
assert(buf[0] === 43);
nrecv++;
});
"#,
));
assert_eq!(dispatch_count.load(Ordering::Relaxed), 0);

js_check(isolate.execute(
"send1.js",
r#"
let control = new Uint8Array([42]);
Deno.core.sharedQueue.push(control);
Deno.core.send();
assert(nrecv === 0);

Deno.core.sharedQueue.push(control);
Deno.core.send();
assert(nrecv === 0);
"#,
));
assert_eq!(dispatch_count.load(Ordering::Relaxed), 2);
assert_eq!(Async::Ready(()), isolate.poll().unwrap());
js_check(isolate.execute("send1.js", "assert(nrecv === 2);"));
});
}

#[test]
fn dyn_import_err() {
// Test an erroneous dynamic import where the specified module isn't found.
Expand Down
6 changes: 1 addition & 5 deletions core/shared_queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,7 @@ SharedQueue Binary Layout

function dispatch(control, zeroCopy = null) {
maybeInit();
// First try to push control to shared.
const success = push(control);
// If successful, don't use first argument of core.send.
const arg0 = success ? null : control;
return Deno.core.send(arg0, zeroCopy);
return Deno.core.send(control, zeroCopy);
}

const denoCore = {
Expand Down
3 changes: 3 additions & 0 deletions core/shared_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ impl SharedQueue {
s[INDEX_OFFSETS + index] = end as u32;
}

#[cfg(test)]
fn get_end(&self, index: usize) -> Option<usize> {
if index < self.num_records() {
let s = self.as_u32_slice();
Expand All @@ -112,6 +113,7 @@ impl SharedQueue {
}
}

#[cfg(test)]
fn get_offset(&self, index: usize) -> Option<usize> {
if index < self.num_records() {
Some(if index == 0 {
Expand All @@ -126,6 +128,7 @@ impl SharedQueue {
}

/// Returns none if empty.
#[cfg(test)]
pub fn shift(&mut self) -> Option<&[u8]> {
let u32_slice = self.as_u32_slice();
let i = u32_slice[INDEX_NUM_SHIFTED_OFF] as usize;
Expand Down