Skip to content

provide faster alternative dmr.compiled.request_accepts instead of django.http HttpRequest.accepts#854

Draft
albertedwardson wants to merge 7 commits intowemake-services:masterfrom
albertedwardson:accepts
Draft

provide faster alternative dmr.compiled.request_accepts instead of django.http HttpRequest.accepts#854
albertedwardson wants to merge 7 commits intowemake-services:masterfrom
albertedwardson:accepts

Conversation

@albertedwardson
Copy link
Copy Markdown
Contributor

Refs: #851

@albertedwardson albertedwardson marked this pull request as draft April 8, 2026 16:03
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Apr 8, 2026

Merging this PR will improve performance by 15.92%

⚡ 2 improved benchmarks
✅ 5 untouched benchmarks
🆕 3 new benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
🆕 test_accepted_header_compiled N/A 237.8 µs N/A
🆕 test_accepted_header_raw N/A 356.3 µs N/A
🆕 test_accepted_header_django_native N/A 1.5 ms N/A
test_negotiation_compiled 213.3 µs 184 µs +15.92%
test_negotiation_raw 298.7 µs 267.4 µs +11.71%

Comparing albertedwardson:accepts (d341057) with master (d1cb87b)

Open in CodSpeed

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 8, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (f5f72f5) to head (12efc5d).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##            master      #854   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          339       339           
  Lines        13632     13654   +22     
  Branches       426       429    +3     
=========================================
+ Hits         13632     13654   +22     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@sobolevn sobolevn marked this pull request as ready for review April 8, 2026 16:51
Copy link
Copy Markdown
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

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

Nice, great stuff! Please, add a changelog entry.

return accepted_type(header_value, (media_type,)) is not None


def request_accepts(request: _HasHeadersAsMapping, media_type: str) -> bool:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe accepted_header? Since we are going to remove the request.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

not fully sure about the resulted name and the docstring though... naming things is hard...

@albertedwardson
Copy link
Copy Markdown
Contributor Author

love the fact that even pure python one is still manages to be faster than native django impl :)

@sobolevn
Copy link
Copy Markdown
Member

Wow, great work! I obviously missed the ',' trick :)

# changed in the dict
self.params_str = ''.join(type_str.partition(';')[1:])
self.params_str = (
f';{type_str.partition(";")[2]}' if ';' in type_str else ''
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

you can use noqa here, since we are going for speed, not readability.

Suggested change
f';{type_str.partition(";")[2]}' if ';' in type_str else ''
f';{type_str.partition(";")[2]}' if ';' in type_str else '' # noqa: WPS237

return None


def accepts(request: HttpRequest, content_type: str) -> bool:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is a very different function. It checks that a selected renderer has a required content type.

Since you pass */* it always returns True now, when it should not, that's why tests fail :)

Let's add an argument strict: bool = True, when it is true pass '', when false, pass */*

also revert some things that i don't fully understand
@albertedwardson albertedwardson marked this pull request as draft April 10, 2026 11:08
@albertedwardson albertedwardson marked this pull request as draft April 10, 2026 11:08
key=_by_priority,
reverse=True,
)
if not accept_value or length_hint(provided_types) == 0:
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

mypyc calls python version of length_hint unfortunately, will revert

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants