Closed Bug 945535 Opened 11 years ago Closed 10 years ago

When loading bugs with large number of attachments, $bug->attachments reloads all flags for each attachment even if preloaded

Categories

(Bugzilla :: Attachments & Requests, defect)

defect
Not set
minor

Tracking

()

RESOLVED FIXED
Bugzilla 5.0

People

(Reporter: dkl, Assigned: dkl)

References

Details

(Keywords: perf)

Attachments

(1 file, 1 obsolete file)

Two places in the Bugzilla/Attachments.pm code that are contradictory of each other and causes extra db hits to occur with large number of attachments.

Bugzilla::Attachments::get_attachments_by_bug:

    [...]
    # To avoid $attachment->flags to run SQL queries itself for each
    # attachment listed here, we collect all the data at once and
    # populate $attachment->{flags} ourselves.
    if ($vars->{preload}) {
        $_->{flags} = [] foreach @$attachments;
        my %att = map { $_->id => $_ } @$attachments;

        my $flags = Bugzilla::Flag->match({ bug_id      => $bug_id,
                                            target_type => 'attachment' });

        # Exclude flags for private attachments you cannot see.
        @$flags = grep {exists $att{$_->attach_id}} @$flags;

        push(@{$att{$_->attach_id}->{flags}}, $_) foreach @$flags;
    [...]

But in Bugzilla::Attachment::flags:

 sub flags {
    # Don't cache it as it must be in sync with ->flag_types.
    return $_[0]->{flags} = [map { @{$_->{flags}} } @{$_[0]->flag_types}];
 }

So I propose updating $attachment->flags to only load if the flags list is not already preloaded.

Patch coming.

dkl
Assignee: create-and-change → attach-and-request
Component: Creating/Changing Bugs → Attachments & Requests
Attached patch 945535_1.patch (obsolete) — Splinter Review
Assignee: attach-and-request → dkl
Attachment #8342084 - Flags: review?(glob)
Comment on attachment 8342084 [details] [diff] [review]
945535_1.patch

I want to review it too as I wrote that code.
Attachment #8342084 - Flags: review?(LpSolit)
Comment on attachment 8342084 [details] [diff] [review]
945535_1.patch

r=glob
Attachment #8342084 - Flags: review?(glob) → review+
Comment on attachment 8342084 [details] [diff] [review]
945535_1.patch

>+    # To avoid $attachment->flags and $attachment->flag_types to run SQL queries
>+    # itself

s/itself/themselves/


>+        foreach my $attachment (@$attachments) {
>+            $attachment->{flag_types} = [];
>+            foreach my $type (@$flag_types) {
>+                my $new_type = dclone($type);

As dclone() is a bit slow (called #attachments x #flag_types), you should clone the whole arrayref at once, i.e. write

        foreach my $attachment (@$attachments) {
            $attachment->{flag_types} = [];
            my $new_types = dclone($flag_types);
            foreach my $new_type (@$new_types) {

In my testing, this makes dclone() 30% faster (it's faster to loop in C than in Perl).


>+                $new_type->{flags} = [ grep($_->type_id == $new_type->id 

There is no need to check the type ID. Flags are already grouped by type ID in Bugzilla::Flag->_flag_types.


>+        $attachments = [sort {$a->id <=> $b->id} @$attachments];

$attachments is already sorted.


Your patch works fine, but the benefit is small in a medium installation. Still good to have.
Attachment #8342084 - Flags: review?(LpSolit) → review-
Severity: normal → minor
Target Milestone: --- → Bugzilla 5.0
Attached patch 945535_2.patchSplinter Review
Attachment #8342084 - Attachment is obsolete: true
Attachment #8355252 - Flags: review?(LpSolit)
Comment on attachment 8355252 [details] [diff] [review]
945535_2.patch

Looks good. r=LpSolit
Attachment #8355252 - Flags: review?(LpSolit) → review+
Flags: approval?
Blocks: 956052
Flags: approval? → approval+
Keywords: perf
Committing to: bzr+ssh://dlawrence%40mozilla.com@bzr.mozilla.org/bugzilla/trunk
modified Bugzilla/Attachment.pm
Committed revision 8850.
Status: ASSIGNED → RESOLVED
Closed: 10 years ago
Resolution: --- → FIXED
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: