Skip to content

key-bindings.fish: much faster ctrl-r in Awk for those without Perl #4661

@averms

Description

@averms

Checklist

  • I have read through the manual page (man fzf)
  • I have searched through the existing issues
  • For bug reports, I have checked if the bug is reproducible in the latest version of fzf

Output of fzf --version

0.67.0 (Homebrew)

OS

  • Linux
  • macOS
  • Windows
  • Etc.

Shell

  • bash
  • zsh
  • fish

Problem / Steps to reproduce

I don't have perl installed (it's not there by default on Fedora) so when I press CTRL-R I'm using @bitraid's clever Fish script (#4171) to preprocess the history output. Unfortunately, despite the optimizations it's still around 20x slower than Perl on my history file with 40k entries.

I didn't want to install Perl so I rewrote it as an AWK one-liner:

builtin history -z --reverse | tr '\0' '\036' | awk 'BEGIN{RS="\036"; ORS="\036"} {gsub(/\n/, "\n\t"); print NR "\t" $0}' | tr '\036' '\0'

tested on macOS and Linux to have identical output to the Perl command and perform within 1-2 ms.

It's entirely POSIX-compliant and could replace both the other methods of pre-processing in the script. If you want it I'll open a PR.

The only downside is that it'll break if you have any RECORD SEPERATOR (https://codepoints.net/U+001E?lang=en) in your Fish history

Benchmarks

Benchmark 1: ./awk.fish
  Time (mean ± σ):      65.7 ms ±   2.7 ms    [User: 59.4 ms, System: 29.6 ms]
  Range (min … max):    61.7 ms …  70.8 ms    43 runs
 
Benchmark 2: ./perl.fish
  Time (mean ± σ):      63.1 ms ±   2.3 ms    [User: 61.6 ms, System: 23.0 ms]
  Range (min … max):    60.0 ms …  69.1 ms    49 runs
 
Benchmark 3: ./pure_fish.fish
  Time (mean ± σ):      1.263 s ±  0.011 s    [User: 0.917 s, System: 0.678 s]
  Range (min … max):    1.250 s …  1.279 s    10 runs
 
Summary
  ./perl.fish ran
    1.04 ± 0.06 times faster than ./awk.fish
   20.01 ± 0.76 times faster than ./pure_fish.fish

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions