• Stars
    star
    2,614
  • Rank 16,773 (Top 0.4 %)
  • Language
    Ruby
  • License
    MIT License
  • Created about 7 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Boot large Ruby/Rails apps faster

Bootsnap Actions Status

Bootsnap is a library that plugs into Ruby, with optional support for YAML and JSON, to optimize and cache expensive computations. See How Does This Work.

Performance

  • Discourse reports a boot time reduction of approximately 50%, from roughly 6 to 3 seconds on one machine;
  • One of our smaller internal apps also sees a reduction of 50%, from 3.6 to 1.8 seconds;
  • The core Shopify platform -- a rather large monolithic application -- boots about 75% faster, dropping from around 25s to 6.5s.
  • In Shopify core (a large app), about 25% of this gain can be attributed to compile_cache_* features; 75% to path caching. This is fairly representative.

Usage

This gem works on macOS and Linux.

Add bootsnap to your Gemfile:

gem 'bootsnap', require: false

If you are using Rails, add this to config/boot.rb immediately after require 'bundler/setup':

require 'bootsnap/setup'

Note that bootsnap writes to tmp/cache (or the path specified by ENV['BOOTSNAP_CACHE_DIR']), and that directory must be writable. Rails will fail to boot if it is not. If this is unacceptable (e.g. you are running in a read-only container and unwilling to mount in a writable tmpdir), you should remove this line or wrap it in a conditional.

Note also that bootsnap will never clean up its own cache: this is left up to you. Depending on your deployment strategy, you may need to periodically purge tmp/cache/bootsnap*. If you notice deploys getting progressively slower, this is almost certainly the cause.

It's technically possible to simply specify gem 'bootsnap', require: 'bootsnap/setup', but it's important to load Bootsnap as early as possible to get maximum performance improvement.

You can see how this require works here.

If you are not using Rails, or if you are but want more control over things, add this to your application setup immediately after require 'bundler/setup' (i.e. as early as possible: the sooner this is loaded, the sooner it can start optimizing things)

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.
)

Protip: You can replace require 'bootsnap' with BootLib::Require.from_gem('bootsnap', 'bootsnap') using this trick. This will help optimize boot time further if you have an extremely large $LOAD_PATH.

Note: Bootsnap and Spring are orthogonal tools. While Bootsnap speeds up the loading of individual source files, Spring keeps a copy of a pre-booted Rails process on hand to completely skip parts of the boot process the next time it's needed. The two tools work well together.

Environment variables

require 'bootsnap/setup' behavior can be changed using environment variables:

  • BOOTSNAP_CACHE_DIR allows to define the cache location.
  • DISABLE_BOOTSNAP allows to entirely disable bootsnap.
  • DISABLE_BOOTSNAP_LOAD_PATH_CACHE allows to disable load path caching.
  • DISABLE_BOOTSNAP_COMPILE_CACHE allows to disable ISeq and YAML caches.
  • BOOTSNAP_READONLY configure bootsnap to not update the cache on miss or stale entries.
  • BOOTSNAP_LOG configure bootsnap to log all caches misses to STDERR.
  • BOOTSNAP_STATS log hit rate statistics on exit. Can't be used if BOOTSNAP_LOG is enabled.
  • BOOTSNAP_IGNORE_DIRECTORIES a comma separated list of directories that shouldn't be scanned. Useful when you have large directories of non-ruby files inside $LOAD_PATH. It defaults to ignore any directory named node_modules.

Environments

All Bootsnap features are enabled in development, test, production, and all other environments according to the configuration in the setup. At Shopify, we use this gem safely in all environments without issue.

If you would like to disable any feature for a certain environment, we suggest changing the configuration to take into account the appropriate ENV var or configuration according to your needs.

Instrumentation

Bootsnap cache misses can be monitored though a callback:

Bootsnap.instrumentation = ->(event, path) { puts "#{event} #{path}" }

event is either :hit, :miss, :stale or :revalidated. You can also call Bootsnap.log! as a shortcut to log all events to STDERR.

To turn instrumentation back off you can set it to nil:

Bootsnap.instrumentation = nil

How does this work?

Bootsnap optimizes methods to cache results of expensive computations, and can be grouped into two broad categories:

  • Path Pre-Scanning
    • Kernel#require and Kernel#load are modified to eliminate $LOAD_PATH scans.
  • Compilation caching
    • RubyVM::InstructionSequence.load_iseq is implemented to cache the result of ruby bytecode compilation.
    • YAML.load_file is modified to cache the result of loading a YAML object in MessagePack format (or Marshal, if the message uses types unsupported by MessagePack).
    • JSON.load_file is modified to cache the result of loading a JSON object in MessagePack format

Path Pre-Scanning

(This work is a minor evolution of bootscale).

Upon initialization of bootsnap or modification of the path (e.g. $LOAD_PATH), Bootsnap::LoadPathCache will fetch a list of requirable entries from a cache, or, if necessary, perform a full scan and cache the result.

Later, when we run (e.g.) require 'foo', ruby would iterate through every item on our $LOAD_PATH ['x', 'y', ...], looking for x/foo.rb, y/foo.rb, and so on. Bootsnap instead looks at all the cached requirables for each $LOAD_PATH entry and substitutes the full expanded path of the match ruby would have eventually chosen.

If you look at the syscalls generated by this behaviour, the net effect is that what would previously look like this:

open  x/foo.rb # (fail)
# (imagine this with 500 $LOAD_PATH entries instead of two)
open  y/foo.rb # (success)
close y/foo.rb
open  y/foo.rb
...

becomes this:

open y/foo.rb
...

The following diagram flowcharts the overrides that make the *_path_cache features work.

Flowchart explaining Bootsnap

Bootsnap classifies path entries into two categories: stable and volatile. Volatile entries are scanned each time the application boots, and their caches are only valid for 30 seconds. Stable entries do not expire -- once their contents has been scanned, it is assumed to never change.

The only directories considered "stable" are things under the Ruby install prefix (RbConfig::CONFIG['prefix'], e.g. /usr/local/ruby or ~/.rubies/x.y.z), and things under the Gem.path (e.g. ~/.gem/ruby/x.y.z) or Bundler.bundle_path. Everything else is considered "volatile".

In addition to the Bootsnap::LoadPathCache::Cache source, this diagram may help clarify how entry resolution works:

How path searching works

It's also important to note how expensive LoadErrors can be. If ruby invokes require 'something', but that file isn't on $LOAD_PATH, it takes 2 * $LOAD_PATH.length filesystem accesses to determine that. Bootsnap caches this result too, raising a LoadError without touching the filesystem at all.

Compilation Caching

(A more readable implementation of this concept can be found in yomikomu).

Ruby has complex grammar and parsing it is not a particularly cheap operation. Since 1.9, Ruby has translated ruby source to an internal bytecode format, which is then executed by the Ruby VM. Since 2.3.0, Ruby exposes an API that allows caching that bytecode. This allows us to bypass the relatively-expensive compilation step on subsequent loads of the same file.

We also noticed that we spend a lot of time loading YAML and JSON documents during our application boot, and that MessagePack and Marshal are much faster at deserialization than YAML and JSON, even with a fast implementation. We use the same strategy of compilation caching for YAML and JSON documents, with the equivalent of Ruby's "bytecode" format being a MessagePack document (or, in the case of YAML documents with types unsupported by MessagePack, a Marshal stream).

These compilation results are stored in a cache directory, with filenames generated by taking a hash of the full expanded path of the input file (FNV1a-64).

Whereas before, the sequence of syscalls generated to require a file would look like:

open    /c/foo.rb -> m
fstat64 m
close   m
open    /c/foo.rb -> o
fstat64 o
fstat64 o
read    o
read    o
...
close   o

With bootsnap, we get:

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

This may look worse at a glance, but underlies a large performance difference.

(The first three syscalls in both listings -- open, fstat64, close -- are not inherently useful. This ruby patch optimizes them out when coupled with bootsnap.)

Bootsnap writes a cache file containing a 64 byte header followed by the cache contents. The header is a cache key including several fields:

  • version, hardcoded in bootsnap. Essentially a schema version;
  • ruby_platform, A hash of RUBY_PLATFORM (e.g. x86_64-linux-gnu) variable.
  • compile_option, which changes with RubyVM::InstructionSequence.compile_option does;
  • ruby_revision, A hash of RUBY_REVISION, the exact version of Ruby;
  • size, the size of the source file;
  • mtime, the last-modification timestamp of the source file when it was compiled; and
  • data_size, the number of bytes following the header, which we need to read it into a buffer.

If the key is valid, the result is loaded from the value. Otherwise, it is regenerated and clobbers the current cache.

Putting it all together

Imagine we have this file structure:

/
├── a
├── b
└── c
    └── foo.rb

And this $LOAD_PATH:

["/a", "/b", "/c"]

When we call require 'foo' without bootsnap, Ruby would generate this sequence of syscalls:

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

With bootsnap, we get:

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

If we call require 'nope' without bootsnap, we get:

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

...and if we call require 'nope' with bootsnap, we get...

# (nothing!)

Precompilation

In development environments the bootsnap compilation cache is generated on the fly when source files are loaded. But in production environments, such as docker images, you might need to precompile the cache.

To do so you can use the bootsnap precompile command.

Example:

$ bundle exec bootsnap precompile --gemfile app/ lib/

When not to use Bootsnap

Alternative engines: Bootsnap is pretty reliant on MRI features, and parts are disabled entirely on alternative ruby engines.

Non-local filesystems: Bootsnap depends on tmp/cache (or whatever you set its cache directory to) being on a relatively fast filesystem. If you put it on a network mount, bootsnap is very likely to slow your application down quite a lot.

More Repositories

1

draggable

The JavaScript Drag & Drop library your grandparents warned you about.
JavaScript
17,454
star
2

dashing

The exceptionally handsome dashboard framework in Ruby and Coffeescript.
JavaScript
11,025
star
3

liquid

Liquid markup language. Safe, customer facing template language for flexible web apps.
Ruby
10,419
star
4

toxiproxy

⏰ 🔥 A TCP proxy to simulate network and system conditions for chaos and resiliency testing
Go
9,412
star
5

react-native-skia

High-performance React Native Graphics using Skia
TypeScript
6,392
star
6

polaris

Shopify’s design system to help us work together to build a great experience for all of our merchants.
TypeScript
5,352
star
7

flash-list

A better list for React Native
TypeScript
4,536
star
8

hydrogen-v1

React-based framework for building dynamic, Shopify-powered custom storefronts.
TypeScript
3,760
star
9

go-lua

A Lua VM in Go
Go
2,773
star
10

graphql-design-tutorial

2,335
star
11

restyle

A type-enforced system for building UI components in React Native with TypeScript.
TypeScript
2,331
star
12

dawn

Shopify's first source available reference theme, with Online Store 2.0 features and performance built-in.
Liquid
2,279
star
13

identity_cache

IdentityCache is a blob level caching solution to plug into Active Record. Don't #find, #fetch!
Ruby
1,874
star
14

shopify_app

A Rails Engine for building Shopify Apps
Ruby
1,649
star
15

kubeaudit

kubeaudit helps you audit your Kubernetes clusters against common security controls
Go
1,624
star
16

quilt

A loosely related set of packages for JavaScript/TypeScript projects at Shopify
TypeScript
1,570
star
17

graphql-batch

A query batching executor for the graphql gem
Ruby
1,388
star
18

shipit-engine

Deployment coordination
Ruby
1,382
star
19

packwerk

Good things come in small packages.
Ruby
1,346
star
20

krane

A command-line tool that helps you ship changes to a Kubernetes namespace and understand the result
Ruby
1,309
star
21

semian

🐒 Resiliency toolkit for Ruby for failing fast
Ruby
1,286
star
22

slate

Slate is a toolkit for developing Shopify themes. It's designed to assist your workflow and speed up the process of developing, testing, and deploying themes.
JavaScript
1,281
star
23

ejson

EJSON is a small library to manage encrypted secrets using asymmetric encryption.
Go
1,246
star
24

superdb

The Super Debugger, a realtime wireless debugger for iOS
Objective-C
1,158
star
25

shopify_python_api

ShopifyAPI library allows Python developers to programmatically access the admin section of stores
Python
1,072
star
26

storefront-api-examples

Example custom storefront applications built on Shopify's Storefront API
JavaScript
1,069
star
27

themekit

Shopify theme development command line tool.
Go
1,068
star
28

Timber

The ultimate Shopify theme framework, built by Shopify.
Liquid
992
star
29

shopify-cli

Shopify CLI helps you build against the Shopify platform faster.
Ruby
987
star
30

shopify-api-ruby

ShopifyAPI is a lightweight gem for accessing the Shopify admin REST and GraphQL web services.
Ruby
982
star
31

hydrogen

Hydrogen is Shopify’s stack for headless commerce. It provides a set of tools, utilities, and best-in-class examples for building dynamic and performant commerce applications. Hydrogen is designed to dovetail with Remix, Shopify’s full stack web framework, but it also provides a React library portable to other supporting frameworks. Demo store 👇🏼
TypeScript
966
star
32

js-buy-sdk

The JS Buy SDK is a lightweight library that allows you to build ecommerce into any website. It is based on Shopify's API and provides the ability to retrieve products and collections from your shop, add products to a cart, and checkout.
JavaScript
932
star
33

job-iteration

Makes your background jobs interruptible and resumable by design.
Ruby
907
star
34

cli-ui

Terminal user interface library
Ruby
869
star
35

ruby-lsp

An opinionated language server for Ruby
Ruby
851
star
36

react-native-performance

Performance monitoring for React Native apps
TypeScript
843
star
37

active_shipping

ActiveShipping is a simple shipping abstraction library extracted from Shopify
Ruby
809
star
38

shopify-api-js

Shopify Admin API Library for Node. Accelerate development with support for authentication, graphql proxy, webhooks
TypeScript
765
star
39

maintenance_tasks

A Rails engine for queueing and managing data migrations.
Ruby
705
star
40

shopify-app-template-node

JavaScript
701
star
41

remote-ui

TypeScript
701
star
42

shopify_theme

A console tool for interacting with Shopify Theme Assets.
Ruby
640
star
43

tapioca

The swiss army knife of RBI generation
Ruby
636
star
44

pitchfork

Ruby
630
star
45

ghostferry

The swiss army knife of live data migrations
Go
596
star
46

yjit

Optimizing JIT compiler built inside CRuby
593
star
47

erb-lint

Lint your ERB or HTML files
Ruby
565
star
48

statsd-instrument

A StatsD client for Ruby apps. Provides metaprogramming methods to inject StatsD instrumentation into your code.
Ruby
546
star
49

shopify.github.com

A collection of the open source projects by Shopify
CSS
505
star
50

theme-scripts

Theme Scripts is a collection of utility libraries which help theme developers with problems unique to Shopify Themes.
JavaScript
470
star
51

livedata-ktx

Kotlin extension for LiveData, chaining like RxJava
Kotlin
467
star
52

starter-theme

The Shopify Themes Team opinionated starting point for new a Slate project
Liquid
459
star
53

ruby-style-guide

Shopify’s Ruby Style Guide
Ruby
446
star
54

shopify-demo-app-node-react

JavaScript
444
star
55

web-configs

Common configurations for building web apps at Shopify
JavaScript
433
star
56

mobile-buy-sdk-ios

Shopify’s Mobile Buy SDK makes it simple to sell physical products inside your mobile app. With a few lines of code, you can connect your app with the Shopify platform and let your users buy your products using Apple Pay or their credit card.
Swift
433
star
57

shopify_django_app

Get a Shopify app up and running with Django and Python Shopify API
Python
425
star
58

deprecation_toolkit

⚒Eliminate deprecations from your codebase ⚒
Ruby
390
star
59

ruby-lsp-rails

A Ruby LSP extension for Rails
Ruby
388
star
60

bootboot

Dualboot your Ruby app made easy
Ruby
374
star
61

FunctionalTableData

Declarative UITableViewDataSource implementation
Swift
365
star
62

shadowenv

reversible directory-local environment variable manipulations
Rust
349
star
63

shopify-node-app

An example app that uses Polaris components and shopify-express
JavaScript
327
star
64

better-html

Better HTML for Rails
Ruby
311
star
65

theme-check

The Ultimate Shopify Theme Linter
Ruby
306
star
66

product-reviews-sample-app

A sample Shopify application that creates and stores product reviews for a store, written in Node.js
JavaScript
300
star
67

tracky

The easiest way to do motion tracking!
Swift
295
star
68

shopify-api-php

PHP
279
star
69

polaris-viz

A collection of React and React native components that compose Shopify's data visualization system
TypeScript
279
star
70

measured

Encapsulate measurements and their units in Ruby.
Ruby
275
star
71

cli

Build apps, themes, and hydrogen storefronts for Shopify
TypeScript
273
star
72

money

Manage money in Shopify with a class that won't lose pennies during division
Ruby
265
star
73

javascript

The home for all things JavaScript at Shopify.
254
star
74

ruvy

Rust
252
star
75

limiter

Simple Ruby rate limiting mechanism.
Ruby
244
star
76

vscode-ruby-lsp

VS Code plugin for connecting with the Ruby LSP
TypeScript
232
star
77

polaris-tokens

Design tokens for Polaris, Shopify’s design system
TypeScript
230
star
78

buy-button-js

BuyButton.js is a highly customizable UI library for adding ecommerce functionality to any website.
JavaScript
230
star
79

android-testify

Add screenshots to your Android tests
Kotlin
225
star
80

turbograft

Hard fork of turbolinks, adding partial page replacement strategies, and utilities.
JavaScript
213
star
81

mobile-buy-sdk-android

Shopify’s Mobile Buy SDK makes it simple to sell physical products inside your mobile app. With a few lines of code, you can connect your app with the Shopify platform and let your users buy your products using their credit card.
Java
202
star
82

spoom

Useful tools for Sorbet enthusiasts
Ruby
192
star
83

graphql-js-client

A Relay compliant GraphQL client.
JavaScript
187
star
84

ruby_memcheck

Use Valgrind memcheck on your native gem without going crazy
Ruby
187
star
85

shopify-app-template-php

PHP
186
star
86

skeleton-theme

A barebones ☠️starter theme with the required files needed to compile with Slate and upload to Shopify.
Liquid
185
star
87

sprockets-commoner

Use Babel in Sprockets to compile JavaScript modules for the browser
Ruby
182
star
88

rotoscope

High-performance logger of Ruby method invocations
Ruby
180
star
89

shopify-app-template-remix

TypeScript
178
star
90

git-chain

Tool to rebase multiple Git branches based on the previous one.
Ruby
176
star
91

verdict

Framework to define and implement A/B tests in your application, and collect data for analysis purposes.
Ruby
176
star
92

hydrogen-react

Reusable components and utilities for building Shopify-powered custom storefronts.
TypeScript
174
star
93

ui-extensions

TypeScript
173
star
94

storefront-api-learning-kit

JavaScript
171
star
95

heap-profiler

Ruby heap profiler
C++
159
star
96

autoload_reloader

Experimental implementation of code reloading using Ruby's autoload
Ruby
158
star
97

app_profiler

Collect performance profiles for your Rails application.
Ruby
157
star
98

graphql-metrics

Extract as much much detail as you want from GraphQL queries, served up from your Ruby app and the graphql gem.
Ruby
157
star
99

active_fulfillment

Active Merchant library for integration with order fulfillment services
Ruby
155
star
100

ci-queue

Distribute tests over many workers using a queue
Ruby
148
star