From 56c3c9d6338f75b170818826241563eb3a3b7375 Mon Sep 17 00:00:00 2001 From: konstin Date: Sat, 5 Oct 2019 14:46:41 +0200 Subject: [PATCH 1/2] Use caching to improve performance --- dataclasses_json/core.py | 10 ++++++++-- dataclasses_json/utils.py | 2 ++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/dataclasses_json/core.py b/dataclasses_json/core.py index df1af0da..35318d1b 100644 --- a/dataclasses_json/core.py +++ b/dataclasses_json/core.py @@ -6,6 +6,7 @@ from datetime import datetime, timezone from decimal import Decimal from enum import Enum +from functools import lru_cache from typing import Collection, Mapping, Union, get_type_hints from uuid import UUID @@ -98,6 +99,11 @@ def _decode_letter_case_overrides(field_names, overrides): return names +@lru_cache(maxsize=128) +def _get_type_hints_cached(cls): + return get_type_hints(cls) + + def _decode_dataclass(cls, kvs, infer_missing): if isinstance(kvs, cls): return kvs @@ -116,7 +122,7 @@ def _decode_dataclass(cls, kvs, infer_missing): kvs[field.name] = None init_kwargs = {} - types = get_type_hints(cls) + types = _get_type_hints_cached(cls) for field in fields(cls): # The field should be skipped from being added # to init_kwargs as it's not intended as a constructor argument. @@ -196,7 +202,7 @@ def _support_extended_types(field_type, field_value): res = field_value return res - +@lru_cache(maxsize=128) def _is_supported_generic(type_): not_str = not _issubclass_safe(type_, str) is_enum = _issubclass_safe(type_, Enum) diff --git a/dataclasses_json/utils.py b/dataclasses_json/utils.py index 5cd993c1..10a738e5 100644 --- a/dataclasses_json/utils.py +++ b/dataclasses_json/utils.py @@ -1,6 +1,7 @@ import inspect import sys from datetime import datetime, timezone +from functools import lru_cache from typing import Collection, Mapping, Optional @@ -62,6 +63,7 @@ def _isinstance_safe(o, t): return result +@lru_cache(maxsize=128) def _issubclass_safe(cls, classinfo): try: return issubclass(cls, classinfo) From cbe4d87b432c49b63534a790d4dc638d7db907ab Mon Sep 17 00:00:00 2001 From: konstin Date: Sat, 5 Oct 2019 14:47:02 +0200 Subject: [PATCH 2/2] Add caching to _user_overrides --- dataclasses_json/core.py | 1 + 1 file changed, 1 insertion(+) diff --git a/dataclasses_json/core.py b/dataclasses_json/core.py index 35318d1b..a199abe2 100644 --- a/dataclasses_json/core.py +++ b/dataclasses_json/core.py @@ -45,6 +45,7 @@ def default(self, o) -> Json: return result +@lru_cache(maxsize=128) def _user_overrides(cls): confs = ['encoder', 'decoder', 'mm_field', 'letter_case'] FieldOverride = namedtuple('FieldOverride', confs)