[Django] #25753: Cache formats retrieved from django settings in formats.get_format

7 views
Skip to first unread message

Django

unread,
Nov 14, 2015, 10:20:27 AM11/14/15
to django-...@googlegroups.com
#25753: Cache formats retrieved from django settings in formats.get_format
--------------------------------------+--------------------
Reporter: jaap3 | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Utilities | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 1
Easy pickings: 0 | UI/UX: 0
--------------------------------------+--------------------
If L10N is disabled `formats.get_format` always goes through django's
settings module to get the requested format type. Accessing settings is
fairly expensive, so I propose caching these values in the same way the
values are already cached when L10N is enabled.

{{{
import timeit
import django
from django.conf import settings
from django.utils.formats import get_format

settings.configure()
django.setup()

print('%s' % timeit.repeat("get_format('DATETIME_INPUT_FORMATS')",
setup='from __main__ import get_format'))
}}}

Before:

`[4.791080316994339, 4.822412799010635, 4.568255095000495]`

After:

`[2.801479902002029, 2.874774623007397, 2.796864636009559]`

The performance remains the same for when L10N is enabled (e.g. replace
`settings.configure()` with `settings.configure(USE_L10N=True,
LANGUAGE_CODE='nl')`)

--
Ticket URL: <https://code.djangoproject.com/ticket/25753>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Nov 14, 2015, 10:30:28 AM11/14/15
to django-...@googlegroups.com
#25753: Cache formats retrieved from django settings in formats.get_format
-------------------------------------+-------------------------------------
Reporter: jaap3 | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Utilities | Version: master
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
-------------------------------------+-------------------------------------
Changes (by jaap3):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Old description:

> If L10N is disabled `formats.get_format` always goes through django's
> settings module to get the requested format type. Accessing settings is
> fairly expensive, so I propose caching these values in the same way the
> values are already cached when L10N is enabled.
>
> {{{
> import timeit
> import django
> from django.conf import settings
> from django.utils.formats import get_format
>
> settings.configure()
> django.setup()
>
> print('%s' % timeit.repeat("get_format('DATETIME_INPUT_FORMATS')",
> setup='from __main__ import get_format'))
> }}}
>
> Before:
>
> `[4.791080316994339, 4.822412799010635, 4.568255095000495]`
>
> After:
>
> `[2.801479902002029, 2.874774623007397, 2.796864636009559]`
>
> The performance remains the same for when L10N is enabled (e.g. replace
> `settings.configure()` with `settings.configure(USE_L10N=True,
> LANGUAGE_CODE='nl')`)

New description:

If L10N is disabled `formats.get_format` always goes through Django's


settings module to get the requested format type. Accessing settings is

fairly expensive, so I propose caching these values in the same way as
when L10N is enabled.

{{{
import timeit
import django
from django.conf import settings
from django.utils.formats import get_format

settings.configure()
django.setup()

print('%s' % timeit.repeat("get_format('DATETIME_INPUT_FORMATS')",
setup='from __main__ import get_format'))
}}}

Before:

`[4.791080316994339, 4.822412799010635, 4.568255095000495]`

After:

`[2.801479902002029, 2.874774623007397, 2.796864636009559]`

The performance remains the same for when L10N is enabled (e.g. replace
`settings.configure()` with `settings.configure(USE_L10N=True,
LANGUAGE_CODE='nl')`)

--

--
Ticket URL: <https://code.djangoproject.com/ticket/25753#comment:1>

Django

unread,
Nov 14, 2015, 10:47:20 AM11/14/15
to django-...@googlegroups.com
#25753: Cache formats retrieved from django settings in formats.get_format
--------------------------------------+------------------------------------

Reporter: jaap3 | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Utilities | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by timgraham):

* needs_better_patch: 0 => 1
* stage: Unreviewed => Accepted


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

Django

unread,
Nov 26, 2015, 6:34:38 AM11/26/15
to django-...@googlegroups.com
#25753: Cache formats retrieved from django settings in formats.get_format
--------------------------------------+------------------------------------

Reporter: jaap3 | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Utilities | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------

Comment (by jaap3):

The patch is now rebased on the current master

--
Ticket URL: <https://code.djangoproject.com/ticket/25753#comment:3>

Django

unread,
Dec 1, 2016, 5:06:19 PM12/1/16
to django-...@googlegroups.com
#25753: Cache formats retrieved from django settings in formats.get_format
--------------------------------------+------------------------------------
Reporter: Jaap Roes | Owner: nobody

Type: Cleanup/optimization | Status: new
Component: Utilities | Version: master
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 Claude Paroz):

* needs_better_patch: 1 => 0


Comment:

New [https://github.com/django/django/pull/7649 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/25753#comment:4>

Django

unread,
Dec 19, 2016, 6:36:22 PM12/19/16
to django-...@googlegroups.com
#25753: Cache formats retrieved from django settings in formats.get_format
-------------------------------------+-------------------------------------

Reporter: Jaap Roes | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Utilities | Version: master
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 Tim Graham):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/25753#comment:5>

Django

unread,
Dec 21, 2016, 2:36:59 PM12/21/16
to django-...@googlegroups.com
#25753: Cache formats retrieved from django settings in formats.get_format
-------------------------------------+-------------------------------------
Reporter: Jaap Roes | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Utilities | Version: master

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
-------------------------------------+-------------------------------------

Comment (by Claude Paroz <claude@…>):

In [changeset:"1206d7fa5793a86ffadeacb2e9280b11d474a58a" 1206d7fa]:
{{{
#!CommitTicketReference repository=""
revision="1206d7fa5793a86ffadeacb2e9280b11d474a58a"
Refs #25753 -- Reset l10n cache when format settings change

Thanks Jaap Roes for the initial patch.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/25753#comment:6>

Django

unread,
Dec 21, 2016, 2:37:00 PM12/21/16
to django-...@googlegroups.com
#25753: Cache formats retrieved from django settings in formats.get_format
-------------------------------------+-------------------------------------
Reporter: Jaap Roes | Owner: nobody
Type: | Status: closed
Cleanup/optimization |
Component: Utilities | Version: master
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 Claude Paroz <claude@…>):

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


Comment:

In [changeset:"3188b49ee29bb8c7183b55bda784e642a6ec3011" 3188b49e]:
{{{
#!CommitTicketReference repository=""
revision="3188b49ee29bb8c7183b55bda784e642a6ec3011"
Fixed #25753 -- Made get_format() cache the formats from Django settings
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/25753#comment:7>

Reply all
Reply to author
Forward
0 new messages