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

Move lru cache from inside of _encode_host to outside #1348

Merged
merged 32 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
8debaf8
Move lru cache from inside of _encode_host to outside
bdraco Oct 21, 2024
056f2e5
remove unused
bdraco Oct 21, 2024
dea6b8e
docs
bdraco Oct 21, 2024
0a4a731
docs
bdraco Oct 21, 2024
944526c
docs
bdraco Oct 21, 2024
25c1377
docs syntax fixes
bdraco Oct 21, 2024
e9271f4
coverage
bdraco Oct 21, 2024
b0311b6
changelog
bdraco Oct 21, 2024
60ae823
bump ver
bdraco Oct 21, 2024
60c1b74
Update CHANGES/1348.breaking.rst
bdraco Oct 21, 2024
1441def
bump ver
bdraco Oct 21, 2024
9279ad6
Merge remote-tracking branch 'origin/move_cache_encode_host' into mov…
bdraco Oct 21, 2024
fb219d2
Update yarl/_url.py
bdraco Oct 21, 2024
0a7fdb8
keep _idna_encode
bdraco Oct 21, 2024
61ad89a
keep _idna_encode
bdraco Oct 21, 2024
861fed7
revert to 256 to check benchmark
bdraco Oct 21, 2024
40a16dc
split sizes
bdraco Oct 21, 2024
7a61562
Update docs/api.rst
bdraco Oct 21, 2024
e176f59
Apply suggestions from code review
bdraco Oct 21, 2024
e6d8744
Update docs/api.rst
bdraco Oct 21, 2024
fd9432b
split defaults
bdraco Oct 21, 2024
baa6966
Update CHANGES/1348.breaking.rst
bdraco Oct 21, 2024
0f4c481
Update docs/api.rst
bdraco Oct 21, 2024
6bd0f1a
Apply suggestions from code review
bdraco Oct 21, 2024
02318e5
fix
bdraco Oct 21, 2024
099abd9
Update yarl/_url.py
bdraco Oct 21, 2024
54bdd41
Update CHANGES/1348.breaking.rst
bdraco Oct 21, 2024
df3a97f
cleanup changelog message
bdraco Oct 21, 2024
5c891a5
Update CHANGES/1348.breaking.rst
bdraco Oct 21, 2024
04ea620
Merge branch 'master' into move_cache_encode_host
bdraco Oct 21, 2024
111da73
Merge branch 'master' into move_cache_encode_host
bdraco Oct 21, 2024
aa9afb4
Merge branch 'master' into move_cache_encode_host
bdraco Oct 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGES/1348.breaking.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Migrated to using a single cache for encoding hosts -- by :user:`bdraco`
bdraco marked this conversation as resolved.
Show resolved Hide resolved

Passing ``ip_address_size``, and ``host_validate_size`` to :py:meth:`~yarl.cache_configure` is deprecated in favor of the new ``encode_host`` parameter and will be removed in a future release.
bdraco marked this conversation as resolved.
Show resolved Hide resolved

For backwards compatibility, the old parameters affect the ``encode_host`` cache size.
bdraco marked this conversation as resolved.
Show resolved Hide resolved
bdraco marked this conversation as resolved.
Show resolved Hide resolved
23 changes: 15 additions & 8 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1039,18 +1039,18 @@ Cache control

IDNA conversion, host validation, and IP Address parsing used for host
encoding are quite expensive operations, that's why the ``yarl``
library caches these calls by storing last ``256`` results in the
library caches these calls by storing last ``512`` results in the
bdraco marked this conversation as resolved.
Show resolved Hide resolved
global LRU cache.
bdraco marked this conversation as resolved.
Show resolved Hide resolved

.. function:: cache_clear()

Clear IDNA, host validation, and IP Address caches.
Clear IDNA and host encoding cache.


.. function:: cache_info()

Return a dictionary with ``"idna_encode"``, ``"idna_decode"``, ``"ip_address"``,
bdraco marked this conversation as resolved.
Show resolved Hide resolved
and ``"host_validate"`` keys, each value
``"host_validate"``, and ``"encode_host"`` keys, each value
bdraco marked this conversation as resolved.
Show resolved Hide resolved
points to corresponding ``CacheInfo`` structure (see :func:`functools.lru_cache` for
details):

Expand All @@ -1060,20 +1060,27 @@ global LRU cache.
>>> yarl.cache_info()
{'idna_encode': CacheInfo(hits=5, misses=5, maxsize=256, currsize=5),
'idna_decode': CacheInfo(hits=24, misses=15, maxsize=256, currsize=15),
'ip_address': CacheInfo(hits=46933, misses=84, maxsize=256, currsize=101),
'host_validate': CacheInfo(hits=0, misses=0, maxsize=256, currsize=0)}
'encode_host': CacheInfo(hits=0, misses=0, maxsize=512, currsize=0)}

.. versionchanged:: 1.16

``ip_address``, and ``host_validate``
are deprecated in favor of a single ``encode_host`` cache.

.. function:: cache_configure(*, idna_encode_size=256, idna_decode_size=256, ip_address_size=256, host_validate_size=256)
.. function:: cache_configure(*, idna_encode_size=256, idna_decode_size=256, encode_host_size=512)

Set the IP Address, host validation, and IDNA encode and
decode cache sizes (``256`` for each by default).
Set the IP Address, host validation, and IDNA encode, host encode and
bdraco marked this conversation as resolved.
Show resolved Hide resolved
decode cache sizes.
bdraco marked this conversation as resolved.
Show resolved Hide resolved

Pass ``None`` to make the corresponding cache unbounded (may speed up host encoding
operation a little but the memory footprint can be very high,
please use with caution).

.. versionchanged:: 1.16

``ip_address_size`` and ``host_validate_size``
are deprecated in favor of a single ``encode_host`` cache.

References
----------

Expand Down
41 changes: 34 additions & 7 deletions tests/test_cache.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

import yarl

# Don't check the actual behavior but make sure that calls are allowed
Expand All @@ -13,7 +15,13 @@ def test_cache_clear() -> None:

def test_cache_info() -> None:
info = yarl.cache_info()
assert info.keys() == {"idna_encode", "idna_decode", "ip_address", "host_validate"}
assert info.keys() == {
"idna_encode",
"idna_decode",
"ip_address",
"host_validate",
"encode_host",
}


def test_cache_configure_default() -> None:
Expand All @@ -22,17 +30,36 @@ def test_cache_configure_default() -> None:

def test_cache_configure_None() -> None:
yarl.cache_configure(
idna_encode_size=None,
idna_decode_size=None,
ip_address_size=None,
host_validate_size=None,
idna_encode_size=None,
encode_host_size=None,
)


def test_cache_configure_explicit() -> None:
yarl.cache_configure(
idna_encode_size=128,
idna_decode_size=128,
ip_address_size=128,
host_validate_size=128,
idna_encode_size=128,
encode_host_size=128,
)


def test_cache_configure_waring() -> None:
msg = (
r"cache_configure\(\) no longer accepts the ip_address_size "
r"or host_validate_size arguments, they are used to set the "
r"encode_host_size instead and will be removed in the future"
)
with pytest.warns(DeprecationWarning, match=msg):
yarl.cache_configure(
idna_encode_size=1024,
idna_decode_size=1024,
ip_address_size=1024,
host_validate_size=1024,
)

assert yarl.cache_info()["encode_host"].maxsize == 1024
with pytest.warns(DeprecationWarning, match=msg):
yarl.cache_configure(host_validate_size=None)

assert yarl.cache_info()["encode_host"].maxsize is None
2 changes: 1 addition & 1 deletion yarl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
cache_info,
)

__version__ = "1.15.6.dev0"
__version__ = "1.16.0.dev0"

__all__ = (
"URL",
Expand Down
Loading
Loading