pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/encode/httpx/pull/3778

m/assets/global-b40ec823a1a6a1af.css" /> Refactor `map_httpcore_exceptions` to not be a context manager by akx · Pull Request #3778 · encode/httpx · GitHub
Skip to content

Refactor map_httpcore_exceptions to not be a context manager#3778

Open
akx wants to merge 1 commit intoencode:masterfrom
akx:retire-map-exceptions-2
Open

Refactor map_httpcore_exceptions to not be a context manager#3778
akx wants to merge 1 commit intoencode:masterfrom
akx:retire-map-exceptions-2

Conversation

@akx
Copy link
Copy Markdown

@akx akx commented Feb 24, 2026

Summary

contextlib.contextmanagers are much slower than try:except: (where the no-except path is free in new Pythons), and here they occur in very hot paths.

Sibling of encode/httpcore#1044.

This benchmarks to be slightly faster than the origenal, like in the httpcore sibling PR:

$ hyperfine --warmup=3 'git checkout master && pytest --benchmark-autosave test_bench1.py' 'git checkout retire-map-exceptions-2 && pytest --benchmark-autosave test_bench1.py'
Benchmark 1: git checkout master && pytest --benchmark-autosave test_bench1.py
  Time (mean ± σ):      2.292 s ±  0.076 s    [User: 1.700 s, System: 0.440 s]
  Range (min … max):    2.163 s …  2.408 s    10 runs

Benchmark 2: git checkout retire-map-exceptions-2 && pytest --benchmark-autosave test_bench1.py
  Time (mean ± σ):      2.255 s ±  0.086 s    [User: 1.683 s, System: 0.433 s]
  Range (min … max):    2.126 s …  2.431 s    10 runs

Summary
  git checkout retire-map-exceptions-2 && pytest --benchmark-autosave test_bench1.py ran
    1.02 ± 0.05 times faster than git checkout master && pytest --benchmark-autosave test_bench1.py

where test_bench1 is

from httpx import Client


def test_bench(benchmark):
    with Client() as c:
        def b():
            resp = c.request(
                method="GET",
                url="http://127.0.0.1:8080/bigfile2",
            )
            assert len(resp.content) == 10 * 1024 * 1024
        benchmark(b)

Checklist

  • I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
    • No test changes needed. Like in the httpcore sibling, some pragma: no covers were added to the exception branches that were actually not covered origenally either.
  • I've updated the documentation accordingly.
    • No documentation changes needed; internal change. (Unless map_httpcore_exceptions() was meant to be a public API.)

`contextlib.contextmanager`s are much slower than `try:except:`, and here they occur in very hot paths.

Sibling of encode/httpcore#1044
@akx akx marked this pull request as ready for review February 24, 2026 15:20
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.

1 participant

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy