Reduce file watching overhead from Rails #555
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I noticed that we were spending a lot of time doing file IO operations coming from the listen gem. After some investigation, these appear to be coming from Rails' own file watching mechanisms, which spawn threads that will check if views are being modified to trigger rendering or reloading in the background.
We currently don't have any functionality that depends on views being rendered. Additionally, the LSP already watches files, so even if we wanted to trigger view rendering, the language server would be in a better position to dictate which views to reload.
This PR shuts down view file watching, which eliminates a good chunk of overhead that we were seeing in the Tapioca add-on.
Approach
I initially tried setting
config.file_watcher
to a no-op implementation, but that doesn't work. The reason is because, by the time we start runningserver.rb
, Rails has already registered the file watchers and its callbacks, so changing the config won't have any real effect.The approach I took is to clear the file system hooks array from the Action View path registry. Those hooks are the ones that build the file watchers, spawning listen threads. By clearing that array, I can see the overhead disappear in my profiles.