[New-bugs-announce] [issue45530] Improve listobject.c's unsafe_tuple_compare()

Tim Peters report at bugs.python.org
Tue Oct 19 17:41:29 EDT 2021


New submission from Tim Peters <tim at python.org>:

The code could typically be faster if it did what its comments imply it does: skip the expense of PyObject_RichCompareBool() entirely for the first pair of tuple elements. It actually always calls PyObject_RichCompareBool() on the first pair, and only if that says "not equal" does it use the faster ms->tuple_elem_compare to resolve "ok, so it's less than?".

Instead it could do the first pair before, and entirely apart from, the loop, along the lines of:

- Use ms->tuple_elem_compare to see whether u[0] < v[0]. If so, or if an error, we're done.

- Use ms->tuple_elem_compare to see whether v[0] < u[0]. If so, or if an error, we're done.

Else we can assume u[0] == v[0], and go on to compare u[1:] to v[1:] via a trivial variation of the current code.

In cases where the first pair does resolve it (common!), replacing a PyObject_RichCompareBool() with a ms->tuple_elem_compare can be significantly faster overall.

----------
components: Interpreter Core
messages: 404360
nosy: tim.peters
priority: normal
severity: normal
stage: needs patch
status: open
title: Improve listobject.c's unsafe_tuple_compare()
type: performance
versions: Python 3.11

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue45530>
_______________________________________


More information about the New-bugs-announce mailing list