yukinarit / pyserde

Yet another serialization library on top of dataclasses, inspired by serde-rs.
https://yukinarit.github.io/pyserde/guide/en
MIT License
735 stars 41 forks source link

Optimize imports #604

Open danielgafni opened 1 month ago

danielgafni commented 1 month ago

I've noticed pyserde contributes a fair share to my CLI app import time flamegraph (I'm using pyserde for app configuration so it has to be imported at top level).

Here is the breakdown for pyserde:

image

It's really nothing to worry about, but I guess dependencies like numpy and sqlalchemy could be imported in a lazy fashion in order to further improve import times.

This flamegraph can be made with tuna.

yukinarit commented 1 month ago

Hi @danielgafni!

Thanks for making a framegraph! I've never done this optimization.

Is it like moving all the imports to the code using it?

        elif is_numpy_array(c):
            from .numpy import deserialize_numpy_array_direct
            res = deserialize_numpy_array_direct(c, o)
danielgafni commented 1 month ago

Yes exactly. If you need them for type annotations, you can do the following:

from typing import TYPE_CHECKING

if TYPE_CHECKING:  # will NOT be executed at runtime
    import numpy as np

def my_func(arg: "np.ndarray"):
    ...
yukinarit commented 1 month ago

Looks good!

Would you be able to contribute to pyserde? :slightly_smiling_face:

danielgafni commented 1 month ago

Maybe! I'm short on time right now but it should improve in a few weeks

yukinarit commented 1 month ago

awesome!