From c0e25b5a9a9449f128672cbc7e1fde20c03efc84 Mon Sep 17 00:00:00 2001 From: James Cook Date: Fri, 10 Jun 2016 21:37:33 -0500 Subject: [PATCH] Replace certain uses of 'gsub' with 'tr' or 'chomp' for a speed improvement --- app/controllers/application_controller.rb | 11 +++++------ app/helpers/application_helper.rb | 2 +- app/mailers/invite_mailer.rb | 2 +- app/mailers/user_notifications.rb | 2 +- app/models/badge.rb | 2 +- app/models/topic_status_update.rb | 2 +- lib/email/sender.rb | 4 ++-- lib/ip_addr.rb | 2 +- lib/slug.rb | 4 ++-- 9 files changed, 15 insertions(+), 16 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 2db84795883..5ac4811442c 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -296,13 +296,12 @@ class ApplicationController < ActionController::Base def fetch_user_from_params(opts=nil) opts ||= {} user = if params[:username] - username_lower = params[:username].downcase - username_lower.gsub!(/\.json$/, '') + username_lower = params[:username].downcase.chomp('.json') find_opts = { username_lower: username_lower } find_opts[:active] = true unless opts[:include_inactive] || current_user.try(:staff?) User.find_by(find_opts) 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) end 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 # headers use hyphens. 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.language_region_compatible_from(available_locales).gsub(/-/, '_') + parser.language_region_compatible_from(available_locales).tr('-', '_') rescue # If Accept-Language headers are not set. I18n.default_locale @@ -493,7 +492,7 @@ class ApplicationController < ActionController::Base @recent = Topic.where.not(id: category_topic_ids).recent(10) @slug = params[:slug].class == String ? params[:slug] : '' @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' end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 4b6808962cc..c5160585f12 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -38,7 +38,7 @@ module ApplicationHelper def script(*args) if SiteSetting.enable_cdn_js_debugging && GlobalSetting.cdn_url 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.html_safe else diff --git a/app/mailers/invite_mailer.rb b/app/mailers/invite_mailer.rb index f94aca2eb65..346507326fc 100644 --- a/app/mailers/invite_mailer.rb +++ b/app/mailers/invite_mailer.rb @@ -22,7 +22,7 @@ class InviteMailer < ActionMailer::Base # get topic excerpt topic_excerpt = "" if first_topic.excerpt - topic_excerpt = first_topic.excerpt.gsub("\n", " ") + topic_excerpt = first_topic.excerpt.tr("\n", " ") end template = 'invite_mailer' diff --git a/app/mailers/user_notifications.rb b/app/mailers/user_notifications.rb index 284cd00ef4d..b863955fe7d 100644 --- a/app/mailers/user_notifications.rb +++ b/app/mailers/user_notifications.rb @@ -341,7 +341,7 @@ class UserNotifications < ActionMailer::Base else invite_template = "user_notifications.invited_to_topic_body" 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) html = UserNotificationRenderer.new(Rails.configuration.paths["app/views"]).render( template: 'email/invite', diff --git a/app/models/badge.rb b/app/models/badge.rb index f9a175713ed..55e1d81f3f1 100644 --- a/app/models/badge.rb +++ b/app/models/badge.rb @@ -203,7 +203,7 @@ SQL end def i18n_name - self.name.downcase.gsub(' ', '_') + self.name.downcase.tr(' ', '_') end end diff --git a/app/models/topic_status_update.rb b/app/models/topic_status_update.rb index cb8557f2a3c..bfd438bbae9 100644 --- a/app/models/topic_status_update.rb +++ b/app/models/topic_status_update.rb @@ -102,7 +102,7 @@ TopicStatusUpdate = Struct.new(:topic, :user) do end def locale_key - "topic_statuses.#{action_code.gsub('.', '_')}" + "topic_statuses.#{action_code.tr('.', '_')}" end def reopening_topic? diff --git a/lib/email/sender.rb b/lib/email/sender.rb index 29683770fba..e40cf09a675 100644 --- a/lib/email/sender.rb +++ b/lib/email/sender.rb @@ -94,12 +94,12 @@ module Email # http://www.ietf.org/rfc/rfc2919.txt 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 if !topic.category.parent_category_id.nil? 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 else list_id = "<#{host}>" diff --git a/lib/ip_addr.rb b/lib/ip_addr.rb index c80501c3bb0..22886a5ec38 100644 --- a/lib/ip_addr.rb +++ b/lib/ip_addr.rb @@ -16,7 +16,7 @@ class IPAddr (4 - parts.size).times { parts << '*' } # support strings like 192.* v = parts.join('.') - "#{v.gsub('*', '0')}/#{32 - (v.count('*') * 8)}" + "#{v.tr('*', '0')}/#{32 - (v.count('*') * 8)}" end def to_cidr_s diff --git a/lib/slug.rb b/lib/slug.rb index e2c876de701..133a4e1bd1b 100644 --- a/lib/slug.rb +++ b/lib/slug.rb @@ -20,9 +20,9 @@ module Slug private def self.ascii_generator(string) - string.gsub("'", "") + string.tr("'", "") .parameterize - .gsub("_", "-") + .tr("_", "-") end def self.encoded_generator(string)