dataclasses-json icon indicating copy to clipboard operation
dataclasses-json copied to clipboard

Improve decoding performance of dataclasses-json

Open eendebakpt opened this issue 10 months ago • 0 comments

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

eendebakpt avatar Dec 17 '24 21:12 eendebakpt