Opened 9 years ago

Closed 9 years ago

#2632 closed enhancement (fixed)

[patch] Clearer, faster _dict_helper()

Reported by: Andy Dustman <farcepest@…> Owned by: adrian
Component: Database layer (models, ORM) Version:
Severity: trivial Keywords:
Cc: farcepest@… Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Currently django.db.backends.util._dict_helper uses enumerate() and several index operations per column to build a dictionary result. It's much simpler and faster, since there's only one index operation per column vs. four with current implementation, and no intermediate list from enumerate():

Index: django/db/backends/util.py
===================================================================
--- django/db/backends/util.py  (revision 3682)
+++ django/db/backends/util.py  (working copy)
@@ -98,7 +98,7 @@

 def _dict_helper(desc, row):
     "Returns a dictionary for the given cursor.description and result row."
-    return dict([(desc[col[0]][0], col[1]) for col in enumerate(row)])
+    return dict(zip([col[0] for col in desc], row))

 def dictfetchone(cursor):
     "Returns a row from the cursor as a dict"

Not that this is likely to be a big bottleneck in Django, but there's no downside.

Change History (1)

comment:1 Changed 9 years ago by adrian

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

(In [3684]) Fixed #2632 -- Made django.db.backends.util._dict_helper more efficient. Thanks for the patch, Andy Dustman

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