bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: bash 5.1 heredoc pipes problematic, shopt needed


From: Alexey
Subject: Re: bash 5.1 heredoc pipes problematic, shopt needed
Date: Tue, 26 Apr 2022 00:33:06 +0400
User-agent: Mail UserAgent

On 2022-04-25 23:55, Chet Ramey wrote:
On 4/25/22 1:03 PM, Alexey wrote:

There is one more problem with pipes — they are extremely slow.

It's not pipes per se -- it's the semantics of the shell `read' builtin
and standard input. Profiling or a system call tracer would have provided
insight.

The shell is very careful not to `steal' input from processes it runs.
This means that

seq 10 | { read one ; cat ; }

will `behead' one line -- and only one line -- from the standard input.
`read' can read ahead, but only under specific conditions. The input file descriptor has to be seekable, so the shell can undo any readahead before invoking another process. With regular files, this is easy -- lseek works
with a negative offset. With pipes, which are not seekable, it is not.

So the time it takes to read the command substitution output is identical
for each run. The time for a single write to a pipe or a temp file is
virtually identical as well.

The difference is the read builtin: when reading from the pipe, you have to
issue 65536 one-character reads. When reading from a file, since it's
seekable, you don't (bash-5.2 happens to use a 4K buffer, so it issues
17 reads).

The upshot is that this doesn't say all that much about pipes vs. temp
files in general, but it demonstrates that it makes a huge difference when
you read a single extremely long line using the `read' builtin.

Sorry for wrong wording. Of course I meant not all pipes, but only how bash read from them.

My key point that we have two choices for future:
 - make read from pipe faster, or
 - provide options for force here-string to use temp files.

I don't see any other options for fast-enough performance.


Regards,
Alexey.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]