1
Fork 0
mirror of https://github.com/RGBCube/nu_scripts synced 2025-08-01 06:37:46 +00:00

custom-completions: scoop: improve performance of getting scoop commands (#988)

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
```
This commit is contained in:
e2dk4r 2024-12-07 16:13:00 +03:00 committed by GitHub
parent 6c9f974df6
commit 6bd54bfab2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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 [] {