File handling: comparing exceptions to additional stat() test

I was doing some work profiling the ‘assets:precompile’ Rake task in a
JRuby on Rails app, and I discovered some interesting performance
issues
related to doing things like this:

 begin
   file.actions()
 rescue Errno::ENOENT
   # ignore or do something
 end

instead of this:

 if File.exists?(file)
   file.actions()
 else
   # ignore or do something
 end

I started submitting a few small patches, but already met with pushback
because the issue was perceived to only affect JRuby and assumed
to lower performance in MRI.

I wrote up a simple benchmark testing counting directory entries for
directories that may or may not exist. I would appreciate any feedback
on this specific benchmark and on the general idioms of exception
handlers
vs. explicit test, neither of which are “free”.

http://polycrystal.org/2012/10/25/comparing_ruby_exceptions_to_additional_stat_test.html

Thanks!


Patrick M.

In general, exceptions are a bad choice for flow control.

http://c2.com/cgi/wiki?DontUseExceptionsForFlowControl
talks about the problem across several languages.

http://www.coffeepowered.net/2011/06/17/jruby-performance-exceptions-are-not-flow-control/
is great for tracking down excessive exceptions in JRuby code. My team
has used these techniques several times to speed up our production code.
It hadn’t occurred to me that our slow Rails asset precompilation might
be partly caused by exceptions as flow control.

I’d love to see asset precompilation go faster under JRuby. I’d like to
help promote your proposed changes. Where have you been submitting
patches?

  • Bruce

P.S. I get a 404 for you blog post and don’t see it referenced from the
front page of your blog.

On 26.10.2012 05:20, Bruce A. wrote:

I’d love to see asset precompilation go faster under JRuby. I’d like
to
help promote your proposed changes. Where have you been submitting
patches?

So far just this one Test for directory existence explicitly to avoid JRuby performance issue by pmahoney · Pull Request #21 · sstephenson/hike · GitHub

I’m still working on patches to stdlib/fileutils, but got a little
discouraged.

I’m hoping now that I have data that shows MRI suffers a similar
performance
hit (even if of a smaller magnitude) with the exceptions vs. extra
stat(),
there will be less objection.

P.S. I get a 404 for you blog post and don’t see it referenced from
the
front page of your blog.

How embarrassing! I guess I didn’t click “publish”. Should be working
now.


Patrick M.

I replied with my support and posted an additional set of numbers
showing GC counts with the various mechanisms:

Clearly a big GC win here.

  • Charlie

Patrick,

Do you have any statistics on how many exists/!exists happen in your
typical assets:precompile. Based on the fact that a exists is 9x
faster on a miss and only 10% slower on a hit for MRI, it should be
fairly easy with typical usage statistics to show that MRI will
generally always be faster.

That is assuming your assets represent a reasonable workload?

-Tom

PS- The quickness of the resolution is a little disturbing. I
suspect your patch will be a net gain in MRI and a huge one for JRuby.

On Thu, Oct 25, 2012 at 10:45 PM, Patrick M. [email protected]
wrote:

instead of this:

Patrick M.


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


blog: http://blog.enebo.com twitter: tom_enebo
mail: [email protected]