Skip to content

Comments

⚡ Decrease import time with deferred OpenAPI model building#10546

Open
sam-mosleh wants to merge 2 commits intofastapi:masterfrom
sam-mosleh:decrease-import-defer-build
Open

⚡ Decrease import time with deferred OpenAPI model building#10546
sam-mosleh wants to merge 2 commits intofastapi:masterfrom
sam-mosleh:decrease-import-defer-build

Conversation

@sam-mosleh
Copy link

This pull request introduces a performance improvement for importing FastAPI, with the potential for up to a 40% reduction in import times. This optimization is particularly crucial for environments like AWS Lambda, where OpenAPI schemas are typically not required to get built during cold starts. The key enhancement in this PR is the utilization of the defer_build option in Pydantic, which has been available since version 2.3.0.

Performance Difference:

To quantify the impact of these changes, performance measurements were taken:

  • Baseline Version:
    • Command: python -X importtime -c "import fastapi"
    • Import Time: 608,727 microseconds
  • Optimized Version:
    • Command: FASTAPI_OPENAPI_DEFER_BUILD=true python -X importtime -c "import fastapi"
    • Import Time: 383,697 microseconds

@sam-mosleh
Copy link
Author

These files contain import time measurements that can be viewed to gain insight into the performance improvements achieved by this pull request:
import.log
import-defer-build.log

Copy link

@Uijeong97 Uijeong97 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I checked the effect of this PR, I see an improvement in import time performance from 2.4s -> 1.3s.

스크린샷 2023-12-25 오후 3 01 33

I want the PRs to merge.

@Jamim
Copy link
Contributor

Jamim commented Dec 28, 2023

Thanks you, @sam-mosleh!
This PR and #10589 might perfectly complement each other 🚀

Copy link
Member

@YuriiMotov YuriiMotov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sam-mosleh, thanks for working on this!

Can confirm significant improvement in import time (i got ~25% decrease).

If this is helpful for those who are using FastAPI on AWS Lambda or similar, I don't see any blockers to merge this.

If the idea is accepted, we will need to document it

return with_info_plain_validator_function(cls._validate)


_FASTAPI_OPENAPI_DEFER_BUILD = "FASTAPI_OPENAPI_DEFER_BUILD" in os.environ
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As an alternative:

_FASTAPI_OPENAPI_DEFER_BUILD = (
    os.environ.get("FASTAPI_OPENAPI_DEFER_BUILD", "").lower() in ("1", "true", "yes")
)

I think this would be a bit better since in current implementation it might be confusing if people set variable to "0" or "false"

@github-actions github-actions bot added the conflicts Automatically generated when a PR has a merge conflict label Dec 17, 2025
@github-actions
Copy link
Contributor

This pull request has a merge conflict that needs to be resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

conflicts Automatically generated when a PR has a merge conflict p4 refactor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants