Opened 9 years ago

Closed 8 years ago

#25753 closed Cleanup/optimization (fixed)

Cache formats retrieved from django settings in formats.get_format

Reported by: Jaap Roes Owned by: nobody
Component: Utilities Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Jaap Roes)

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'))

Change History (7)

comment:1 by Jaap Roes, 9 years ago

Description: modified (diff)

comment:2 by Tim Graham, 9 years ago

Patch needs improvement: set
Triage Stage: UnreviewedAccepted

comment:3 by Jaap Roes, 9 years ago

The patch is now rebased on the current master

comment:4 by Claude Paroz, 8 years ago

Patch needs improvement: unset

New PR

comment:5 by Tim Graham, 8 years ago

Triage Stage: AcceptedReady for checkin

comment:6 by Claude Paroz <claude@…>, 8 years ago

In 1206d7fa:

Refs #25753 -- Reset l10n cache when format settings change

Thanks Jaap Roes for the initial patch.

comment:7 by Claude Paroz <claude@…>, 8 years ago

Resolution: fixed
Status: newclosed

In 3188b49e:

Fixed #25753 -- Made get_format() cache the formats from Django settings

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