Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve decoding performance of dataclasses-json #552

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

eendebakpt
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant