mirror of
https://github.com/zulip/zulip.git
synced 2025-11-10 08:56:10 +00:00
render_markdown_path: Convert dicts to dict item tuples for caching.
Calls to `render_markdown_path` weren't getting cached since the context argument is unhashable, and the `ignore_unhashable_lru_cache` decorator ignores such calls. This commit adds a couple of more decorators - one which converts dict arguments to the function to a dict items tuple, and another which converts dict items tuple arguments back to dicts. These two decorators used along with the `ignore_unhashable_lru_cache` decorator ensure that the calls to `render_markdown_path` with the context dict argument are also cached. The time to run zerver.tests.test_urls.PublicURLTest.test_public_urls drops by about 50% from 8.4s to 4.1s with this commit. The time to run zerver.tests.test_docs.DocPageTest.test_doc_endpoints drops by about 20% from 3.2s to 2.5s.
This commit is contained in:
committed by
Tim Abbott
parent
dd3ccbc897
commit
184c0b9870
@@ -534,3 +534,33 @@ def ignore_unhashable_lru_cache(maxsize: int=128, typed: bool=False) -> DECORATO
|
||||
return wrapper
|
||||
|
||||
return decorator
|
||||
|
||||
def dict_to_items_tuple(user_function: Callable[..., Any]) -> Callable[..., Any]:
|
||||
"""Wrapper that converts any dict args to dict item tuples."""
|
||||
def dict_to_tuple(arg: Any) -> Any:
|
||||
if isinstance(arg, dict):
|
||||
return tuple(sorted(arg.items()))
|
||||
return arg
|
||||
|
||||
def wrapper(*args: Any, **kwargs: Any) -> Any:
|
||||
new_args = (dict_to_tuple(arg) for arg in args)
|
||||
return user_function(*new_args, **kwargs)
|
||||
|
||||
return wrapper
|
||||
|
||||
def items_tuple_to_dict(user_function: Callable[..., Any]) -> Callable[..., Any]:
|
||||
"""Wrapper that converts any dict items tuple args to dicts."""
|
||||
def dict_items_to_dict(arg: Any) -> Any:
|
||||
if isinstance(arg, tuple):
|
||||
try:
|
||||
return dict(arg)
|
||||
except TypeError:
|
||||
pass
|
||||
return arg
|
||||
|
||||
def wrapper(*args: Any, **kwargs: Any) -> Any:
|
||||
new_args = (dict_items_to_dict(arg) for arg in args)
|
||||
new_kwargs = {key: dict_items_to_dict(val) for key, val in kwargs.items()}
|
||||
return user_function(*new_args, **new_kwargs)
|
||||
|
||||
return wrapper
|
||||
|
||||
Reference in New Issue
Block a user