#66 ✓resolved
James Mead

Improve performance of backtrace filtering

Reported by James Mead | March 17th, 2010 @ 07:11 PM

Nikolai Weibull posted the following to the Mocha mailing list :-

Mocha::BacktraceFilter creates a new Regexp object for every line of every backtrace it filters. It also calls File.expand_path for each such line.

The Regexp object creation can easily be removed by creating it once in #initialize. File.expand_path might be more difficult to get rid of, but File.expand_path is slow, at least on Windows. Getting rid of all those calls to it would thus save a lot of time. Rewriting BacktraceFilter to that below makes running my 108 tests with Expectations run in less than a third of the time:

  def initialize(lib_directory = LIB_DIRECTORY)
    @lib_directory = Regexp.new('^' + Regexp.escape(lib_directory))
  end

  def filtered(backtrace)
    backtrace.reject { |location| @lib_directory.match(location) }
  end

I realize that this doesn’t filter backtraces correctly if Mocha’s paths are relative. I was, however, hoping that someone with more knowledge in this area would be able to resolve this. Perhaps setting LIB_DIRECTORY = File.dirname(File.dirname(__FILE__)) + File::SEPARATOR is a good way of doing it? Then both @lib_directory and location will both be relative or absolute, depending on how Mocha was required.

Comments and changes to this ticket

  • Nikolai Weibull

    Nikolai Weibull March 22nd, 2010 @ 11:03 AM

    Here’s a patch that should work.

  • James Mead

    James Mead November 26th, 2010 @ 11:41 AM

    • State changed from “new” to “open”

    @nikolai: Apologies for the (very) slow response. I've just been looking at this and thinking about it a bit. I've made the change so that the Regexp instance is now only created once in the BacktraceFilter#initialize method.

    However, I've also realized that (a) this probably won't improve performance because a new BacktraceFilter instance is created for every expectation error; and (b) in any case, any performance improvement will only be apparent for people who have a lot of test failing with expectation errors. This latter case seems like a bit of an edge case, so I'm not going to make the other changes.

    I'm closing this ticket. Thanks, James.

  • James Mead

    James Mead November 26th, 2010 @ 11:41 AM

    • State changed from “open” to “resolved”

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

A mocking & stubbing library for Ruby.

* <a href="http://github.com/floehopper/mocha">GitHub repository</a>
* <a href="http://mocha.rubyforge.org">Documentation</a>
* <a href="http://groups.google.com/group/mocha-developer">Mailing List</a>

People watching this ticket

Attachments

Pages