[Django] #35998: Add caching to urlize

9 views
Skip to first unread message

Django

unread,
Dec 11, 2024, 8:39:10 AM12/11/24
to django-...@googlegroups.com
#35998: Add caching to urlize
-------------------------------------+-------------------------------------
Reporter: Sarah Boyce | Type:
| Cleanup/optimization
Status: new | Component: Template
| system
Version: 5.1 | Severity: Normal
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
`urlize` will checks every word for whether it is a url (and then updates
the url to be a html link)
We can add some form of caching so that it doesn't repeat the checks on
the same "word"

PR: https://github.com/django/django/pull/18665

Benchmarks:

Before:
{{{
>>> import cProfile
>>> from django.template.defaultfilters import urlize
>>> cProfile.run("urlize('Django. ' * 1_000)")
61048 function calls (59047 primitive calls) in 0.030 seconds
}}}
After:
{{{
>>> import cProfile
>>> from django.template.defaultfilters import urlize
>>> cProfile.run("urlize('Django. ' * 1_000)")
3783 function calls (3702 primitive calls) in 0.005 seconds
}}}

PR to add benchmark to django-asv: https://github.com/django/django-
asv/pull/91
--
Ticket URL: <https://code.djangoproject.com/ticket/35998>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Dec 11, 2024, 8:41:46 AM12/11/24
to django-...@googlegroups.com
#35998: Add caching to urlize
-------------------------------------+-------------------------------------
Reporter: Sarah Boyce | Owner: Sarah
Type: | Boyce
Cleanup/optimization | Status: assigned
Component: Template system | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Natalia Bidart):

* owner: (none) => Sarah Boyce
* stage: Unreviewed => Accepted
* status: new => assigned
* version: 5.1 => dev


Old description:

> `urlize` will checks every word for whether it is a url (and then updates
> the url to be a html link)
> We can add some form of caching so that it doesn't repeat the checks on
> the same "word"
>
> PR: https://github.com/django/django/pull/18665
>
> Benchmarks:
>
> Before:
> {{{
> >>> import cProfile
> >>> from django.template.defaultfilters import urlize
> >>> cProfile.run("urlize('Django. ' * 1_000)")
> 61048 function calls (59047 primitive calls) in 0.030 seconds
> }}}
> After:
> {{{
> >>> import cProfile
> >>> from django.template.defaultfilters import urlize
> >>> cProfile.run("urlize('Django. ' * 1_000)")
> 3783 function calls (3702 primitive calls) in 0.005 seconds
> }}}
>
> PR to add benchmark to django-asv: https://github.com/django/django-
> asv/pull/91

New description:

`urlize` will check every word for whether it is a url (and then updates
the url to be a html link).
We can add some form of caching so that it doesn't repeat the checks on
the same "word"

PR: https://github.com/django/django/pull/18665

Benchmarks:

Before:
{{{
>>> import cProfile
>>> from django.template.defaultfilters import urlize
>>> cProfile.run("urlize('Django. ' * 1_000)")
61048 function calls (59047 primitive calls) in 0.030 seconds
}}}
After:
{{{
>>> import cProfile
>>> from django.template.defaultfilters import urlize
>>> cProfile.run("urlize('Django. ' * 1_000)")
3783 function calls (3702 primitive calls) in 0.005 seconds
}}}

PR to add benchmark to django-asv: https://github.com/django/django-
asv/pull/91

--
Comment:

Thank you Sarah, makes sense to me.
--
Ticket URL: <https://code.djangoproject.com/ticket/35998#comment:1>

Django

unread,
Jan 14, 2025, 10:47:56 AM1/14/25
to django-...@googlegroups.com
#35998: Add caching to urlize
-------------------------------------+-------------------------------------
Reporter: Sarah Boyce | Owner: Sarah
Type: | Boyce
Cleanup/optimization | Status: assigned
Component: Template system | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* stage: Accepted => Ready for checkin

--
Ticket URL: <https://code.djangoproject.com/ticket/35998#comment:2>

Django

unread,
Jan 14, 2025, 11:59:45 AM1/14/25
to django-...@googlegroups.com
#35998: Add caching to urlize
-------------------------------------+-------------------------------------
Reporter: Sarah Boyce | Owner: Sarah
Type: | Boyce
Cleanup/optimization | Status: closed
Component: Template system | Version: dev
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce <42296566+sarahboyce@…>):

* resolution: => fixed
* status: assigned => closed

Comment:

In [changeset:"b721f127603516c75ebda6912046ff5f0694e150" b721f127]:
{{{#!CommitTicketReference repository=""
revision="b721f127603516c75ebda6912046ff5f0694e150"
Fixed #35998 -- Added caching to django.utils.html.urlize().
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35998#comment:3>
Reply all
Reply to author
Forward
0 new messages