You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Example of slowness compared to Bash (while or test problem?) #2007
I would expect them to take equal amounts of total time, except that fish will fully buffer the output (#1396) so you won't see anything until the command is complete. Does the fish version take more total time, or is it just more time before the first output is printed?
I'm not noticing the buffering you mentioned--output from Fish seems constant and gradual--but it seems that fish is simply taking a lot longer to do it, both in real time and in CPU time, and is using about 10 times as much memory as Bash.
$cat testfish.fish
#!/usr/bin/fish
function onlyDirs
while read dir
test -d $dir; and echo $dir
end
end
locate $HOME | onlyDirs
$ cat testbash.sh
#!/bin/bash
locate $HOME | bash -c 'while read dir; do [[ -d $dir ]] && echo $dir; done'
$ time ./testfish.fish >/dev/null
26.77user 16.72system 0:43.35elapsed 100%CPU (0avgtext+0avgdata 37520maxresident)k
0inputs+104outputs (0major+24078minor)pagefaults 0swaps
$ time ./testbash.sh >/dev/null
6.56user 7.50system 0:13.86elapsed 101%CPU (0avgtext+0avgdata 3124maxresident)k
0inputs+0outputs (0major+961minor)pagefaults 0swaps
It's not a big deal really, because I still write most scripts in Bash. The situation here was that I was writing some functions to quickly find directories with find/locate, Percol, and Fish, and I ended up having to pipe through Bash instead of using Fish functions because Fish was so much slower (about 3 times slower wall-time here). (Well, I actually ended up piping through Python scripts, because that was even faster, but the point here is the difference between Bash and Fish. :)
Hmm, it looks like reading multiple bytes is not trivial. Challenge accepted, though.
I was thinking, read N bytes at a time, process up to \n, \0, or eof, then lseek back to the start of the next line. However, we can't seek is stdin is a pipe. Can we just reuse the buffer in that case, and hope nobody notices that we've bitten off more stdin than we can chew? Sounds dangerous...
Actually, Bash is reading one character at a time as well, since this is a pipe (and it can't actually do buffered IO). @ridiculousfish something else must be going on..
I'm sure you're right, probably many things are going on :) But this is definitely one of them. For every byte read, fish blocks and then unblocks signals. See read_blocked. That pthread_sigmask call is consuming 25% of the time.
We're only blocking SIGCHLD, right? Would it make sense to call pthread_sigmask once per call to builtin_read? Honestly, I don't really understand why SIGCHLD needs to be blocked.
Honestly I don't understand why any signals need to be blocked there. That's why I've been afraid to touch it!
Regarding SIGCHLD specifically, blocking that may have been necessary in the past, but the SIGCHLD handler is now completely trivial so it shouldn't be necessary to block.
I'm OK with replacing that call to read_blocked, and maybe all such calls, with read_loop (which doesn't block signals) and seeing if anything goes wrong, but not for fish 2.2.
When I profiled it a lot of it was not the one byte buffer size, but instead manipulating the signal mask for every byte read. I think this can be greatly simplified.
I used the macOS iprofiler command to trace the syscalls of the simple fish script documented in the original comment for 60 seconds. I then used the Instruments app to read the trace and export it to a CSV file that I then massaged with a python program I threw together. Here are the first few lines of the report:
Notice that the pthread_sigmask() calls account for approximately 1/3 of the total time. I'm willing to bet that all, or nearly all, of those calls can be eliminated.
I reduced the number of paths to the first 100,000 from find $HOME.
I then timed each shell five times and chose the best elapsed time of
the five runs:
git head: 32.05 real 12.23 user 19.90 sys
w/o pthread_setmask(): 24.13 real 9.90 user 14.32 sys
bash: 3.84 real 2.20 user 1.63 sys
So my first attempt at improving the performance of the benchmark in this
issue reduces the elapsed time 25% (user time 19%, sys time 28%). This
is still considerably slower than bash as you can see above (now only 6x
slower rather than 8x). But it's a big step in the right direction.
Using iprofile => Instruments.app export as CSV => summarize data shows
that fish is considerably less efficient in its I/O. Specifically, it makes
a huge number of read() calls to requesting only one byte from the file
containing the path names. Bash reads the same data in chunks of 128 bytes.
Consider these two syscall profiles for the first 60 seconds of run time of
each shell. The fish profile is with my pthread_sigmask() elimination.
Notice that in the same interval bash manages to do more than 2x times as
many stat() calls. Fish spends most of its time reading one byte at a
time from stdin while that is inconsequential in the bash profile. And that
appears to be the most important reason for the difference in performance
between the two shells. Obviously the forks done by fish are also a factor
and itself interesting since there shouldn't be any need to call fork()
for this simple script:
I've implemented a chunked read implementation. In the table below the results labeled "w/perf improvements" includes both the reduced signal manipulation improvements plus the chunked read enhancement. That yields a 72% reduction in run time. We're now only 2.4x slower than bash versus 8.3x slower with the git head version of fish. Not great but a damn sight better. I'd be satisfied if we were within a factor of 2x.
git head: 32.05 real 12.23 user 19.90 sys
w/o pthread_setmask(): 24.13 real 9.90 user 14.32 sys
w/perf improvements: 9.03 real 6.26 user 2.81 sys
bash: 3.84 real 2.20 user 1.63 sys
Reacted by David Adam, Adam Porter, Dilawar Singh and Ron Wolf
Reacted by Fabian Boehm, Adam Porter, Dennis Ideler, Dilawar Singh and Ron Wolf
The fork() calls are, according to fish -d2 due to
<2> fish: Fork #1, pid 23676: internal builtin for 'echo'
I have absolutely no idea why the main thread would fork to execute a builtin command. If we got rid of that fork fish would be reasonably close to bash on this benchmark.
Running the benchmark under fish -d5 shows the reason for the fork has to do with whether or not it produces output:
<3> fish: Skipping fork: no output for internal builtin 'read'
<3> fish: Set status of read dir to 0 using short circuit
<3> fish: Job is constructed
<4> fish: Continue job 2, gid 0 (read dir), COMPLETED, NON-INTERACTIVE
<4> fish: Exec job 'test -d $dir' with id 2
<3> fish: Skipping fork: no output for internal builtin 'test'
<3> fish: Set status of test -d $dir to 0 using short circuit
<3> fish: Job is constructed
<4> fish: Continue job 2, gid 0 (test -d $dir), COMPLETED, NON-INTERACTIVE
<4> fish: Exec job 'and echo $dir' with id 2
<2> fish: Fork #10, pid 23966: internal builtin for 'echo'
<3> fish: Job is constructed
<4> fish: Continue job 2, gid 23956 (and echo $dir), UNCOMPLETED, NON-INTERACTIVE
This is another manifestation of issue #1396: fish buffering the output of not just functions but blocks in general. Fixing that is far outside the scope of this issue.
fish probably forks echo's output because it's got a redirection. There's some complicated logic here (see redirection_is_to_real_file and its uses) and I suspect it's not actually quite right - there may be fd redirections that make it do the wrong thing. Not forking is hard.
while read dir
test -d $dir; and echo $dir
end < data > onlydirs.fish.out
I still don't see why that needs to fork. The relevant comment in src/exec.cpp says we have to fork so that the file will be truncated even if the block produces no output. But that seems rather silly since the truncation should have been done when the redirection was setup. I'm guessing that if I look at the code more closely I'll see that it is deferring the opening of the file for some reason. In any event I think that's a problem for another day. Specifically, after the code in that module has been refactored to eliminate most of the oclint complexity warnings.
Right now I'd simply like as many people to build and run a fish with this patch as possible. Even if just for a few minutes or hours. I'm nervous that the change to how signals are managed might only exhibit problems in the face of extensive use of the trap command, --on-event functions, or similar mechanisms that I'm unlikely to exercise (much) on my systems.
I'm going to close this because the two most significant improvements to the scenario discussed in this issue have been merged. Note that one of the improvements is gated on the variable FISH_NO_SIGNAL_BLOCK being defined so that we can gain confidence it does not introduce any regressions without causing problems for users willing to build from source but not willing to use possibly risky behavior. Hopefully we'll have enough confidence that aspect of the change is correct it can become the default behavior for the 2.6.0 release.
One thing I noticed is that ctrl-C is less reliable with FISH_NO_SIGNAL_BLOCK set, for this code: while true ; echo derp > /dev/null; end . It's always been a little flaky but this amplifies it. Time to track it down I guess.
Activity
ridiculousfish commentedon Apr 2, 2015
I would expect them to take equal amounts of total time, except that fish will fully buffer the output (#1396) so you won't see anything until the command is complete. Does the fish version take more total time, or is it just more time before the first output is printed?
alphapapa commentedon Apr 2, 2015
Thanks for the quick response.
I'm not noticing the buffering you mentioned--output from Fish seems constant and gradual--but it seems that fish is simply taking a lot longer to do it, both in real time and in CPU time, and is using about 10 times as much memory as Bash.
It's not a big deal really, because I still write most scripts in Bash. The situation here was that I was writing some functions to quickly find directories with find/locate, Percol, and Fish, and I ended up having to pipe through Bash instead of using Fish functions because Fish was so much slower (about 3 times slower wall-time here). (Well, I actually ended up piping through Python scripts, because that was even faster, but the point here is the difference between Bash and Fish. :)
Thanks.
ridiculousfish commentedon Apr 2, 2015
Thanks, that's very useful. Looking at a sample, we're spending like 25% of our time in
pthread_sigmask- very surprising.ridiculousfish commentedon Apr 2, 2015
Ha ha ha, that's because
builtin_readreads one byte at a time in the non-interactive case. It is that way in fish 1.x too.snnw commentedon Apr 9, 2015
Hmm, it looks like reading multiple bytes is not trivial. Challenge accepted, though.
I was thinking, read N bytes at a time, process up to
\n,\0, oreof, thenlseekback to the start of the next line. However, we can't seek is stdin is a pipe. Can we just reuse the buffer in that case, and hope nobody notices that we've bitten off more stdin than we can chew? Sounds dangerous...I wonder how Bash does this.
snnw commentedon Apr 9, 2015
Actually, Bash is reading one character at a time as well, since this is a pipe (and it can't actually do buffered IO). @ridiculousfish something else must be going on..
ridiculousfish commentedon Apr 9, 2015
I'm sure you're right, probably many things are going on :) But this is definitely one of them. For every byte read, fish blocks and then unblocks signals. See read_blocked. That pthread_sigmask call is consuming 25% of the time.
snnw commentedon Apr 10, 2015
We're only blocking
SIGCHLD, right? Would it make sense to callpthread_sigmaskonce per call tobuiltin_read? Honestly, I don't really understand whySIGCHLDneeds to be blocked.ridiculousfish commentedon Apr 10, 2015
Honestly I don't understand why any signals need to be blocked there. That's why I've been afraid to touch it!
Regarding SIGCHLD specifically, blocking that may have been necessary in the past, but the SIGCHLD handler is now completely trivial so it shouldn't be necessary to block.
I'm OK with replacing that call to
read_blocked, and maybe all such calls, withread_loop(which doesn't block signals) and seeing if anything goes wrong, but not for fish 2.2.ridiculousfish commentedon Jul 27, 2015
I want to revisit this one.
pickfire commentedon Jul 27, 2015
Does fish have the capability of reading multiple character at a time? Will it be in the future?
If
fishandbashis reading one character at the same time, why isbasha lot faster thanfish?ridiculousfish commentedon Jul 27, 2015
When I profiled it a lot of it was not the one byte buffer size, but instead manipulating the signal mask for every byte read. I think this can be greatly simplified.
22 remaining items
krader1961 commentedon Dec 31, 2016
I used the macOS
iprofilercommand to trace the syscalls of the simple fish script documented in the original comment for 60 seconds. I then used theInstrumentsapp to read the trace and export it to a CSV file that I then massaged with a python program I threw together. Here are the first few lines of the report:Notice that the
pthread_sigmask()calls account for approximately 1/3 of the total time. I'm willing to bet that all, or nearly all, of those calls can be eliminated.krader1961 commentedon Jan 1, 2017
I reduced the number of paths to the first 100,000 from
find $HOME.I then timed each shell five times and chose the best elapsed time of
the five runs:
So my first attempt at improving the performance of the benchmark in this
issue reduces the elapsed time 25% (user time 19%, sys time 28%). This
is still considerably slower than bash as you can see above (now only 6x
slower rather than 8x). But it's a big step in the right direction.
Using
iprofile=> Instruments.app export as CSV => summarize data showsthat fish is considerably less efficient in its I/O. Specifically, it makes
a huge number of
read()calls to requesting only one byte from the filecontaining the path names. Bash reads the same data in chunks of 128 bytes.
Consider these two syscall profiles for the first 60 seconds of run time of
each shell. The fish profile is with my
pthread_sigmask()elimination.Top ten syscalls, by elapsed time, for bash:
Top ten syscalls, by elapsed time, for my improved fish:
Notice that in the same interval bash manages to do more than 2x times as
many
stat()calls. Fish spends most of its time reading one byte at atime from stdin while that is inconsequential in the bash profile. And that
appears to be the most important reason for the difference in performance
between the two shells. Obviously the forks done by fish are also a factor
and itself interesting since there shouldn't be any need to call
fork()for this simple script:
krader1961 commentedon Jan 2, 2017
I've implemented a chunked read implementation. In the table below the results labeled "w/perf improvements" includes both the reduced signal manipulation improvements plus the chunked read enhancement. That yields a 72% reduction in run time. We're now only 2.4x slower than bash versus 8.3x slower with the git head version of fish. Not great but a damn sight better. I'd be satisfied if we were within a factor of 2x.
krader1961 commentedon Jan 2, 2017
The instrumented syscall summary for the fish version labeled "w/perf improvements" in my previous comment looks like this:
TBD is where the
fork()calls are coming from given that the benchmark does not require any external commands.krader1961 commentedon Jan 2, 2017
The
fork()calls are, according tofish -d2due toI have absolutely no idea why the main thread would fork to execute a builtin command. If we got rid of that fork fish would be reasonably close to bash on this benchmark.
Running the benchmark under
fish -d5shows the reason for the fork has to do with whether or not it produces output:This is another manifestation of issue #1396: fish buffering the output of not just functions but blocks in general. Fixing that is far outside the scope of this issue.
ridiculousfish commentedon Jan 3, 2017
fish probably forks
echo's output because it's got a redirection. There's some complicated logic here (seeredirection_is_to_real_fileand its uses) and I suspect it's not actually quite right - there may be fd redirections that make it do the wrong thing. Not forking is hard.krader1961 commentedon Jan 3, 2017
Yes, the actual benchmark is doing
I still don't see why that needs to fork. The relevant comment in src/exec.cpp says we have to fork so that the file will be truncated even if the block produces no output. But that seems rather silly since the truncation should have been done when the redirection was setup. I'm guessing that if I look at the code more closely I'll see that it is deferring the opening of the file for some reason. In any event I think that's a problem for another day. Specifically, after the code in that module has been refactored to eliminate most of the oclint complexity warnings.
Right now I'd simply like as many people to build and run a fish with this patch as possible. Even if just for a few minutes or hours. I'm nervous that the change to how signals are managed might only exhibit problems in the face of extensive use of the
trapcommand,--on-eventfunctions, or similar mechanisms that I'm unlikely to exercise (much) on my systems.krader1961 commentedon Jan 15, 2017
I'm going to close this because the two most significant improvements to the scenario discussed in this issue have been merged. Note that one of the improvements is gated on the variable
FISH_NO_SIGNAL_BLOCKbeing defined so that we can gain confidence it does not introduce any regressions without causing problems for users willing to build from source but not willing to use possibly risky behavior. Hopefully we'll have enough confidence that aspect of the change is correct it can become the default behavior for the 2.6.0 release.ridiculousfish commentedon Jan 21, 2017
One thing I noticed is that ctrl-C is less reliable with
FISH_NO_SIGNAL_BLOCKset, for this code:while true ; echo derp > /dev/null; end. It's always been a little flaky but this amplifies it. Time to track it down I guess.alphapapa commentedon Jan 28, 2017
Kurtis, fantastic work on this, thanks so much. If I have time, I'll see if I can test with the env var set as you described.