Mocha::BacktraceFilter creates Regexps unnecessarily and calls File.expand_path frivolously

Skip to first unread message

Nikolai Weibull

Mar 17, 2010, 9:32:39 AM3/17/10
to mocha-developer

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.escape(lib_directory))

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

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.

James Mead

Mar 17, 2010, 3:17:46 PM3/17/10

Hi Nikolai,

That's really helpful - thanks.

I've taken the liberty of creating a ticket [1] on your behalf.

I'm probably not going to have a chance to look at this in the next
week or so, but it'd be great if someone could supply a patch.

Cheers, James.


Reply all
Reply to author
0 new messages