Skip to content

Improve decoding performance of dataclasses-json#552

Open
eendebakpt wants to merge 2 commits intolidatong:masterfrom
eendebakpt:decoding_performance
Open

Improve decoding performance of dataclasses-json#552
eendebakpt wants to merge 2 commits intolidatong:masterfrom
eendebakpt:decoding_performance

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