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

2 views
Skip to first unread message

Nikolai Weibull

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

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.

James Mead

unread,
Mar 17, 2010, 3:17:46 PM3/17/10
to mocha-d...@googlegroups.com

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.

[1] http://floehopper.lighthouseapp.com/projects/22289-mocha/tickets/66

Reply all
Reply to author
Forward
0 new messages