Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

Improve performance of backtrace filtering

#66

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.

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

State: resolved
Milestone: none
Assigned to: James Mead James Mead

Activity

  1. Nikolai Weibull
    Nikolai Weibull

    Here’s a patch that should work.

    March 22nd, 2010 @ 11:03 AM

  2. James Mead
    James Mead
    • 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.

    November 26th, 2010 @ 11:41 AM

  3. James Mead
    James Mead
    • State changed from open to resolved

    November 26th, 2010 @ 11:41 AM

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.