From 5092b388feedce31b9f8d5886f4c51ad3ab76779 Mon Sep 17 00:00:00 2001 From: Reinier Balt Date: Wed, 19 Aug 2015 15:12:52 +0200 Subject: [PATCH] Fasterer: Parallel assignment is slower Parallel assignment is slower than sequential assignment. Only got the low hanging fruit. There are some functions that have multiple return values. Fixing this needs more refactoring. --- app/controllers/todos_controller.rb | 20 ++++++--- app/helpers/projects_helper.rb | 3 +- lib/login_system.rb | 68 ++++++++++++++++------------- lib/tracks/utils.rb | 19 ++++---- 4 files changed, 63 insertions(+), 47 deletions(-) diff --git a/app/controllers/todos_controller.rb b/app/controllers/todos_controller.rb index e0869f03..dbfda8ab 100644 --- a/app/controllers/todos_controller.rb +++ b/app/controllers/todos_controller.rb @@ -40,18 +40,21 @@ class TodosController < ApplicationController cookies[:mobile_url]= { :value => request.fullpath, :secure => SITE_CONFIG['secure_cookies']} determine_down_count - render :action => 'index' + render :action => 'index'.freeze end format.text do # somehow passing Mime::TEXT using content_type to render does not work - headers['Content-Type']=Mime::TEXT.to_s + headers['Content-Type'.freeze]=Mime::TEXT.to_s render :content_type => Mime::TEXT end format.xml do @xml_todos = params[:limit_to_active_todos] ? @not_done_todos : @todos render :xml => @xml_todos.to_xml( *todo_xml_params ) end - format.any(:rss, :atom) { @feed_title, @feed_description = 'Tracks Actions', "Actions for #{current_user.display_name}" } + format.any(:rss, :atom) do + @feed_title = 'Tracks Actions'.freeze + @feed_description = "Actions for #{current_user.display_name}" + end format.ics end end @@ -156,7 +159,10 @@ class TodosController < ApplicationController p = Todos::TodoCreateParamsHelper.new(params, current_user) tag_list = p.tag_list - @not_done_todos, @build_todos, @todos, errors = [], [], [], [] + @not_done_todos = [] + @build_todos = [] + @todos = [] + errors = [] @predecessor = nil validates = true @@ -887,13 +893,15 @@ class TodosController < ApplicationController elsif params[:format].nil? # if no format is given, default to html # note that if url has ?format=m, we should not overwrite it here - request.format, params[:format] = :html, :html + request.format = :html + params[:format] = :html end end def set_format_for_tag_view(format) # tag name ends with .m, set format to :m en remove .m from name - request.format, params[:format] = format, format + request.format = format + params[:format] = format params[:name] = params[:name].chomp(".#{format.to_s}") end diff --git a/app/helpers/projects_helper.rb b/app/helpers/projects_helper.rb index 503583ff..f6463263 100644 --- a/app/helpers/projects_helper.rb +++ b/app/helpers/projects_helper.rb @@ -25,7 +25,8 @@ module ProjectsHelper end def project_next_prev_mobile - prev_project,next_project= "", "" + prev_project = "" + next_project = "" prev_project = content_tag(:li, link_to_project_mobile(@previous_project, "5", @previous_project.shortened_name), :class=>"prev") if @previous_project next_project = content_tag(:li, link_to_project_mobile(@next_project, "6", @next_project.shortened_name), :class=>"next") if @next_project return content_tag(:ul, "#{prev_project}#{next_project}".html_safe, :class=>"next-prev-project") diff --git a/lib/login_system.rb b/lib/login_system.rb index ef8e0164..f2f97f9d 100644 --- a/lib/login_system.rb +++ b/lib/login_system.rb @@ -9,7 +9,7 @@ module LoginSystem def prefs current_user.prefs unless current_user.nil? end - + # Logout the {#current_user} and redirect to login page # # @param [String] message notification to display @@ -25,9 +25,9 @@ module LoginSystem redirect_to_login end end - + protected - + # overwrite this if you want to restrict access to only a few actions # or if you want to check if the user has the correct rights # example: @@ -39,7 +39,7 @@ module LoginSystem def authorize?(user) true end - + # overwrite this method if you only want to protect certain actions of the controller # example: # @@ -54,7 +54,7 @@ module LoginSystem def protect?(action) true end - + # When called with before_filter :login_from_cookie will check for an :auth_token # cookie and log the user back in if appropriate def login_from_cookie @@ -69,7 +69,7 @@ module LoginSystem flash[:notice] = t('login.successful') end end - + def login_or_feed_token_required if ['rss', 'atom', 'txt', 'ics'].include?(params[:format]) if user = User.where(:token => params[:token]).first @@ -79,7 +79,7 @@ module LoginSystem end login_required end - + # login_required filter. add # # before_filter :login_required @@ -90,19 +90,19 @@ module LoginSystem # def authorize?(user) # def login_required - + if not protect?(action_name) return true end - + login_from_cookie if session['user_id'] and authorize?(get_current_user) return true end - - http_user, http_pass = get_basic_auth_data - if user = User.authenticate(http_user, http_pass) + + auth = get_basic_auth_data + if user = User.authenticate(auth[:user], auth[:pass]) session['user_id'] = user.id set_current_user(user) return true @@ -111,22 +111,22 @@ module LoginSystem # store current location so that we can # come back after the user logged in store_location unless params[:format] == 'js' - + # call overwriteable reaction to unauthorized access access_denied return false end - + def login_optional login_from_cookie - + if session['user_id'] and authorize?(get_current_user) return true end - - http_user, http_pass = get_basic_auth_data - if user = User.authenticate(http_user, http_pass) + + auth = get_basic_auth_data + if user = User.authenticate(auth[:user], auth[:pass]) session['user_id'] = user.id set_current_user(user) return true @@ -134,22 +134,22 @@ module LoginSystem return true end - + def logged_in? current_user != nil end - + def get_current_user if @user.nil? && session['user_id'] @user = User.find(session['user_id']) end @user end - + def set_current_user(user) @user = user end - + # overwrite if you want to have special behavior in case the user is not authorized # to access the current operation. # the default action is to redirect to the login screen @@ -179,28 +179,34 @@ module LoginSystem session['return-to'] = nil end end - + # HTTP Basic auth code adapted from Coda Hale's simple_http_auth plugin. Thanks, Coda! def get_basic_auth_data - + auth_locations = ['REDIRECT_REDIRECT_X_HTTP_AUTHORIZATION', 'REDIRECT_X_HTTP_AUTHORIZATION', 'X-HTTP_AUTHORIZATION', 'HTTP_AUTHORIZATION'] - + authdata = nil - for location in auth_locations + auth_locations.each do |location| if request.env.has_key?(location) authdata = request.env[location].to_s.split end end if authdata and authdata[0] == 'Basic' - user, pass = Base64.decode64(authdata[1]).split(':')[0..1] + data = Base64.decode64(authdata[1]).split(':')[0..1] + return { + user: data[0], + pass: data[1] + } else - user, pass = ['', ''] + return { + user: ''.freeze, + pass: ''.freeze + } end - return user, pass end - + def basic_auth_denied response.headers["WWW-Authenticate"] = "Basic realm=\"'Tracks Login Required'\"" render :text => t('login.unsuccessful'), :status => 401 @@ -216,4 +222,4 @@ private end end -end \ No newline at end of file +end diff --git a/lib/tracks/utils.rb b/lib/tracks/utils.rb index 09e842cd..42ebd9b8 100644 --- a/lib/tracks/utils.rb +++ b/lib/tracks/utils.rb @@ -4,13 +4,14 @@ module Tracks class Utils AUTO_LINK_MESSAGE_RE = %r{message://<[^>]+>} unless const_defined?(:AUTO_LINK_MESSAGE_RE) - + # Converts message:// links to href. This URL scheme is used on Mac OS X # to link to a mail message in Mail.app. def self.auto_link_message(text) text.gsub(AUTO_LINK_MESSAGE_RE) do href = $& - left, right = $`, $' + left = $` + right = $' # detect already linked URLs and URLs in the middle of a tag if left =~ /<[^>]+$/ && right =~ /^[^>]*>/ # do not change string; URL is alreay linked @@ -30,25 +31,25 @@ module Tracks config = Sanitize::Config.merge(Sanitize::Config::RELAXED, :protocols => { 'a' => {'href' => Sanitize::Config::RELAXED[:protocols]['a']['href'] + ['onenote', 'message']}} ) - + rendered = Sanitize.clean(rendered, config) return rendered.html_safe end - + def self.textile(text) RedCloth.new(text).to_html end - + def self.sanitize_filename(filename) filename.gsub(/[^0-9A-z.\-]/, '_') end private - + def self.helpers ActionController::Base.helpers end - + end - -end \ No newline at end of file + +end