Skip to content

Commit

Permalink
Cache get decoder class from string (#117)
Browse files Browse the repository at this point in the history
* Moved the logic from obtaining a decoder class from a string to its own method. This allows caching, which greatly improves performance.

* Added default maxsize arg to lru_cache for Python <3.8 compatibility

* Revert the caching changes of get_decoder_class, instead cache convert_type_string and compile the regex for faster execution.

* Revert the caching changes of get_decoder_class, instead cache convert_type_string and compile the regex for faster execution.

* Okay, actually reverted the initial changes this time.
  • Loading branch information
thewhaleking committed May 30, 2024
1 parent e8aaf0d commit f57f65b
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions scalecodec/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import re
import warnings
from abc import ABC, abstractmethod
from functools import lru_cache
from typing import Optional, TYPE_CHECKING, Union

from scalecodec.constants import TYPE_DECOMP_MAX_RECURSIVE
Expand Down Expand Up @@ -62,8 +63,11 @@ def __init__(self, config_id=None, ss58_format=None, only_primitives_on_init=Fal
self.only_primitives_on_init = only_primitives_on_init
self.ss58_format = ss58_format
self.implements_scale_info = implements_scale_info
self.arrow_match_re = re.compile(r'^([^<]*)<(.+)>$')
self.bracket_match_re = re.compile(r'^\[([A-Za-z0-9]+); ([0-9]+)\]$')

@classmethod
@lru_cache(maxsize=128)
def convert_type_string(cls, name):

name = re.sub(r'T::', "", name)
Expand Down Expand Up @@ -130,7 +134,7 @@ def get_decoder_class(self, type_string: Union[str, dict]):
if type_string[-1:] == '>':

# Extract sub types
type_parts = re.match(r'^([^<]*)<(.+)>$', type_string)
type_parts = self.arrow_match_re.match(type_string)

if type_parts:
type_parts = type_parts.groups()
Expand All @@ -151,7 +155,7 @@ def get_decoder_class(self, type_string: Union[str, dict]):
decoder_class.build_type_mapping()

elif type_string[0] == '[' and type_string[-1] == ']':
type_parts = re.match(r'^\[([A-Za-z0-9]+); ([0-9]+)\]$', type_string)
type_parts = self.bracket_match_re.match(type_string)

if type_parts:
type_parts = type_parts.groups()
Expand Down Expand Up @@ -1076,4 +1080,3 @@ def generate_type_decomposition(cls, _recursion_level: int = 0, max_recursion: i
return cls.__name__.lower()



0 comments on commit f57f65b

Please sign in to comment.