Skip to content

Commit

Permalink
Improve sleep resolution (#12049)
Browse files Browse the repository at this point in the history
# Description
This improves the resolution of the sleep commands by simply not
clamping to the default 100ms ctrl+c signal checking loop if the
passed-in duration is shorter.

# User-Facing Changes
You can use smaller values in sleep.

```
# Before
timeit { 0..100 | each { |row| print $row; sleep 10ms; } } # +10sec

# After
timeit { 0..100 | each { |row| print $row; sleep 10ms; } } # +1sec
```

It still depends on the internal behavior of thread::sleep and the OS
timers. In windows it doesn't seem to go much lower than 15 or 10ms, or
0 if you asked for that.

# After Submitting
Sleep didn't have anything documenting its minimum value, so this should
be more in line with its standard procedure. It will still never sleep
for less time than allocated.

Did you know `sleep` can take multiple durations, and it'll add them up?
I didn't
  • Loading branch information
Dorumin committed Mar 2, 2024
1 parent 4cda183 commit 669659f
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion crates/nu-command/src/platform/sleep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl Command for Sleep {
let ctrlc_ref = &engine_state.ctrlc.clone();
let start = Instant::now();
loop {
thread::sleep(CTRL_C_CHECK_INTERVAL);
thread::sleep(CTRL_C_CHECK_INTERVAL.min(total_dur));
if start.elapsed() >= total_dur {
break;
}
Expand Down

0 comments on commit 669659f

Please sign in to comment.