Skip to content
Snippets Groups Projects

Don't call `#uniq` on a relation

Merged username-removed-443319 requested to merge speed-up-members-dropdown into master
1 unresolved thread

When there was no project, no search, and no current user or author param, the AutocompleteController would call #uniq! on a relation instead of an array. This performed the less-efficient SELECT DISTINCT when it wasn't even needed (because the query wouldn't return duplicates anyway - duplicates were only added by putting a user on top of the list).

Makes the worst case for https://gitlab.com/gitlab-org/gitlab-ce/issues/27085 better.

Merge request reports

Loading
Loading

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
18 18 if params[:search].blank?
19 19 # Include current user if available to filter by "Me"
20 20 if params[:current_user].present? && current_user
21 @users = [current_user, *@users]
21 @users = [current_user, *@users].uniq
  • @smcgivern Since the goal of the uniq seems to be to make sure current_user doesn't occur twice, how explicitly removing current_user from @users? Two options:

    # 1
    @users = [current_user, *@users.delete(current_user)]
    
    # 2
    
    @users = @users.where.not(id: current_user.id)
    @users = [current_user, *@users]

    I prefer 2, since 1, could have us end up showing an inconsistent number of users depending on whether current_user occurs on the first page.

  • I don't have a problem with either, but showing an inconsistent number of users has been happening for over a year :wink:

    Also, for option 2, that's trickier when we're passing an author_id, because if that user ID doesn't exist, we want 20 'other' users, and if that user ID does exist, we want 19 'other' users. We could add CASE WHEN id = :author_id THEN 0 ELSE 1 AS order, and then ORDER BY order, name, but realistically I have no problem with doing this in Ruby to avoid the SELECT DISTINCT :slight_smile:

  • @smcgivern I don't care all too much about the number of users, but if we can skip a uniq when we know exactly which element we want to remove, I think we should!

  • I've done this for the current_user case. For the author_id case, because you can pass current_user and author_id, I don't think we should do this, as we wouldn't have a relation. I'm pretty sure that we don't do that, but this isn't very well-specified, and all I really want to do here is fix that one query.

  • This fails in EE on the mirror page, because @users is an array at this point :disappointed:

    https://gitlab.com/gitlab-org/gitlab-ce/issues/29316

  • Please register or sign in to reply
  • assigned to @smcgivern

  • Douwe Maan canceled the automatic merge

    canceled the automatic merge

  • assigned to @smcgivern

  • added 1 commit

    • 2c40a012 - Don't call #uniq on a relation

    Compare with previous version

  • Douwe Maan enabled an automatic merge when the pipeline for 2c40a012 succeeds

    enabled an automatic merge when the pipeline for 2c40a012 succeeds

  • merged

  • Douwe Maan mentioned in commit 916fae3a

    mentioned in commit 916fae3a

  • mentioned in issue #27400 (closed)

  • Picked into 8-16-stable, will be in 8.16.3

  • Robert Speicher removed ~149423 label

    removed ~149423 label

  • Please register or sign in to reply
    Loading