dataclasses-json
dataclasses-json copied to clipboard
Improve decoding performance of dataclasses-json
We improve performance of decoding by:
- Reducing the number of times
fields(obj)is calculated - Adding fast paths for the common types (e.g. int, float, str, ...)
- Avoid repeated creating of the same lambda function
Benchmark:
from dataclasses import dataclass
import dataclasses_json
import dataclasses_json.core
import timeit
@dataclasses_json.dataclass_json
@dataclass
class A:
s : str
i : int
b : bool
d : dict[str, str]
a=A('hi', 3, 3.14, {'a': 'dict'})
j= a.to_json()
a2=A.from_json(j)
print(timeit.timeit('a.to_json()', globals=globals(), number=100_000))
print(timeit.timeit('A.from_json(j)', globals=globals(), number=100_000))
Results on master:
2.1303018000908196
5.067091099917889
This PR:
2.1118524000048637
3.4828713997267187