#36215: Use PEP 448 unpacking to concatenate iterables
-------------------------------------+-------------------------------------
     Reporter:  Aarni Koskela        |                    Owner:  (none)
         Type:                       |                   Status:  new
  Cleanup/optimization               |
    Component:  Uncategorized        |                  Version:  5.1
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Comment (by Aarni Koskela):

 Ah, sorry, yeah, I left the microbenchmarks out of the original ticket
 text for brevity.

 {{{
 BLANK_CHOICE_DASH = [("", "---------")]

 def get_action_choices_1(default_choices=BLANK_CHOICE_DASH):
     choices = [] + default_choices
     ...
     return choices


 def get_action_choices_2(default_choices=BLANK_CHOICE_DASH):
     choices = [*default_choices]
     ...
     return choices
 }}}

 (adapted from what `get_action_choices` in the admin does)

 results in

 {{{
 $ uv run --python=3.13 concates.py
 name='get_action_choices_1' iters=5000000 time=0.247
 iters_per_sec=20206826.98
 name='get_action_choices_2' iters=5000000 time=0.207
 iters_per_sec=24111147.61
 $ uv run --python=3.12 concates.py
 name='get_action_choices_1' iters=5000000 time=0.333
 iters_per_sec=15023921.85
 name='get_action_choices_2' iters=5000000 time=0.220
 iters_per_sec=22699169.61
 }}}

 However, now that I benchmark some other expressions (such as some tuple
 concatenations), they may actually be slower in some of these cases; it
 looks like CPython internally converts the first input tuple to a list
 (`BUILD_LIST`), then calls an `INTRINSIC_LIST_TO_TUPLE` opcode on it...
-- 
Ticket URL: <https://code.djangoproject.com/ticket/36215#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/010701953d7d9a37-e403de5a-66a0-4d28-be79-6f4d8c2e614d-000000%40eu-central-1.amazonses.com.

Reply via email to