[PATCH 2/2] xmlrpc: Treat negative max_count as meaning "return last N results"

Damien Lespiau damien.lespiau at intel.com
Fri Apr 1 01:08:45 AEDT 2016

On Fri, Feb 05, 2016 at 05:21:20PM +0000, Stephen Finucane wrote:
> @@ -602,6 +607,8 @@ def patch_list(filt=None):
>          if max_count > 0:
>              return list(map(patch_to_dict, patches[:max_count]))
> +        elif max_count < 0:
> +            return list(map(patch_to_dict, patches[len(patches)+max_count:]))

This will do something you probably don't want to happen:

  - len(patches) will actually retrieve all patches in memory and return
    the length from that data,

  - the slicing operation will then happen without querying the DB and
    slice the results cached from the len() evaluation.

You can use 2 queries to not have to load all patches:

  patches[patches.count() + max_count:]

Alternatively, it's possible to use reverse() to not have to depend on
querying the number of items beforehand (which is what I did in my
version of the patch) and reverse the list once again once we retrieved
the elements. Given that we already go through the list of patches to
map each patch to a dict, iterating in reverse order doesn't add extra
cost. Something only those lines maybe?

  query = patches.reverse()[:-max_count]
  return [patch_to_dict(patch) for patch in reversed(query)]


More information about the Patchwork mailing list