Repository navigation
Performance - #3691
Performance#3691krader1961 wants to merge 6 commits into
Conversation
The shell was doing a log of signal blocking/unblocking that hurts performance and can be avoided. This reduced the elapsed time for a simple benchmark by 25%. Partial fix for #2007
Refactor `builtin_read()` to split the code that does the actual reading into separate functions. This introduces the `read_in_chunks()` function but in this change it is just a clone of `read_one_char_at_a_time()`. It will be modified to actually read in chunks in the next change. Partial fix for #2007
Provide a more efficient method for reading lines (or null terminated sequences) when the input is seekable. Another partial fix for #2007
|
As discussed on Gitter, I think holding this until after 2.5.0 is wise. |
|
I'm hoping the lack of feedback means people have applied this change and did not see any problems. If you haven't done so please take a few minutes to test it; even for just a quick test that it doesn't break anything you do regularly with fish. |
|
I've been testing it, and it seems fine. |
|
I'm digging it. |
|
I still plan to review! |
|
A failing test case I found: Now type two lines, followed by control-D. Without the PR, it prints the two lines. With the PR, it doesn't terminate on the first ctrl-D, and seems to drop the second line. (Tested on 10.12.2) |
|
@ridiculousfish: That's because the use of a function means the I have no idea why it's not doing Do we really want to bypass the interactive read behavior just because we're executing the read inside a function which, at the moment, will always get a non-zero fd for The code still needs to have a |
|
I'll also add your example to the unit tests to help prevent regressions. |
| errno = EINTR; | ||
| while (result == -1 && errno == EINTR) { | ||
| result = tcsetpgrp(STDIN_FILENO, j->pgid); | ||
| } |
There was a problem hiding this comment.
I'll take a crack at reducing the EINTR-logic duplication later. For now this is fine.
| sigaction(SIGTSTP, &act, 0); | ||
| sigaction(SIGTTIN, &act, 0); | ||
| sigaction(SIGTTOU, &act, 0); | ||
|
|
There was a problem hiding this comment.
I'm no signal guru - does this do the right thing with the call to killpg(shell_pgid, SIGTTIN); in reader_interactive_init()?
There was a problem hiding this comment.
That code in fish seems to mimic bash's initialize_job_control() which also does a kill (0, SIGTTIN) if the tcgetpgrp() fails. The only reason I'm ignoring SIGTTIN is because it it related to SIGTTOU which needs to be ignored to avoid a pathological failure case when fish writes to a broken tty. However, reading from that broken tty should probably not be protected in the same manner. I'll revert that portion of the change.
| // TODO: Determine if the following three functions will need to be reinstated. For the time being | ||
| // they are #define stubs since they appear to be unnecessary and noticably slow the shell down. | ||
| // Be sure to reinstate the CHECK_BLOCK macro if these functions are reinstated. | ||
|
|
There was a problem hiding this comment.
This makes me pretty nervous, since now signals may be delivered in code that doesn't expect it. I suspect there are a lot of places where we don't handle EINTR, e.g. calls to tcsetattr. Maybe we can develop a test that delivers a signals with some frequency, to give us some confidence that these aren't necessary.
There was a problem hiding this comment.
It makes me nervous as well. However, the existing implementation to block/unblock every signal is clearly akin to firing a shotgun to avoid being bitten by a mosquito. This is why I implemented this by implementing no-op stubs rather than removing the calls to these functions. We can reinstate the original version quickly without having to worry about resolving change conflicts.
|
My big concern here is removing the I would guess/hope that the chunked reads will be a big boost and turn the signal munging into background noise. Do you think it's important for performance to remove the |
|
The elimination of the signal blocking/unblocking is important. It doesn't have as big an impact as implementing chunked reads but eliminating that signal manipulation is still a significant source of improvement. There is no way to do it safely since, as you note, no one understands why the existing code is blocking/unblocking every signal. That behavior is obviously not needed. What is unclear is whether it is necessary to selectively block/unblock a few signals like SIGCHLD. I have no objection to deferring merging the signal management aspect of this PR and proceeding with merging the chunked read aspect. The only reason for eliminating the seemingly unnecessary signal manipulation was the initial, and correct, hypothesis that it measurably affects the performance of fish. |
|
FWIW, I would not be surprised if changing how fish blocks/unblocks signals introduces a regression. However I am inclined to accept that risk in order to learn more about why the original author of this code decided this strategy was needed. At least between minor releases. |
|
I like the idea of getting a handle on our signal situation. An easy thing we could try is a mode (similar to Assuming that idea is at all viable, my preference is to merge the chunked reads now, and plan to merge the |
|
Okay, I'm going to merge this gated on an env var. That will allow the core devs and others to enable it all the time so we can gain confidence it doesn't break anything or learn why the signal blocking was needed. Unless we do that it becomes difficult to know if this change is being used since people are otherwise installing fish based on other commits which won't have the signal management change. Also, the SIGCHLD handler should be installed with the SA_RESTART flag since there is absolutely no reason for it to interrupt a syscall. I strongly suspect, given where signals are being blocked, the lack of that flag on that signal is the primary reason for the crazy blocking/unblocking. There may be other signals, such as SIGWINCH and SIGTERM, which also should have that flag as they should not need to cause an otherwise blocking syscall to return early. |
|
Note that bash uses SA_RESTART for SIGCHLD and SIGTERM with these comments: We definitely want to do the same for SIGCHLD. TBD is whether that's appropriate for SIGTERM given that fish's architecture is quite different. |
|
I've merged this series of changes along with the revisions discussed in previous comments. Please add |
@ridiculousfish, what would that look like? Would |
|
Yep. I think we would have to ensure that SIGUSR1 is not ignored. |
This mostly fixes issue #2007. I've been running this for a few hours on macOS and Ubuntu with no obvious problems. I'll also install it on Cygwin. The chunked read portion of the change should be safe and have no unintended side-effects. The changes to how signals are managed, the first commit in this PR, is a very different matter with significant risk that it could introduce hard to test or reproduce race conditions.