This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Inelegant loops in Modules/_sqlite/cursor.c
Type: performance Stage: resolved
Components: Library (Lib) Versions: Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: alex.henrie, berker.peksag, veky
Priority: normal Keywords: patch

Created on 2020-01-30 03:18 by alex.henrie, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 18270 merged alex.henrie, 2020-01-30 03:21
Messages (6)
msg361006 - (view) Author: Alex Henrie (alex.henrie) * Date: 2020-01-30 03:18
pysqlite_cursor_fetchall currently has the following bit of code:

    /* just make sure we enter the loop */
    row = (PyObject*)Py_None;

    while (row) {
        row = pysqlite_cursor_iternext(self);
        if (row) {
            PyList_Append(list, row);
            Py_DECREF(row);
        }
    }

This can and should be rewritten as a for loop to avoid the unnecessary initialization to Py_None and the redundant if statement inside the loop.

pysqlite_cursor_fetchmany has the same problem.
msg361150 - (view) Author: Vedran Čačić (veky) * Date: 2020-02-01 05:50
You mean, something like

    while ((row = pysqlite_cursor_iternext(self))) {
            PyList_Append(list, row);
            Py_DECREF(row);
    }

? It's interesting that now we have walrus in Python, we see the opportunities for it in other languages. (In C, it's spelled (=).:)
msg361152 - (view) Author: Alex Henrie (alex.henrie) * Date: 2020-02-01 07:15
You're right, that's even better. I forgot that the equals operator also returns the variable's new value. I just updated my pull request :-)
msg361153 - (view) Author: Vedran Čačić (veky) * Date: 2020-02-01 07:36
It seems you haven't read carefully what I've written. This way some compilers might emit warnings. Please read https://stackoverflow.com/questions/5476759/compiler-warning-suggest-parentheses-around-assignment-used-as-truth-value
msg361174 - (view) Author: Alex Henrie (alex.henrie) * Date: 2020-02-01 16:47
Sorry about that, I didn't notice that GCC's -Wall option emits a warning about this. I just added the extra sets of parentheses.
msg361192 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2020-02-01 20:45
New changeset 78c7183f470b60a39ac2dd0ad1a94d49d1e0b062 by Alex Henrie in branch 'master':
bpo-39496: Remove redundant checks from _sqlite/cursor.c (GH-18270)
https://github.com/python/cpython/commit/78c7183f470b60a39ac2dd0ad1a94d49d1e0b062
History
Date User Action Args
2022-04-11 14:59:26adminsetgithub: 83677
2020-02-01 20:46:08berker.peksagsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2020-02-01 20:45:47berker.peksagsetnosy: + berker.peksag
messages: + msg361192
2020-02-01 16:47:40alex.henriesetmessages: + msg361174
2020-02-01 07:36:55vekysetmessages: + msg361153
2020-02-01 07:15:11alex.henriesetmessages: + msg361152
2020-02-01 05:50:34vekysetnosy: + veky
messages: + msg361150
2020-01-30 03:21:22alex.henriesetkeywords: + patch
stage: patch review
pull_requests: + pull_request17645
2020-01-30 03:18:40alex.henriecreate