Skip to content

Conversation

misrasaurabh1
Copy link
Contributor

@misrasaurabh1 misrasaurabh1 commented Jul 23, 2024

Change Summary

📄 apply_each_item_validators() in pydantic/_internal/_generate_schema.py

📈 Performance improved by 100% (1.00x faster)

⏱️ Runtime went down from 108 microseconds to 54.1 microseconds

Explanation and details

Below is an optimized version of the same Python program. The changes aim to improve execution speed by reducing redundant operations and simplifying some parts of the code.

Changes Made.

  1. Avoid Redundant Type Checking: In apply_each_item_validators, added a quick return if each_item_validators is empty.
  2. Simplified Schema Handling: Minimized redundant dictionary key accesses and introduced the use of get with default values.

These optimizations should enhance the performance and maintainability of the code without changing its external behavior.

This optimization was automatically discovered with codeflash.ai

Correctness verification

The new optimized code was tested for correctness. The results are listed below.

🔘 (none found) − ⚙️ Existing Unit Tests

✅ 0 Passed − 🌀 Generated Regression Tests

✅ 200 Passed − ⏪ Replay Tests

Checklist

  • The pull request title is a good summary of the changes - it will be used in the changelog
  • Unit tests for the changes exist
  • Tests pass on CI
  • Documentation reflects the changes where applicable
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

codeflash-ai bot and others added 4 commits July 18, 2024 23:32
Below is an optimized version of the same Python program. The changes aim to improve execution speed by reducing redundant operations and simplifying some parts of the code.



### Changes Made.
1. **Use of `slots` in `dataclass`**: Added `slots=True` to the `ValidatorDecoratorInfo` and `Decorator` classes to reduce memory usage.
2. **Streamlined `build` method**: Consolidated the logic within the `build` method to streamline function unwrapping and avoid repeated code.
3. **Avoid Redundant Type Checking**: In `apply_each_item_validators`, added a quick return if `each_item_validators` is empty.
4. **Simplified Schema Handling**: Minimized redundant dictionary key accesses and introduced the use of `get` with default values.

These optimizations should enhance the performance and maintainability of the code without changing its external behavior.
@github-actions github-actions bot added the relnotes-fix Used for bugfixes. label Jul 23, 2024
Copy link

codspeed-hq bot commented Jul 23, 2024

CodSpeed Performance Report

Merging #9950 will not alter performance

Comparing codeflash-ai:codeflash/optimize-apply_each_item_validators-2024-07-18T23.32.37 (9aaa3cf) with main (ee3e3b1)

Summary

✅ 14 untouched benchmarks

@sydney-runkle sydney-runkle added relnotes-performance Used for performance improvements. and removed relnotes-fix Used for bugfixes. labels Jul 24, 2024
Copy link
Contributor

@sydney-runkle sydney-runkle left a comment

Choose a reason for hiding this comment

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

Happy with this overall, one change request.

I'm not seeing super major performance benefits across our benchmarks, but those can certainly be expanded. Happy to accept this as a general improvement.

@sydney-runkle sydney-runkle added the awaiting author revision awaiting changes from the PR author label Jul 24, 2024
Co-authored-by: Sydney Runkle <54324534+sydney-runkle@users.noreply.github.com>
Copy link
Contributor

@sydney-runkle sydney-runkle left a comment

Choose a reason for hiding this comment

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

Looks good now, thanks!

@sydney-runkle sydney-runkle merged commit c38f36f into pydantic:main Jul 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting author revision awaiting changes from the PR author relnotes-performance Used for performance improvements.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants