Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #33546 -- Optimized ResponseHeaders and HttpResponseBase #15466

Merged
merged 5 commits into from Mar 2, 2022

Conversation

kezabelle
Copy link
Contributor

ticket is 33546.

Lets hope CI doesn't disprove me.

Timings, mirrored from ticket, on my local Macbook Air:

main patched
ResponseHeaders({}) 745 ns 313 ns
ResponseHeaders({'TEST': "1"}) 2.17 µs 1.8 µs
ResponseHeaders({'TEST': "1", "TEST2": "2"}) 3.51 µs 2.77 µs
ResponseHeaders({"Content-Type": "application/json"}) 2.19 µs 1.93 µs
HttpResponseBase() 7.89 µs 4.18 µs
HttpResponseBase(headers={"TEST": "1"}) 9.75 µs 6.02 µs
HttpResponseBase(headers={"TEST": "1", "TEST2": "2"}) 11 µs 7.09 µs
HttpResponseBase(headers={"Content-Type": "application/json"}) 3.71 µs 3.26 µs
HttpResponseBase(content_type="test") 4.48 µs 3.03 µs
HttpResponse() 9.06 µs 5.46 µs
HttpResponse(headers={"TEST": "1"}) 11.4 µs 7.58 µs
HttpResponse(headers={"TEST": "1", "TEST2": "2"}) 13 µs 8.83 µs
HttpResponse(headers={"Content-Type": "application/json"}) 5.22 µs 4.82 µs
HttpResponse(content_type="test") 6.2 µs 4.38 µs

As always, I'd welcome anyone point out flaws, additional timings to check, or counterpoint datapoints. Also scrutinise my logic, as I've been staring at this for too long ;)

Copy link
Member

@felixxm felixxm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kezabelle Thanks 👍

I pushed tiny edits.

django/http/response.py Outdated Show resolved Hide resolved
kezabelle and others added 5 commits March 2, 2022 20:23
…ucing the type-checking duplication.

In the common case, where keys and values are be encoded into
ascii/latin-1, defer the checking for newlines until it's been
successfully coerced to a string.

Co-authored-by: Nick Pope <nick@nickpope.me.uk>
This avoids scanning the Content-Type if it's empty, allowing the
Content-Type header itself to have a charset assigned without using
the re module.
…_init__().

This removes an extraneous conditional causing "Content-Type" to be
checked within the ResponseHeaders twice, if a content_type parameter
is provided.
@felixxm felixxm merged commit 95b7d01 into django:main Mar 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants