Skip to content

Implement lazy imports for numpy and beartype to improve startup time#658

Merged
yukinarit merged 1 commit into
mainfrom
optimize-import
Aug 13, 2025
Merged

Implement lazy imports for numpy and beartype to improve startup time#658
yukinarit merged 1 commit into
mainfrom
optimize-import

Conversation

@yukinarit

@yukinarit yukinarit commented Aug 11, 2025

Copy link
Copy Markdown
Owner

This PR improves serde import time from 200+ msec to 100+ msec.

before

$ python -X importtime -c "import serde" 2>&1 | grep -E "(serde|import time)" | tail -1
import time:       333 |     202650 | serde

after

$ python -X importtime -c "import serde" 2>&1 | grep -E "(serde|import time)" | tail -1
import time:       272 |     104262 | serde

Closes #604

@yukinarit yukinarit added the performance Performance improvements label Aug 11, 2025
@github-actions

Copy link
Copy Markdown

Please consider formatting your code according to the standards described here: https://github.com/yukinarit/pyserde/blob/main/CONTRIBUTING.md

@github-actions

Copy link
Copy Markdown

Please consider formatting your code according to the standards described here: https://github.com/yukinarit/pyserde/blob/main/CONTRIBUTING.md

@github-actions

Copy link
Copy Markdown

Please consider formatting your code according to the standards described here: https://github.com/yukinarit/pyserde/blob/main/CONTRIBUTING.md

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull Request Overview

This PR implements lazy imports for numpy and beartype dependencies to improve serde's startup time from 200+ msec to 100+ msec by deferring module imports until they are actually needed.

  • Replaces direct numpy imports with wrapper functions that lazily import numpy functions when called
  • Moves beartype and SQLAlchemy imports to be lazy-loaded at usage points
  • Adds local imports within functions that need numpy serialization/deserialization functionality

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
serde/se.py Replaces numpy imports with lazy wrapper functions and adds local imports in render method
serde/de.py Implements lazy numpy imports with wrapper functions and adds local imports for deserialization
serde/msgpack.py Moves numpy encode function import to point of usage
serde/json.py Moves numpy encode function import to point of usage
serde/core.py Adds blank line after beartype import
serde/compat.py Implements lazy SQLAlchemy import wrapper function

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment thread serde/compat.py
Comment thread serde/compat.py
Comment thread serde/se.py
Comment thread serde/de.py
@yukinarit yukinarit marked this pull request as ready for review August 13, 2025 06:18
@yukinarit yukinarit merged commit 45efa03 into main Aug 13, 2025
7 checks passed
@yukinarit yukinarit deleted the optimize-import branch August 13, 2025 06:18
@danielgafni

danielgafni commented Aug 13, 2025

Copy link
Copy Markdown

Just fyi there is this neat TID253 ruff rule you could use to enforce lazy imports for certain dependencies

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

Labels

performance Performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Optimize imports

3 participants