-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Performance: remove methodtools/wirerope, use direct lru_cache()
- Loading branch information
Showing
4 changed files
with
44 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,7 +40,6 @@ install_requires = | |
requests | ||
jinja2 | ||
mappyfile | ||
methodtools | ||
jsonpath-rw | ||
orjson | ||
more-ds | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
"""Internal utils, not meant to be used outside schematools.""" | ||
|
||
import functools | ||
import weakref | ||
|
||
|
||
def cached_method(*lru_args, **lru_kwargs): | ||
"""A simple lru-cache per object. | ||
This removed the need for methodtools.lru_cache(), which uses wirerope for purity. | ||
The usage of wirerope started showing up as 5% of the request time, hence it's significant to remove. | ||
""" | ||
|
||
def decorator(func): | ||
@functools.wraps(func) | ||
def initial_wrapped_func(self, *args, **kwargs): | ||
# Not storing the wrapped method inside the instance. If we had | ||
# a strong reference to self the instance would never die. | ||
self_weak = weakref.ref(self) | ||
|
||
@functools.wraps(func) | ||
@functools.lru_cache(*lru_args, **lru_kwargs) | ||
def cached_method(*args, **kwargs): | ||
return func(self_weak(), *args, **kwargs) | ||
|
||
# Assigns to the self reference (preserving the cache), and optimizes the next access. | ||
setattr(self, func.__name__, cached_method) | ||
return cached_method(*args, **kwargs) | ||
|
||
return initial_wrapped_func | ||
|
||
return decorator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters