Fatal1ty / mashumaro

Fast and well tested serialization library
Apache License 2.0
751 stars 44 forks source link

Improve union encoding performance #194

Closed Fatal1ty closed 5 months ago

Fatal1ty commented 5 months ago

Currently, we have to wrap int and float values on serialization with int(...) and float(...). This is used as a hack for poor union serialization logic which is based on applying an encoding operation for each type in the list until we get the first result without errors. If we have a value of type Union[int, DataClassA] then without applying int(value) (which will obviously fail on instances of DataClassA) we would pass instances of DataClassA as is because int is on the first place.

In this PR union serialization logic is changed so that we check the type of a value with pass-through strategy before returning it as is. It allows us to remove a hack for int and float and not try to perform encoding operations that are known to fail.

This pull request fixes the following issues: