bat icon indicating copy to clipboard operation
bat copied to clipboard

Bat is slow with --color=never --decorations=always on some files

Open Emilv2 opened this issue 1 year ago • 1 comments

Related to #304 What steps will reproduce the bug?

# create two identical test files, but with a different extension:
> seq 1 100000 > test && echo 'print(1)' >> test && cp test test.py
# confirm both commands show the same output, no syntax highlight and highlighted line 100000:
> bat --color=never --decorations=always --highlight-line=100000 --pager=never -- test.py | cat
> bat --color=never --decorations=always --highlight-line=100000 --pager=never -- test | cat

What happens? Previewing test.py takes ~10 longer than test, although the output is the same, syntax highlighting is not enabled.

> hyperfine 'bat --color=never --decorations=always --highlight-line=100000 --pager=never -- test'  'bat --color=never --decorations=always --highlight-line=100000 --pager=never -- test.py'
Benchmark 1: bat --color=never --decorations=always --highlight-line=100000 --pager=never -- test | cat
  Time (mean ± σ):     136.2 ms ±   2.1 ms    [User: 102.3 ms, System: 94.2 ms]
  Range (min … max):   132.9 ms … 142.4 ms    21 runs
 
Benchmark 2: bat --color=never --decorations=always --highlight-line=100000 --pager=never -- test.py | cat
  Time (mean ± σ):      1.151 s ±  0.013 s    [User: 1.117 s, System: 0.281 s]
  Range (min … max):    1.140 s …  1.176 s    10 runs
 
Summary
  'bat --color=never --decorations=always --highlight-line=100000 --pager=never -- test | cat' ran
    8.45 ± 0.16 times faster than 'bat --color=never --decorations=always --highlight-line=100000 --pager=never -- test.py | cat'
hyperfine    13.47s user 4.81s system 125% cpu 14.529 total

...

What did you expect to happen instead?

Identical output should take the same time. I assume bat does some unnecessary parsing of the file in case of the test.py file. bat --color=never --decorations=never --highlight-line=100000 --pager=never -- test.py runs fast too, but the highlight coloring is not preserved when piping to another command.

How did you install bat?

pacman -S bat


bat version and environment

Software version

bat 0.22.1 (e5d95796)

Operating system

Linux 6.0.2-arch1-1

Command-line

bat --color=never --decorations=always --highlight-line=100000 --pager=never --diagnostic -- test.py 

Environment variables

SHELL=/bin/zsh
PAGER=less
LESS=<not set>
LANG=en_US.UTF-8
LC_ALL=en_US.UTF-8
BAT_PAGER=<not set>
BAT_CACHE_PATH=<not set>
BAT_CONFIG_PATH=<not set>
BAT_OPTS=<not set>
BAT_STYLE=<not set>
BAT_TABS=<not set>
BAT_THEME=<not set>
XDG_CONFIG_HOME=<not set>
XDG_CACHE_HOME=<not set>
COLORTERM=yes
NO_COLOR=<not set>
MANPAGER=<not set>

System Config file

Could not read contents of '/etc/bat/config': No such file or directory (os error 2).

Config file

--theme="Solarized (light)"

Custom assets metadata

Could not read contents of '~/.cache/bat/metadata.yaml': No such file or directory (os error 2).

Custom assets

'/home/emil/.cache/bat' not found

Compile time information

  • Profile: release
  • Target triple: x86_64-unknown-linux-gnu
  • Family: unix
  • OS: linux
  • Architecture: x86_64
  • Pointer width: 64
  • Endian: little
  • CPU features: fxsr,sse,sse2
  • Host: x86_64-unknown-linux-gnu

Less version

Could not run command 'never': No such file or directory (os error 2)

Emilv2 avatar Nov 05 '22 23:11 Emilv2

Excellent catch! Skipping syntax parsing when --color=never results in significant performance gains:

image

eth-p avatar Feb 12 '24 06:02 eth-p