#36242: NodeList render overhead with huge templates
-------------------------------------+-------------------------------------
Reporter: Michal Čihař | Owner: (none)
Type: | Status: closed
Cleanup/optimization |
Component: Uncategorized | Version: 5.1
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Michal Čihař):
This all made me look into the implementation, and the list comprehension
seems like the best approach in this case. It calls `PySequence_Fast`
which converts iterable into a list if it is not a list or a tuple.
Creating a list using comprehension should be faster than creating a
generator and then converting it to the list, but it most likely depends
on the CPU cache size and this is the corner case I observe (the strings
fill the CPU cache in my case).
Additionally, there is a fast path for same width Unicode strings
(separator + all items), so pure ASCII is:
{{{
(py3.14) nijel@lobsang:/tmp$ python -m timeit -n 1000 '"".join(["x" * 5000
for n in range(1000)])'
1000 loops, best of 5: 4.34 msec per loop
(py3.14) nijel@lobsang:/tmp$ python -m timeit -n 1000 '"".join("x" * 5000
for n in range(1000))'
1000 loops, best of 5: 772 usec per loop
}}}
But once you mix Unicode into that:
{{{
(py3.14) nijel@lobsang:/tmp$ python -m timeit -n 1000 '"".join(["š" * 5000
for n in range(1000)])'
1000 loops, best of 5: 10.5 msec per loop
(py3.14) nijel@lobsang:/tmp$ python -m timeit -n 1000 '"".join("š" * 5000
for n in range(1000))'
1000 loops, best of 5: 10.4 msec per loop
}}}
And now any difference is gone. So, indeed, this is not a way to optimize.
--
Ticket URL: <
https://code.djangoproject.com/ticket/36242#comment:4>