Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Only compute normalized decimal places if necessary indecimal_places_validator#11281

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Conversation

misrasaurabh1
Copy link
Contributor

📄decimal_places_validator inpydantic/_internal/_validators.py

✨ Performance Summary:

  • Speed Increase: 📈24% (0.24x faster)
  • Runtime Reduction: ⏱️ From182 microseconds down to147 microseconds (best of159 runs)

📝Explanation and details

Here are the changes made to the code for optimization.

  1. Calling_extract_decimal_digits_info function isn't required when(decimal_places_ > decimal_places) does not hold true.

Correctness verification

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

TestStatusDetails
⚙️ Existing Unit Tests🔘None Found
🌀 Generated Regression Tests23 PassedSee below
⏪ Replay Tests🔘None Found
🔎 Concolic Coverage Tests1 Passed
📊 Coverage100.00%

🌀 Generated Regression Tests Details

Click to view details
fromdecimalimportDecimalfromtypingimportAny# importsimportpytest# used for our unit testsfrompydantic._internal._validatorsimportdecimal_places_validatorfrompydantic_core._pydantic_coreimportPydanticKnownError# unit tests# Basic Test Casesdeftest_valid_decimal_exact_places():codeflash_output=decimal_places_validator(Decimal('1.23'),2)codeflash_output=decimal_places_validator(Decimal('0.123'),3)deftest_valid_decimal_less_places():codeflash_output=decimal_places_validator(Decimal('1.2'),2)codeflash_output=decimal_places_validator(Decimal('0.1'),3)deftest_invalid_decimal_more_places():withpytest.raises(PydanticKnownError):decimal_places_validator(Decimal('1.234'),2)withpytest.raises(PydanticKnownError):decimal_places_validator(Decimal('0.1234'),3)# Edge Test Casesdeftest_non_normalized_input():codeflash_output=decimal_places_validator(Decimal('1.2300'),3)codeflash_output=decimal_places_validator(Decimal('0.1230'),3)deftest_zero_value():codeflash_output=decimal_places_validator(Decimal('0'),0)codeflash_output=decimal_places_validator(Decimal('0.0'),1)deftest_negative_values():codeflash_output=decimal_places_validator(Decimal('-1.23'),2)codeflash_output=decimal_places_validator(Decimal('-0.123'),3)deftest_large_numbers():codeflash_output=decimal_places_validator(Decimal('123456789.123456789'),9)codeflash_output=decimal_places_validator(Decimal('123456789.1234567890'),10)# Invalid Inputsdeftest_non_decimal_types():withpytest.raises(TypeError):decimal_places_validator('1.23',2)withpytest.raises(TypeError):decimal_places_validator(1.23,2)withpytest.raises(TypeError):decimal_places_validator(None,2)# Boundary Conditionsdeftest_maximum_decimal_places():codeflash_output=decimal_places_validator(Decimal('1.'+'0'*28+'1'),29)withpytest.raises(PydanticKnownError):decimal_places_validator(Decimal('1.'+'0'*28+'1'),28)deftest_minimum_decimal_places():codeflash_output=decimal_places_validator(Decimal('1.0'),0)withpytest.raises(PydanticKnownError):decimal_places_validator(Decimal('1.1'),0)# Large Scale Test Casesdeftest_large_scale():codeflash_output=decimal_places_validator(Decimal('1.'+'0'*1000+'1'),1001)withpytest.raises(PydanticKnownError):decimal_places_validator(Decimal('1.'+'0'*1000+'1'),1000)# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

📣 **Feedback**

If you have any feedback or need assistance, feel free to join our Discord community:

Discord

codeflash-aibotand others added2 commitsDecember 3, 2024 23:41
Here are the changes made to the code for optimization.1. Avoided repeated calls to expensive functions like `len()`.2. Simplified some logic to minimize computations.3. Removed the redundant instances of re-extracting decimal info where possible.Points of optimization.- Avoided `assert` statement which is typically disabled in optimized mode.- Simplified the conditional blocks and reduced the number of function calls and length calculations.
@github-actionsgithub-actionsbot added the relnotes-fixUsed for bugfixes. labelJan 16, 2025
@codspeed-hqCodSpeed HQ
Copy link

codspeed-hqbot commentedJan 16, 2025
edited
Loading

CodSpeed Performance Report

Merging#11281 willnot alter performance

Comparingmisrasaurabh1:codeflash/optimize-decimal_places_validator-2024-12-03T23.41.41 (197ab22) withmain (dac74dc)

Summary

✅ 45 untouched benchmarks

@github-actionsGitHub Actions
Copy link
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  pydantic/_internal
  _validators.py
Project Total 

This report was generated bypython-coverage-comment-action

@ViicosViicos changed the title⚡️ Speed up functiondecimal_places_validator by 24% [codeflash]Only compute normalized decimal places if necessary indecimal_places_validatorJan 16, 2025
@ViicosViicosenabled auto-merge (squash)January 16, 2025 11:07
@ViicosViicos merged commit46f3075 intopydantic:mainJan 16, 2025
53 checks passed
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@ViicosViicosViicos approved these changes

Assignees
No one assigned
Labels
relnotes-fixUsed for bugfixes.
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

2 participants
@misrasaurabh1@Viicos

[8]ページ先頭

©2009-2025 Movatter.jp