Repository navigation
check signals in nu-glob and ls - #15140
Conversation
sholderbach
left a comment
There was a problem hiding this comment.
Thanks for tackling this!
| bench = false | ||
|
|
||
| [dependencies] | ||
| nu-protocol = { path = "../nu-protocol", version = "0.102.1", default-features = false } |
There was a problem hiding this comment.
We kind of need to be careful here, when it comes to circular imports. nu-engine as the next crate down the line of nu-protocol already depends on nu-glob. So far it doesn't seem to cause trouble, but in the future (or whenever we can carry @devyn 's platform revised platform-tolerant and fancy glob library across the finish line) we may have to watch out for that.
| children.sort_by(|p1, p2| p2.file_name().cmp(&p1.file_name())); | ||
| children.sort_by(|p1, p2| { | ||
| if signals.interrupted() { | ||
| std::cmp::Ordering::Equal |
There was a problem hiding this comment.
Can this Ordering::Equal cause any weirdness?
I think we should check below again for signals.interrupted and explicitly return the error.
There was a problem hiding this comment.
I don't think it'll cause problems because we don't use the result. Here's a contrived benchmark that demonstrates the improvement when we're interrupted in this code path:
mkdir ~/bigdir
cd ~/bigdir
seq 1 1000000 | par-each { || touch (random chars) }
cd <nushell working dir>
gh pr checkout 15140
def bench [] { cargo build; timeit { do -i { ./target/debug/nu -c "ls ~/bigdir/*" } } }
"--- a/crates/nu-glob/src/lib.rs
+++ b/crates/nu-glob/src/lib.rs
@@ -1011,13 +1011,8 @@ fn fill_todo(
// .starts_with('.')
// });
// }
- children.sort_by(|p1, p2| {
- if signals.interrupted() {
- Ordering::Equal
- } else {
- p2.file_name().cmp(&p1.file_name())
- }
- });
+ nix::sys::signal::raise(nix::sys::signal::SIGINT).unwrap();
+ children.sort_by(|p1, p2| p2.file_name().cmp(&p1.file_name()));
if signals.interrupted() {
todo.push(Err(GlobError {
path: path.to_path_buf(),
" | save /tmp/bench-slow.diff
git apply /tmp/bench-slow.diff
bench
# 2sec 819ms 882µs 883ns
"--- a/crates/nu-glob/src/lib.rs
+++ b/crates/nu-glob/src/lib.rs
@@ -1012,7 +1012,13 @@ fn fill_todo(
// });
// }
nix::sys::signal::raise(nix::sys::signal::SIGINT).unwrap();
- children.sort_by(|p1, p2| p2.file_name().cmp(&p1.file_name()));
+ children.sort_by(|p1, p2| {
+ if signals.interrupted() {
+ Ordering::Equal
+ } else {
+ p2.file_name().cmp(&p1.file_name())
+ }
+ });
if signals.interrupted() {
todo.push(Err(GlobError {
path: path.to_path_buf(),
" | save /tmp/speedup.diff
git apply /tmp/speedup.diff
bench
# 681ms 702µs 760nsThere was a problem hiding this comment.
While I understand the potential for speedup, I think sort_by may choose to panic if the interrupt changes the sort order mid-operation.
Changing a relation of three elements a b c from some ordering to Equals can violate both the transitivity and duality properties for PartialOrd and the rules for Ord
There was a problem hiding this comment.
Oh, good catch! That's unfortunate.
Fixes #10144 # User-Facing Changes Long running glob expansions and `ls` runs (e.g. `ls /**/*`) can now be interrupted with ctrl-c.
|
Are we through this? Just as a heads up from a reviewing point of view, force-pushed amended commits are really hard to follow when a conversation is happening, as we squash at the end its fine to leave an iterative history. (I am fine with a force-push when things are rebased onto larger upstream change and its more readable than a big merge commit blob which GH doesn't render well, or if you tweaked one particular commit due to formatting or whatever in a larger history) |
|
I think I addressed your comments. Sorry about the big rebase! Here's a post-review diff with the original base commit: https://github.com/nushell/nushell/compare/43bdf607cd7b26cd2d7083e230c3b2753abb58d3..e9bf870272190850c4ce81f910e41538c5128fb6 |
|
Thanks, let's give it a try :) |
Fixes #10144
User-Facing Changes
Long running glob expansions and
lsruns (e.g.ls /**/*) can now beinterrupted with ctrl-c.