|
120 | 120 |
|
121 | 121 | TValue = TypeVar("TValue")
|
122 | 122 |
|
| 123 | +CachedFormatters = Dict[ |
| 124 | + Union[RPCEndpoint, Callable[..., RPCEndpoint]], |
| 125 | + Callable[[RPCResponse], Any], |
| 126 | +] |
| 127 | + |
123 | 128 |
|
124 | 129 | def bytes_to_ascii(value: bytes) -> str:
|
125 | 130 | return codecs.decode(value, "ascii")
|
@@ -1260,19 +1265,28 @@ def get_result_formatters(
|
1260 | 1265 | return compose(*partial_formatters, *formatters)
|
1261 | 1266 |
|
1262 | 1267 |
|
| 1268 | +_error_formatters: CachedFormatters = {} |
| 1269 | + |
1263 | 1270 | def get_error_formatters(
|
1264 | 1271 | method_name: Union[RPCEndpoint, Callable[..., RPCEndpoint]],
|
1265 | 1272 | ) -> Callable[[RPCResponse], Any]:
|
1266 | 1273 | # 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 |
1269 | 1280 |
|
1270 |
| - return compose(*formatters) |
1271 | 1281 |
|
| 1282 | +_null_result_formatters: CachedFormatters = {} |
1272 | 1283 |
|
1273 | 1284 | def get_null_result_formatters(
|
1274 | 1285 | method_name: Union[RPCEndpoint, Callable[..., RPCEndpoint]],
|
1275 | 1286 | ) -> 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