Skip to content

Commit

Permalink
custom-completions: scoop: improve performance of getting scoop commands
Browse files Browse the repository at this point in the history
Instead of calling scoop that calls powershell, use nushell's native
commands.

On Windows 11 virtual machine,
powershell takes '330ms 492µs 860ns' on fastest case,
with this commit takes '23ms 24µs 660ns' on slowest.

```nu
> use old-scoop-completions.nu *
> let oldTimes = (1..10 | each { timeit { scoopCommands } })
> $oldTimes | sort --reverse
╭───┬───────────────────╮
│ 0 │ 357ms 565µs 720ns │
│ 1 │  344ms 64µs 450ns │
│ 2 │ 343ms 264µs 680ns │
│ 3 │  342ms 686µs 10ns │
│ 4 │ 342ms 241µs 740ns │
│ 5 │ 338ms 365µs 910ns │
│ 6 │ 337ms 682µs 790ns │
│ 7 │ 335ms 473µs 290ns │
│ 8 │ 335ms 186µs 830ns │
│ 9 │ 330ms 492µs 860ns │
╰───┴───────────────────╯
> $oldTimes | math avg
340ms 702µs 428ns

> use scoop-completions.nu *
> let newTimes = (1..10 | each { timeit { scoopCommands } })
> $newTimes | sort --reverse
╭───┬──────────────────╮
│ 0 │  23ms 24µs 660ns │
│ 1 │  18ms 33µs 480ns │
│ 2 │ 15ms 597µs 650ns │
│ 3 │ 15ms 412µs 850ns │
│ 4 │  15ms 58µs 770ns │
│ 5 │  14ms 536µs 30ns │
│ 6 │  14ms 366µs 20ns │
│ 7 │ 14ms 175µs 270ns │
│ 8 │ 13ms 688µs 730ns │
│ 9 │ 13ms 378µs 590ns │
╰───┴──────────────────╯
> $newTimes | math avg
15ms 727µs 205ns
```
  • Loading branch information
e2dk4r committed Dec 7, 2024
1 parent 6c9f974 commit 8b0f3ba
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions custom-completions/scoop/scoop-completions.nu
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,29 @@ def scoopShimBuilds [] {
}

def scoopCommands [] {
^powershell -nop -nol -c "(scoop help | ConvertTo-Json -Compress)"
| decode
| lines
| last
| to text
| from json
| rename value description
let libexecDir = if ('SCOOP' in $env) {
[ $env.SCOOP, 'apps', 'scoop', 'current', 'libexec' ] | path join
} else {
[ $env.USERPROFILE, 'scoop', 'apps', 'scoop', 'current', 'libexec' ] | path join
}

let commands = (
ls $libexecDir
| each {|command|
[
[value, description];
[
# eg. scoop-help.ps1 -> help
($command.name | path basename | str substring 6..-4),
# second line is starts with '# Summary: '
# eg. '# Summary: Install apps' -> 'Install apps'
(open $command.name | lines | skip 1 | first | str substring 11..)
]
]
}
| flatten
)
$commands
}

def scoopAliases [] {
Expand Down

0 comments on commit 8b0f3ba

Please sign in to comment.