Browse files

Replace flow-control exception with explicit test.

It was noticed while profiling 'assets:precompile' in JRuby that
exception creation was consuming a large portion of time, and
some of that was due to File.atomic_write.

Testing first with File.exists? eliminates the need for an exception
which should be a perfomrance improvement on both JRuby and MRI.
In this case, the stat() isn't even extra overhead, since it is always
  • Loading branch information...
1 parent 2e44dda commit 8d0efcc5757a486084c73054202702a2028cba77 @pmahoney pmahoney committed Oct 27, 2012
Showing with 2 additions and 2 deletions.
  1. +2 −2 activesupport/lib/active_support/core_ext/file/atomic.rb
4 activesupport/lib/active_support/core_ext/file/atomic.rb
@@ -23,10 +23,10 @@ def self.atomic_write(file_name, temp_dir = Dir.tmpdir)
yield temp_file
- begin
+ if File.exists?(file_name)
# Get original file permissions
old_stat = stat(file_name)
- rescue Errno::ENOENT
+ else
# If not possible, probe which are the default permissions in the
# destination directory.
old_stat = probe_stat_in(dirname(file_name))

0 comments on commit 8d0efcc

Please sign in to comment.