Use a RAM Disk for Rails Temp Folder
Back in the days of the Commodore Amiga, users had access to an ever-present RAM Disk. The file read/write speed rivaled any disk drive at the time by magnitudes. The RAM Disk made it possible to work file data at the top speed of the computer and was only limited by the amount of RAM in the system; back then a maximum size of 128MB.
Today's computers commonly have enough RAM to hold an entire DVD's worth of data and for power users, enough capacity to fill several Blu-Ray DVDs. Depending on the speed of your system's disk and your memory bus speed, you can expect speeds of 4 to 12 times faster throughput and ZERO seek time.
With this in mind, I decided to move my Rails' tmp folder to a RAM Disk to see if there was a noticable benefit during development. THERE WAS! rails server
started faster. Making changes to asset files recompiled at a fraction of their former speed. Any Rails caching was automatically in RAM without the use of Memcached. Just browsing my works-in-progress felt faster than before.
Follow these four steps to leverage a RAM Disk in your application:
From a console, you can manually create a 1GB RAM Disk on OS X using
diskutil erasevolume HFS+ 'RAM Disk' `hdiutil attach -nomount ram://2048000`
or download Florian Bogner's free RAM Disk Creator app from https://bogner.sh/2012/12/os-x-create-a-ram-disk-the-easy-way.Next remove the tmp folder from your Rails app
rm -rf tmp
.Create a folder in the RAM Disk (I used the same name as my app's folder)
mkdir -p '/Volumes/RAM Disk/rails_app/tmp'
.And, finally, create a symlink to the tmp folder on the RAM Disk
ln -s '/Volumes/RAM Disk/rails_app/tmp' tmp
.
Since the volatile nature of a RAM Disk requires recreating the folder structure after a reboot, I recommend modifying the config/application.rb
and appending this code snippet directly after the require 'rails/all'
line to ensure that the tmp folder is present.
if Rails.env.development?
basepath = File.expand_path('../../.', __FILE__)
basename = basepath.split('/').last
tmp_path = "/Volumes/RAM Disk/#{basename}/tmp"
FileUtils.mkdir_p tmp_path
if File.directory?("#{basepath}/tmp") && !File.symlink?("#{basepath}/tmp")
FileUtils.rmdir("#{basepath}/tmp")
end
FileUtils.ln_sf("/Volumes/RAM Disk/#{basename}/tmp", "#{basepath}/tmp")
end
Using Automator or launchd, you can add the diskutil
command as an Application > Run Shell Script
action and set the automator to start as a Login Item
so that the RAM Disk is always present at startup - just like in the days of the Amiga.
You can test the performance of your RAM Disk vs your hard drive using Blackmagic Disk Speed Test https://itunes.apple.com/us/app/blackmagic-disk-speed-test/id425264550.
NOTE: Any program that uses a "scratch disk", can leverage the speed of a RAM Disk including Adobe Photoshop.