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

Introduce absolute cached property #1100

Merged
merged 6 commits into from
Sep 4, 2024
Merged

Introduce absolute cached property #1100

merged 6 commits into from
Sep 4, 2024

Conversation

bdraco
Copy link
Member

@bdraco bdraco commented Sep 4, 2024

What do these changes do?

We call is_absolute quite often internally and externally. Currently this requires re-parsing the netloc. To make the API more consistent and perform better, absolute is now preferred over is_absolute. is_absolute is retained for backwards compatibility.

Are there changes in behavior for the user?

absolute is now preferred over is_absolute, however there are no plans to remove is_absolute.

Screenshot 2024-09-04 at 12 50 53 PM

is_absolute

Probably not the best representation of a microbenchmark since raw_host would previous be a cache miss most of the time and this change will perform much better either way:

>>> timeit.timeit("x.is_absolute()", globals=locals())
0.0817429160233587
>>> timeit.timeit("x.absolute", globals=locals())
0.06663041608408093

On previous version when raw_host is a cache miss

>>> from yarl import URL
>>> x=URL("")
>>> timeit.timeit("x.is_absolute()", globals=locals())
0.18370608310215175

We call is_absolute quite often internally and externally.
Curently this requires re-parsing the netloc. To make the
API more consistent and perform better, absolute is now
preferred over is_absolute. is_absolute is retained for
backwards compatibility.
@psf-chronographer psf-chronographer bot added the bot:chronographer:provided There is a change note present in this PR label Sep 4, 2024
Copy link

codecov bot commented Sep 4, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.91%. Comparing base (3d4b8ca) to head (a65a80c).
Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1100      +/-   ##
==========================================
+ Coverage   94.90%   94.91%   +0.01%     
==========================================
  Files          30       30              
  Lines        4358     4367       +9     
  Branches      382      383       +1     
==========================================
+ Hits         4136     4145       +9     
  Misses        196      196              
  Partials       26       26              
Flag Coverage Δ
CI-GHA 94.87% <100.00%> (+0.01%) ⬆️
MyPy 39.10% <77.77%> (-0.02%) ⬇️
OS-Linux 99.34% <100.00%> (+<0.01%) ⬆️
OS-Windows 99.44% <100.00%> (+<0.01%) ⬆️
OS-macOS 99.02% <100.00%> (+<0.01%) ⬆️
Py-3.10.11 98.91% <100.00%> (+<0.01%) ⬆️
Py-3.10.14 99.17% <100.00%> (+<0.01%) ⬆️
Py-3.11.9 99.17% <100.00%> (+<0.01%) ⬆️
Py-3.12.5 99.17% <100.00%> (+<0.01%) ⬆️
Py-3.13.0-rc.1 99.17% <100.00%> (+<0.01%) ⬆️
Py-3.8.10 98.85% <100.00%> (+<0.01%) ⬆️
Py-3.8.18 99.11% <100.00%> (+<0.01%) ⬆️
Py-3.9.13 98.85% <100.00%> (+<0.01%) ⬆️
Py-3.9.19 99.11% <100.00%> (+<0.01%) ⬆️
Py-pypy7.3.11 99.16% <100.00%> (+<0.01%) ⬆️
Py-pypy7.3.16 99.16% <100.00%> (+<0.01%) ⬆️
Py-pypy7.3.17 99.19% <100.00%> (+<0.01%) ⬆️
VM-macos-latest 99.02% <100.00%> (+<0.01%) ⬆️
VM-ubuntu-latest 99.34% <100.00%> (+<0.01%) ⬆️
VM-windows-latest 99.44% <100.00%> (+<0.01%) ⬆️
pytest 99.34% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

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

CHANGES/1100.feature.rst Outdated Show resolved Hide resolved
docs/api.rst Outdated Show resolved Hide resolved
CHANGES/1100.feature.rst Outdated Show resolved Hide resolved
@bdraco bdraco marked this pull request as ready for review September 4, 2024 22:59
@bdraco bdraco merged commit b631867 into master Sep 4, 2024
47 of 49 checks passed
@bdraco bdraco deleted the absolute branch September 4, 2024 22:59
@bdraco
Copy link
Member Author

bdraco commented Sep 4, 2024

Getting closer to the point where _url.py is below client_reqrep.py in the profile

@@ -1307,26 +1307,31 @@ def test_with_suffix_replace():
def test_is_absolute_for_relative_url():
url = URL("/path/to")
assert not url.is_absolute()
assert not url.absolute
Copy link
Member

Choose a reason for hiding this comment

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

@bdraco cached things are usually tested through their __wrapped__ attributes to prevent caching from influencing behavior in tests. Have you considered doing something like this?

Copy link
Member Author

Choose a reason for hiding this comment

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

Since the URL object is immutable and the state is already predefined we shouldn’t have much risk of the cached value being calculated incorrectly.

Was there a specific case you are concerned about?

Copy link
Member

Choose a reason for hiding this comment

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

No, just got surprised seeing this and thought I'd mention it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot:chronographer:provided There is a change note present in this PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants