From bffc8dec65459440bcd00c830f1e1c37874450a1 Mon Sep 17 00:00:00 2001 From: Yury Pliner Date: Mon, 11 Dec 2023 04:09:37 +0000 Subject: [PATCH] Cache get_pre_loads results --- marshmallow_recipe/hooks.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/marshmallow_recipe/hooks.py b/marshmallow_recipe/hooks.py index 55f0469..0d3f53a 100644 --- a/marshmallow_recipe/hooks.py +++ b/marshmallow_recipe/hooks.py @@ -4,6 +4,7 @@ _PRE_LOAD_KEY = "__marshmallow_recipe_pre_load__" _PRE_LOAD_CLASS_KEY = "__marshmallow_recipe_pre_load_class__" +_PRE_LOADS_CACHE = {} def pre_load(fn: collections.abc.Callable[..., Any]) -> collections.abc.Callable[..., Any]: @@ -12,12 +13,17 @@ def pre_load(fn: collections.abc.Callable[..., Any]) -> collections.abc.Callable def get_pre_loads(cls: type) -> list[collections.abc.Callable[..., Any]]: + existing = _PRE_LOADS_CACHE.get(cls) + if existing is not None: + return existing + result = [] for _, method in inspect.getmembers(cls): if hasattr(method, _PRE_LOAD_KEY): result.append(method) for fn in getattr(cls, _PRE_LOAD_CLASS_KEY, []): result.append(fn) + _PRE_LOADS_CACHE[cls] = result return result