Merge pull request #4260 from jamescook/james/replace-certain-gsub-with-tr

Replace certain uses of 'gsub' with 'tr' / 'chomp' for a speed improvement
This commit is contained in:
Guo Xiang Tan 2016-06-13 18:25:38 +08:00 committed by GitHub
commit 95a013784f
9 changed files with 15 additions and 16 deletions

View File

@ -296,13 +296,12 @@ class ApplicationController < ActionController::Base
def fetch_user_from_params(opts=nil) def fetch_user_from_params(opts=nil)
opts ||= {} opts ||= {}
user = if params[:username] user = if params[:username]
username_lower = params[:username].downcase username_lower = params[:username].downcase.chomp('.json')
username_lower.gsub!(/\.json$/, '')
find_opts = { username_lower: username_lower } find_opts = { username_lower: username_lower }
find_opts[:active] = true unless opts[:include_inactive] || current_user.try(:staff?) find_opts[:active] = true unless opts[:include_inactive] || current_user.try(:staff?)
User.find_by(find_opts) User.find_by(find_opts)
elsif params[:external_id] elsif params[:external_id]
external_id = params[:external_id].gsub(/\.json$/, '') external_id = params[:external_id].chomp('.json')
SingleSignOnRecord.find_by(external_id: external_id).try(:user) SingleSignOnRecord.find_by(external_id: external_id).try(:user)
end end
raise Discourse::NotFound if user.blank? raise Discourse::NotFound if user.blank?
@ -335,9 +334,9 @@ class ApplicationController < ActionController::Base
# Rails I18n uses underscores between the locale and the region; the request # Rails I18n uses underscores between the locale and the region; the request
# headers use hyphens. # headers use hyphens.
require 'http_accept_language' unless defined? HttpAcceptLanguage require 'http_accept_language' unless defined? HttpAcceptLanguage
available_locales = I18n.available_locales.map { |locale| locale.to_s.gsub(/_/, '-') } available_locales = I18n.available_locales.map { |locale| locale.to_s.tr('_', '-') }
parser = HttpAcceptLanguage::Parser.new(request.env["HTTP_ACCEPT_LANGUAGE"]) parser = HttpAcceptLanguage::Parser.new(request.env["HTTP_ACCEPT_LANGUAGE"])
parser.language_region_compatible_from(available_locales).gsub(/-/, '_') parser.language_region_compatible_from(available_locales).tr('-', '_')
rescue rescue
# If Accept-Language headers are not set. # If Accept-Language headers are not set.
I18n.default_locale I18n.default_locale
@ -493,7 +492,7 @@ class ApplicationController < ActionController::Base
@recent = Topic.where.not(id: category_topic_ids).recent(10) @recent = Topic.where.not(id: category_topic_ids).recent(10)
@slug = params[:slug].class == String ? params[:slug] : '' @slug = params[:slug].class == String ? params[:slug] : ''
@slug = (params[:id].class == String ? params[:id] : '') if @slug.blank? @slug = (params[:id].class == String ? params[:id] : '') if @slug.blank?
@slug.gsub!('-',' ') @slug.tr!('-',' ')
render_to_string status: status, layout: layout, formats: [:html], template: '/exceptions/not_found' render_to_string status: status, layout: layout, formats: [:html], template: '/exceptions/not_found'
end end

View File

@ -38,7 +38,7 @@ module ApplicationHelper
def script(*args) def script(*args)
if SiteSetting.enable_cdn_js_debugging && GlobalSetting.cdn_url if SiteSetting.enable_cdn_js_debugging && GlobalSetting.cdn_url
tags = javascript_include_tag(*args, "crossorigin" => "anonymous") tags = javascript_include_tag(*args, "crossorigin" => "anonymous")
tags.gsub!("/assets/", "/cdn_asset/#{Discourse.current_hostname.gsub(".","_")}/") tags.gsub!("/assets/", "/cdn_asset/#{Discourse.current_hostname.tr(".","_")}/")
tags.gsub!(".js\"", ".js?v=1&origin=#{CGI.escape request.base_url}\"") tags.gsub!(".js\"", ".js?v=1&origin=#{CGI.escape request.base_url}\"")
tags.html_safe tags.html_safe
else else

View File

@ -22,7 +22,7 @@ class InviteMailer < ActionMailer::Base
# get topic excerpt # get topic excerpt
topic_excerpt = "" topic_excerpt = ""
if first_topic.excerpt if first_topic.excerpt
topic_excerpt = first_topic.excerpt.gsub("\n", " ") topic_excerpt = first_topic.excerpt.tr("\n", " ")
end end
template = 'invite_mailer' template = 'invite_mailer'

View File

@ -341,7 +341,7 @@ class UserNotifications < ActionMailer::Base
else else
invite_template = "user_notifications.invited_to_topic_body" invite_template = "user_notifications.invited_to_topic_body"
end end
topic_excerpt = post.excerpt.gsub("\n", " ") if post.is_first_post? && post.excerpt topic_excerpt = post.excerpt.tr("\n", " ") if post.is_first_post? && post.excerpt
message = I18n.t(invite_template, username: username, topic_title: title, topic_excerpt: topic_excerpt, site_title: SiteSetting.title, site_description: SiteSetting.site_description) message = I18n.t(invite_template, username: username, topic_title: title, topic_excerpt: topic_excerpt, site_title: SiteSetting.title, site_description: SiteSetting.site_description)
html = UserNotificationRenderer.new(Rails.configuration.paths["app/views"]).render( html = UserNotificationRenderer.new(Rails.configuration.paths["app/views"]).render(
template: 'email/invite', template: 'email/invite',

View File

@ -203,7 +203,7 @@ SQL
end end
def i18n_name def i18n_name
self.name.downcase.gsub(' ', '_') self.name.downcase.tr(' ', '_')
end end
end end

View File

@ -102,7 +102,7 @@ TopicStatusUpdate = Struct.new(:topic, :user) do
end end
def locale_key def locale_key
"topic_statuses.#{action_code.gsub('.', '_')}" "topic_statuses.#{action_code.tr('.', '_')}"
end end
def reopening_topic? def reopening_topic?

View File

@ -94,12 +94,12 @@ module Email
# http://www.ietf.org/rfc/rfc2919.txt # http://www.ietf.org/rfc/rfc2919.txt
if topic && topic.category && !topic.category.uncategorized? if topic && topic.category && !topic.category.uncategorized?
list_id = "<#{topic.category.name.downcase.gsub(' ', '-')}.#{host}>" list_id = "<#{topic.category.name.downcase.tr(' ', '-')}.#{host}>"
# subcategory case # subcategory case
if !topic.category.parent_category_id.nil? if !topic.category.parent_category_id.nil?
parent_category_name = Category.find_by(id: topic.category.parent_category_id).name parent_category_name = Category.find_by(id: topic.category.parent_category_id).name
list_id = "<#{topic.category.name.downcase.gsub(' ', '-')}.#{parent_category_name.downcase.gsub(' ', '-')}.#{host}>" list_id = "<#{topic.category.name.downcase.tr(' ', '-')}.#{parent_category_name.downcase.tr(' ', '-')}.#{host}>"
end end
else else
list_id = "<#{host}>" list_id = "<#{host}>"

View File

@ -16,7 +16,7 @@ class IPAddr
(4 - parts.size).times { parts << '*' } # support strings like 192.* (4 - parts.size).times { parts << '*' } # support strings like 192.*
v = parts.join('.') v = parts.join('.')
"#{v.gsub('*', '0')}/#{32 - (v.count('*') * 8)}" "#{v.tr('*', '0')}/#{32 - (v.count('*') * 8)}"
end end
def to_cidr_s def to_cidr_s

View File

@ -20,9 +20,9 @@ module Slug
private private
def self.ascii_generator(string) def self.ascii_generator(string)
string.gsub("'", "") string.tr("'", "")
.parameterize .parameterize
.gsub("_", "-") .tr("_", "-")
end end
def self.encoded_generator(string) def self.encoded_generator(string)