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

Use lazy imports for generated client #67

Merged
merged 1 commit into from
Nov 14, 2023
Merged

Use lazy imports for generated client #67

merged 1 commit into from
Nov 14, 2023

Conversation

robin-nitrokey
Copy link
Member

The auto-generated client is pretty large and importing the DefaultApi pulls in the data for all endpoints, so the load time is pretty high:

$ time python3 -c "import nethsm.client.apis.tags.default_api"
real    0m0.598s
user    0m0.563s
sys     0m0.036s

Previously, we had an unconditional import of the default_api module in the main nethsm module so that loading the nethsm module caused a significant delay in pynitrokey even if it is not used at all:

$ $ time python3 -c "import nethsm"
real    0m0.735s
user    0m0.634s
sys     0m0.035s

This patch changes the main nethsm module to only import the generated types if they are actually needed, reducing the load time significantly:

$ time python3 -c "import nethsm"
real    0m0.100s
user    0m0.084s
sys     0m0.016s

Further improvements could probably be achieved by using the endpoint classes directly instead of DefaultApi, but this is a less pressing issue as it only occurs if the client is actually used and performing the request introduces an additional delay.

The auto-generated client is pretty large and importing the DefaultApi
pulls in the data for all endpoints, so the load time is pretty high:

    $ time python3 -c "import nethsm.client.apis.tags.default_api"
    real    0m0.598s
    user    0m0.563s
    sys     0m0.036s

Previously, we had an unconditional import of the default_api module in
the main nethsm module so that loading the nethsm module caused a
significant delay in pynitrokey even if it is not used at all:

    $ $ time python3 -c "import nethsm"
    real    0m0.735s
    user    0m0.634s
    sys     0m0.035s

This patch changes the main nethsm module to only import the generated
types if they are actually needed, reducing the load time significantly:

    $ time python3 -c "import nethsm"
    real    0m0.100s
    user    0m0.084s
    sys     0m0.016s

Further improvements could probably be achieved by using the endpoint
classes directly instead of DefaultApi, but this is a less pressing
issue as it only occurs if the client is actually used and performing
the request introduces an additional delay.
@robin-nitrokey robin-nitrokey merged commit a662beb into main Nov 14, 2023
6 checks passed
@robin-nitrokey robin-nitrokey deleted the import branch November 14, 2023 09:04
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.

2 participants