Skip to content

Commit

Permalink
Use caching to improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
konstin committed Oct 5, 2019
1 parent 5d8b005 commit 56c3c9d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
10 changes: 8 additions & 2 deletions dataclasses_json/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions dataclasses_json/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import inspect
import sys
from datetime import datetime, timezone
from functools import lru_cache
from typing import Collection, Mapping, Optional


Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 56c3c9d

Please sign in to comment.