Skip to content

Commit 6f745f9

Browse files
committed
feat: cache error and null result formatters
1 parent 0b7ec3c commit 6f745f9

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

web3/_utils/method_formatters.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@
120120

121121
TValue = TypeVar("TValue")
122122

123+
CachedFormatters = Dict[
124+
Union[RPCEndpoint, Callable[..., RPCEndpoint]],
125+
Callable[[RPCResponse], Any],
126+
]
127+
123128

124129
def bytes_to_ascii(value: bytes) -> str:
125130
return codecs.decode(value, "ascii")
@@ -1260,19 +1265,28 @@ def get_result_formatters(
12601265
return compose(*partial_formatters, *formatters)
12611266

12621267

1268+
_error_formatters: CachedFormatters = {}
1269+
12631270
def get_error_formatters(
12641271
method_name: Union[RPCEndpoint, Callable[..., RPCEndpoint]],
12651272
) -> Callable[[RPCResponse], Any]:
12661273
# Note error formatters work on the full response dict
1267-
error_formatter_maps = (ERROR_FORMATTERS,)
1268-
formatters = combine_formatters(error_formatter_maps, method_name)
1274+
formatters = _error_formatters.get(method_name)
1275+
if formatters is None:
1276+
formatters = _error_formatters[method_name] = compose(
1277+
*combine_formatters((ERROR_FORMATTERS,), method_name)
1278+
)
1279+
return formatters
12691280

1270-
return compose(*formatters)
12711281

1282+
_null_result_formatters: CachedFormatters = {}
12721283

12731284
def get_null_result_formatters(
12741285
method_name: Union[RPCEndpoint, Callable[..., RPCEndpoint]],
12751286
) -> Callable[[RPCResponse], Any]:
1276-
formatters = combine_formatters((NULL_RESULT_FORMATTERS,), method_name)
1277-
1278-
return compose(*formatters)
1287+
formatters = _null_result_formatters.get(method_name)
1288+
if formatters is None:
1289+
formatters = _null_result_formatters[method_name] = compose(
1290+
*combine_formatters((NULL_RESULT_FORMATTERS,), method_name)
1291+
)
1292+
return formatters

0 commit comments

Comments
 (0)