Merge pull request #37348 from jhawthorn/backtrace_shared_strings

Avoid string allocations in BacktraceCleaner
This commit is contained in:
John Hawthorn 2019-10-03 14:06:43 -07:00 committed by GitHub
commit 804a4c144e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 7 deletions

View File

@ -4,18 +4,25 @@ require "active_support/backtrace_cleaner"
module Rails
class BacktraceCleaner < ActiveSupport::BacktraceCleaner
APP_DIRS_PATTERN = /^\/?(app|config|lib|test|\(\w*\))/
APP_DIRS_PATTERN = /\A\/?(?:app|config|lib|test|\(\w*\))/
RENDER_TEMPLATE_PATTERN = /:in `.*_\w+_{2,3}\d+_\d+'/
EMPTY_STRING = ""
SLASH = "/"
DOT_SLASH = "./"
def initialize
super
@root = "#{Rails.root}/"
add_filter { |line| line.sub(@root, EMPTY_STRING) }
add_filter { |line| line.sub(RENDER_TEMPLATE_PATTERN, EMPTY_STRING) }
add_filter { |line| line.sub(DOT_SLASH, SLASH) } # for tests
add_filter do |line|
line.start_with?(@root) ? line.from(@root.size) : line
end
add_filter do |line|
if RENDER_TEMPLATE_PATTERN.match?(line)
line.sub(RENDER_TEMPLATE_PATTERN, "")
else
line
end
end
add_filter do |line|
line.start_with?("./") ? line.from(1) : line
end
add_silencer { |line| !APP_DIRS_PATTERN.match?(line) }
end
end