### Configuring Bootsnap for Non-Rails Applications Source: https://github.com/shopify/bootsnap/blob/main/README.md This snippet provides a comprehensive example of configuring Bootsnap for applications not using Rails, or for those requiring more granular control. It demonstrates setting the cache directory, ignoring specific directories, enabling/disabling various caching features (load path, ISeq, YAML, JSON compilation), and setting read-only mode. ```Ruby require 'bootsnap' env = ENV['RAILS_ENV'] || "development" Bootsnap.setup( cache_dir: 'tmp/cache', # Path to your cache ignore_directories: ['node_modules'], # Directory names to skip. development_mode: env == 'development', # Current working environment, e.g. RACK_ENV, RAILS_ENV, etc load_path_cache: true, # Optimize the LOAD_PATH with a cache compile_cache_iseq: true, # Compile Ruby code into ISeq cache, breaks coverage reporting. compile_cache_yaml: true, # Compile YAML into a cache compile_cache_json: true, # Compile JSON into a cache readonly: true # Use the caches but don't update them on miss or stale entries. ) ``` -------------------------------- ### Installing Ruby Gem Dependencies (Bundler) Source: https://github.com/shopify/bootsnap/blob/main/CONTRIBUTING.md This command uses Bundler to read the project's Gemfile and Gemfile.lock, then installs all specified Ruby gem dependencies. It ensures that the project has all required libraries in the correct versions, preparing the environment for execution or testing. ```Shell bundle install ``` -------------------------------- ### Precompiling Bootsnap Cache Source: https://github.com/shopify/bootsnap/blob/main/README.md This command demonstrates how to precompile the Bootsnap cache in production environments using the `bootsnap precompile` command. It specifies directories (`app/`, `lib/`, `config/`) to be scanned and preprocessed, ensuring faster application startup without on-the-fly compilation. ```Bash $ bundle exec bootsnap precompile --gemfile app/ lib/ config/ ``` -------------------------------- ### Loading Bootsnap in Rails config/boot.rb Source: https://github.com/shopify/bootsnap/blob/main/README.md This snippet demonstrates how to explicitly load Bootsnap in a Rails application's 'config/boot.rb' file, immediately after 'require 'bundler/setup''. This ensures Bootsnap is initialized as early as possible during the boot process to maximize performance gains. ```Ruby require 'bootsnap/setup' ``` -------------------------------- ### Ruby require Syscalls (With Bootsnap) Source: https://github.com/shopify/bootsnap/blob/main/README.md This snippet demonstrates the optimized sequence of system calls when Bootsnap is active. It shows fewer `read` operations and introduces a cache file lookup, significantly reducing the overhead of repeated file parsing and compilation. ```Bash open /c/foo.rb -> n fstat64 n close n open /c/foo.rb -> n fstat64 n open (cache) -> m read m read m close m close n ``` -------------------------------- ### Enabling Bootsnap Instrumentation with a Custom Callback (Ruby) Source: https://github.com/shopify/bootsnap/blob/main/README.md This snippet enables Bootsnap's instrumentation feature by assigning a Proc (lambda) to `Bootsnap.instrumentation`. The provided callback receives `event` (e.g., `:hit`, `:miss`, `:stale`, `:revalidated`) and `path` as arguments, allowing custom logging or monitoring of cache events. This helps in debugging and performance analysis. ```Ruby Bootsnap.instrumentation = ->(event, path) { puts "#{event} #{path}" } ``` -------------------------------- ### Ruby require Path Search (With Bootsnap) Source: https://github.com/shopify/bootsnap/blob/main/README.md This snippet illustrates the system calls when Ruby searches for a file (`foo.rb`) with Bootsnap enabled. Bootsnap optimizes this process, leading to a more direct lookup once the file is found and cached, reducing redundant path searches. ```Bash open /c/foo.rb -> n fstat64 n close n open /c/foo.rb -> n fstat64 n open (cache) -> m read m read m close m close n ``` -------------------------------- ### Enabling Ruby Development Kit (Windows) Source: https://github.com/shopify/bootsnap/blob/main/CONTRIBUTING.md This command, part of the RubyInstaller Development Kit (ridk) on Windows, configures the shell environment by adding necessary development tools like 'make' to the system's PATH. It is a crucial prerequisite for compiling native extensions of Ruby gems. ```Shell ridk enable ``` -------------------------------- ### Ruby require Syscalls (Without Bootsnap) Source: https://github.com/shopify/bootsnap/blob/main/README.md This snippet illustrates the sequence of system calls made by Ruby's `require` method when Bootsnap is not in use. It shows multiple `open`, `fstat64`, `read`, and `close` calls for the same file, indicating redundant operations during file loading. ```Bash open /c/foo.rb -> m fstat64 m close m open /c/foo.rb -> o fstat64 o fstat64 o read o read o ... close o ``` -------------------------------- ### Ruby require Path Search (Without Bootsnap) Source: https://github.com/shopify/bootsnap/blob/main/README.md This snippet shows the system calls involved when Ruby searches for a file (`foo.rb`) across multiple `$LOAD_PATH` directories without Bootsnap. It highlights the repeated `open` and `close` calls for non-existent files in earlier paths before finding the correct one. ```Bash open /a/foo.rb -> -1 open /b/foo.rb -> -1 open /c/foo.rb -> n close n open /c/foo.rb -> m fstat64 m close m open /c/foo.rb -> o fstat64 o fstat64 o read o read o ... close o ``` -------------------------------- ### Ruby require Non-Existent File (With Bootsnap) Source: https://github.com/shopify/bootsnap/blob/main/README.md This snippet shows the system calls when attempting to `require` a non-existent file (`nope`) with Bootsnap enabled. Bootsnap caches the 'not found' status, preventing repeated costly file system lookups for subsequent attempts, resulting in no syscalls after the initial check. ```Bash # (nothing!) ``` -------------------------------- ### Ruby require Non-Existent File (Without Bootsnap) Source: https://github.com/shopify/bootsnap/blob/main/README.md This snippet demonstrates the system calls when Ruby attempts to `require` a non-existent file (`nope`) without Bootsnap. It shows Ruby exhaustively checking all `$LOAD_PATH` directories and common extensions (`.rb`, `.bundle`), resulting in many failed `open` calls. ```Bash open /a/nope.rb -> -1 open /b/nope.rb -> -1 open /c/nope.rb -> -1 open /a/nope.bundle -> -1 open /b/nope.bundle -> -1 open /c/nope.bundle -> -1 ``` -------------------------------- ### Adding Bootsnap to Gemfile (Rails) Source: https://github.com/shopify/bootsnap/blob/main/README.md This snippet shows how to add the Bootsnap gem to a Ruby application's Gemfile. The 'require: false' option prevents Bootsnap from being loaded automatically, allowing for explicit and early loading in 'config/boot.rb' for maximum performance. ```Ruby gem 'bootsnap', require: false ``` -------------------------------- ### Executing Rake Tasks via Bundler (Ruby) Source: https://github.com/shopify/bootsnap/blob/main/CONTRIBUTING.md This command executes a Rake task, typically used for running tests or other build automation, ensuring that the Rake command uses the gem versions specified by Bundler. This is the final step to run the project's test suite in the correct environment. ```Shell bundle exec rake ``` -------------------------------- ### Committing Changes (Git) Source: https://github.com/shopify/bootsnap/blob/main/CONTRIBUTING.md This Git command stages all modified and deleted files, then commits them to the current branch with the provided message. The '-a' flag automatically stages tracked files, and '-m' specifies the commit message, which should clearly describe the changes. ```Shell git commit -am 'Add some feature' ``` -------------------------------- ### Creating a New Feature Branch (Git) Source: https://github.com/shopify/bootsnap/blob/main/CONTRIBUTING.md This Git command creates and immediately switches to a new branch named 'my-new-feature'. It's the recommended first step in the contribution workflow after forking, ensuring development work is isolated and doesn't directly affect the main branch. ```Shell git checkout -b my-new-feature ``` -------------------------------- ### Pushing Changes to Remote (Git) Source: https://github.com/shopify/bootsnap/blob/main/CONTRIBUTING.md This Git command uploads the committed changes from the local 'my-new-feature' branch to the 'origin' remote repository. This makes the local work available on the remote for others to see and for creating a pull request. ```Shell git push origin my-new-feature ``` -------------------------------- ### Disabling Bootsnap Instrumentation (Ruby) Source: https://github.com/shopify/bootsnap/blob/main/README.md This snippet disables Bootsnap's instrumentation by setting `Bootsnap.instrumentation` to `nil`. This stops all event callbacks, effectively turning off the monitoring feature when it's no longer needed, reducing any overhead associated with instrumentation. ```Ruby Bootsnap.instrumentation = nil ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.