[Django] #34517: ImageField unnecessarily adds a post_init signal handler to the model

31 views
Skip to first unread message

Django

unread,
Apr 26, 2023, 3:22:14 AM4/26/23
to django-...@googlegroups.com
#34517: ImageField unnecessarily adds a post_init signal handler to the model
-------------------------------------+-------------------------------------
Reporter: Orhan | Owner: nobody
Hirsch |
Type: | Status: new
Cleanup/optimization |
Component: Database | Version: 4.1
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 1
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
While debugging some performance issues in a Django app, I found a
codepath where most of the time was being spent on initializing Django
models after fetching from the DB. It turns out that 30% of the time was
being spent on evaluating post_init signals because we were using
`ImageField`. However, the post_init signal handler is a noop because we
don't use the width_field / height_field.

If width_field and height_field are not set, removing the post_init signal
should have no effect since the signal handler will return right away.
Removing this signal handler gave us a 30-40% speedup on initializing
models where ImageField was used.

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

Django

unread,
Apr 26, 2023, 3:25:39 AM4/26/23
to django-...@googlegroups.com
#34517: ImageField unnecessarily adds a post_init signal handler to the model
-------------------------------------+-------------------------------------
Reporter: Orhan Hirsch | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: 4.2
(models, ORM) |
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 Orhan Hirsch):

* version: 4.1 => 4.2


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

Django

unread,
Apr 26, 2023, 3:36:02 AM4/26/23
to django-...@googlegroups.com
#34517: ImageField unnecessarily adds a post_init signal handler to the model
-------------------------------------+-------------------------------------
Reporter: orhanhenrik | Owner: nobody

Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: 4.2
(models, ORM) |
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 Mariusz Felisiak):

* stage: Unreviewed => Accepted


Comment:

Nice catch, thanks.

[https://github.com/django/django/pull/16801 PR]

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

Django

unread,
Apr 26, 2023, 5:15:08 AM4/26/23
to django-...@googlegroups.com
#34517: ImageField unnecessarily adds a post_init signal handler to the model
-------------------------------------+-------------------------------------
Reporter: orhanhenrik | Owner:
Type: | orhanhenrik
Cleanup/optimization | Status: assigned

Component: Database layer | Version: 4.2
(models, ORM) |
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 Mariusz Felisiak):

* owner: nobody => orhanhenrik
* status: new => assigned


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

Django

unread,
Apr 26, 2023, 6:53:07 AM4/26/23
to django-...@googlegroups.com
#34517: ImageField unnecessarily adds a post_init signal handler to the model
-------------------------------------+-------------------------------------
Reporter: Orhan Hirsch | Owner: Orhan
Type: | Hirsch

Cleanup/optimization | Status: assigned
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0

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

* needs_tests: 0 => 1


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

Django

unread,
May 3, 2023, 12:38:14 AM5/3/23
to django-...@googlegroups.com
#34517: ImageField unnecessarily adds a post_init signal handler to the model
-------------------------------------+-------------------------------------
Reporter: Orhan Hirsch | Owner: Orhan
Type: | Hirsch
Cleanup/optimization | Status: assigned
Component: Database layer | Version: 4.2
(models, ORM) |
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 Mariusz Felisiak):

* needs_tests: 1 => 0
* stage: Accepted => Ready for checkin


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

Django

unread,
May 3, 2023, 1:10:23 AM5/3/23
to django-...@googlegroups.com
#34517: ImageField unnecessarily adds a post_init signal handler to the model
-------------------------------------+-------------------------------------
Reporter: Orhan Hirsch | Owner: Orhan
Type: | Hirsch
Cleanup/optimization | Status: closed

Component: Database layer | Version: 4.2
(models, ORM) |
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 Mariusz Felisiak <felisiak.mariusz@…>):

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


Comment:

In [changeset:"ea53e7c09f1b8864c20c65976bbeaeab77abdaec" ea53e7c]:
{{{
#!CommitTicketReference repository=""
revision="ea53e7c09f1b8864c20c65976bbeaeab77abdaec"
Fixed #34517 -- Avoided connection post_init signal to ImageField without
width/height fields.
}}}

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

Django

unread,
Jun 19, 2023, 12:08:31 AM6/19/23
to django-...@googlegroups.com
#34517: ImageField unnecessarily adds a post_init signal handler to the model
-------------------------------------+-------------------------------------
Reporter: Orhan Hirsch | Owner: Orhan
Type: | Hirsch
Cleanup/optimization | Status: closed
Component: Database layer | Version: 4.2
(models, ORM) |
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
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"bcacc6321ae819965f478bfee2072a796801f298" bcacc63]:
{{{
#!CommitTicketReference repository=""
revision="bcacc6321ae819965f478bfee2072a796801f298"
Refs #34517 -- Restored skipping ImageFileField.update_dimension_fields
without width/height fields.

This avoids reading the image size when the dimensions fields
(image_width, image_height) do not exist, as that operation may be
expensive.

Partially reverts ea53e7c09f1b8864c20c65976bbeaeab77abdaec, that dropped
the check for the dimension fields in update_dimension_fields(), because
the post_init signal was no longer registered without dimension fields.

However, another code path to that function exists: when the
ImageFileField is save()d, the name from the storage is setattr()ed on
the field, and ImageFileDescriptor calls update_dimension_fields()
because the image size might have changed. Keep bailing out early when
dimensions are unused.

Besides, computing the image dimensions causes to close() the file,
resulting in a backward-incompatible change. The test protects against
that change.
}}}

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

Reply all
Reply to author
Forward
0 new messages