|
116 | 116 |
|
117 | 117 | TValue = TypeVar("TValue")
|
118 | 118 |
|
| 119 | +CachedFormatters = Dict[ |
| 120 | + Union[RPCEndpoint, Callable[..., RPCEndpoint]], |
| 121 | + Callable[[RPCResponse], Any], |
| 122 | +] |
| 123 | + |
119 | 124 |
|
120 | 125 | def bytes_to_ascii(value: bytes) -> str:
|
121 | 126 | return codecs.decode(value, "ascii")
|
@@ -1209,19 +1214,28 @@ def get_result_formatters(
|
1209 | 1214 | return compose(*partial_formatters, *formatters)
|
1210 | 1215 |
|
1211 | 1216 |
|
| 1217 | +_error_formatters: CachedFormatters = {} |
| 1218 | + |
1212 | 1219 | def get_error_formatters(
|
1213 | 1220 | method_name: Union[RPCEndpoint, Callable[..., RPCEndpoint]]
|
1214 | 1221 | ) -> Callable[[RPCResponse], Any]:
|
1215 | 1222 | # Note error formatters work on the full response dict
|
1216 |
| - error_formatter_maps = (ERROR_FORMATTERS,) |
1217 |
| - formatters = combine_formatters(error_formatter_maps, method_name) |
| 1223 | + formatters = _error_formatters.get(method_name) |
| 1224 | + if formatters is None: |
| 1225 | + formatters = _error_formatters[method_name] = compose( |
| 1226 | + *combine_formatters((ERROR_FORMATTERS,), method_name) |
| 1227 | + ) |
| 1228 | + return formatters |
1218 | 1229 |
|
1219 |
| - return compose(*formatters) |
1220 | 1230 |
|
| 1231 | +_null_result_formatters: CachedFormatters = {} |
1221 | 1232 |
|
1222 | 1233 | def get_null_result_formatters(
|
1223 | 1234 | method_name: Union[RPCEndpoint, Callable[..., RPCEndpoint]]
|
1224 | 1235 | ) -> Callable[[RPCResponse], Any]:
|
1225 |
| - formatters = combine_formatters((NULL_RESULT_FORMATTERS,), method_name) |
1226 |
| - |
1227 |
| - return compose(*formatters) |
| 1236 | + formatters = _null_result_formatters.get(method_name) |
| 1237 | + if formatters is None: |
| 1238 | + formatters = _null_result_formatters[method_name] = compose( |
| 1239 | + *combine_formatters((NULL_RESULT_FORMATTERS,), method_name) |
| 1240 | + ) |
| 1241 | + return formatters |
0 commit comments