Opened 5 years ago

Closed 5 years ago

Last modified 4 years ago

#15283 closed (fixed)

A performance imporvement for QuerySet

Reported by: lanyjie Owned by: nobody
Component: Database layer (models, ORM) Version: 1.2
Severity: Keywords: QuerySet performance
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


Since the len method of class QuerySet is called quite frequently (for example, as p art of list(qs), we make some effort here to be as efficient as possible whilst not messing up any existing iterators against the QuerySet.

In the implementation of that method, I noticed a performance improvement, which is very simple to do. The last three lines of code was:

        elif self._iter:
        return len(self._result_cache)

which involves the creation of a list to pass as an argument to the extend() method. Clearly this is not necessary as the extend() method takes an iterable, and self._iter is already iterable, so that can be simplified without creating the list, which should result in improved performance both in speed and in memory use. Here is the improved code:

        elif self._iter:
        return len(self._result_cache)

Change History (6)

comment:1 Changed 5 years ago by Alex

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

This looks fine, do you have any actual performance numbers from this, or is it just a case of "it obviously can't be worse".

comment:2 Changed 5 years ago by Alex

  • Triage Stage changed from Unreviewed to Accepted

comment:3 Changed 5 years ago by russellm

  • milestone set to 1.3
  • Triage Stage changed from Accepted to Ready for checkin

Like Alex says, this obviously can't be worse, it's a one line change, and the existing test suite should be exercising this pretty hard. I've run it through the full test suite, and it doesn't break anything (not that I would expect that it would), so I'm gonna call this RFC.

comment:4 Changed 5 years ago by russellm

  • Resolution set to fixed
  • Status changed from new to closed

In [15491]:

Fixed #15283 -- Made a minor performance tweak to iter. Thanks to lanyjie for the report.

comment:5 Changed 5 years ago by russellm

In [15496]:

[1.2.X] Fixed #15283 -- Made a minor performance tweak to iter. Thanks to lanyjie for the report.

Backport of r15491 from trunk.

comment:6 Changed 4 years ago by jacob

  • milestone 1.3 deleted

Milestone 1.3 deleted

Note: See TracTickets for help on using tickets.
Back to Top