Project

General

Profile

Actions

Patch #11827

closed

Avoid retrieving memberships for projects jump box

Added by Jean-Baptiste Barth about 12 years ago. Updated over 11 years ago.

Status:
Closed
Priority:
Normal
Category:
Code cleanup/refactoring
Target version:
-
Start date:
Due date:
% Done:

0%

Estimated time:

Description

ApplicationHelper#render_project_jump_box retrieves current user's memberships (and roles, see the association..) so that it can render user's projects. From what I seen it's unecessary, but it comes from an inverse optimization in r5153 (maybe it was relevant in Rails 2?). Retrieving directly user's projects could be better :
  • saves a complex query : on a large instance at work, the average staff user has 120 projects and the double join in the SQL query doesn't come for free ;
  • saves a lot of objects instanciations (Memberships)

So here's the proposal:

diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index 794cc6f..6c1f919 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -237,7 +237,8 @@ module ApplicationHelper
   # Renders the project quick-jump box
   def render_project_jump_box
     return unless User.current.logged?
-    projects = User.current.memberships.collect(&:project).compact.uniq
+    #'to_a' forces immediate querying, which saves a 'count' query, see two lines below
+    projects = User.current.projects.active.to_a
     if projects.any?
       s = '<select onchange="if (this.value != \'\') { window.location = this.value; }">' +
             "<option value=''>#{ l(:label_jump_to_a_project) }</option>" +

On a very slow machine, a neutral page (say /admin/info) spends ~20% of his time building this select. On this machine this line takes about 350ms with the current version, 80ms with the proposed one. On a faster machine, it only saves a few dozens ms, but not something I benchmarked seriously (I can if needed). Of course all tests pass with both versions.

Comments welcome!


Related issues

Related to Redmine - Patch #13301: Performance: avoid querying all memberships in User#roles_for_projectClosedJean-Baptiste Barth

Actions
Actions

Also available in: Atom PDF