Skip to content

Commit f57f65b

Browse files
authored
Cache get decoder class from string (#117)
* 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.
1 parent e8aaf0d commit f57f65b

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

scalecodec/base.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import re
1818
import warnings
1919
from abc import ABC, abstractmethod
20+
from functools import lru_cache
2021
from typing import Optional, TYPE_CHECKING, Union
2122

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

6669
@classmethod
70+
@lru_cache(maxsize=128)
6771
def convert_type_string(cls, name):
6872

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

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

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

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

156160
if type_parts:
157161
type_parts = type_parts.groups()
@@ -1076,4 +1080,3 @@ def generate_type_decomposition(cls, _recursion_level: int = 0, max_recursion: i
10761080
return cls.__name__.lower()
10771081

10781082

1079-

0 commit comments

Comments
 (0)