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 __class__ attribute for 5% speed gain. #1294

Merged
merged 3 commits into from
Mar 18, 2020

Conversation

StephenBrown2
Copy link
Contributor

Change Summary

Updates https://github.com/samuelcolvin/pydantic/tree/use-class-attribute on top of current master. It isn't 2x faster, which may be why the branch hasn't been merged yet, but it is faster, about 5% on average, as demonstrated below (avg=117.592μs/iter on master vs avg=111.204μs/iter on use-class-attribute):

# On branch master
❯ BENCHMARK_REPEATS=100 make benchmark-pydantic
python benchmarks/run.py pydantic-only
                                pydantic time=0.674s, success=50.10%
                                ...
                                pydantic time=0.670s, success=50.10%
                                pydantic best=0.647s, avg=0.706s, stdev=0.111s

                                pydantic best=107.908μs/iter avg=117.592μs/iter stdev=18.569μs/iter version=1.4a1

❯ git checkout use-class-attribute
Switched to branch 'use-class-attribute'
Your branch is up to date with 'mine/use-class-attribute'.

❯ BENCHMARK_REPEATS=100 make benchmark-pydantic
python benchmarks/run.py pydantic-only
                                pydantic time=0.824s, success=50.10%
                                ...
                                pydantic time=0.660s, success=50.10%
                                pydantic best=0.637s, avg=0.667s, stdev=0.032s

                                pydantic best=106.111μs/iter avg=111.204μs/iter stdev=5.374μs/iter version=1.4a1

Related issue number

None

Checklist

  • Unit tests for the changes exist
  • Tests pass on CI and coverage remains at 100%
  • Documentation reflects the changes where applicable
  • changes/<pull request or issue id>-<github username>.md file added describing change
    (see changes/README.md for details)

@samuelcolvin
Copy link
Member

interesting, I couldn't get a confirmed improvement.

Did you compile pydantic for the benchmarks?

@StephenBrown2
Copy link
Contributor Author

Yes, I got the same relative results after compiling (and recompiling):

❯ git checkout master && make clean build-cython benchmark-pydantic && git checkout use-class-attribute && make clean build-cython benchmark-pydantic
master:                 pydantic best=75.329μs/iter avg=85.574μs/iter stdev=11.919μs/iter version=1.4a1
use-class-attribute:    pydantic best=74.781μs/iter avg=81.755μs/iter stdev=8.646μs/iter version=1.4a1

@samuelcolvin
Copy link
Member

@StephenBrown2 could you fix tests?

@codecov
Copy link

codecov bot commented Mar 17, 2020

Codecov Report

Merging #1294 into master will not change coverage by %.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master    #1294   +/-   ##
=======================================
  Coverage   99.89%   99.89%           
=======================================
  Files          21       21           
  Lines        3713     3713           
  Branches      731      731           
=======================================
  Hits         3709     3709           
  Misses          2        2           
  Partials        2        2           
Impacted Files Coverage Δ
pydantic/dataclasses.py 100.00% <100.00%> (ø)
pydantic/env_settings.py 100.00% <100.00%> (ø)
pydantic/error_wrappers.py 100.00% <100.00%> (ø)
pydantic/fields.py 100.00% <100.00%> (ø)
pydantic/json.py 100.00% <100.00%> (ø)
pydantic/mypy.py 100.00% <100.00%> (ø)
pydantic/networks.py 100.00% <100.00%> (ø)
pydantic/schema.py 99.44% <100.00%> (ø)
pydantic/typing.py 100.00% <100.00%> (ø)
pydantic/utils.py 98.92% <100.00%> (ø)
... and 1 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update fcf5478...5572acb. Read the comment docs.

@StephenBrown2
Copy link
Contributor Author

StephenBrown2 commented Mar 17, 2020

Tests fixed!

@samuelcolvin
Copy link
Member

I think codecov is broken, I'll try and fix that and fix coverage later.

@StephenBrown2
Copy link
Contributor Author

StephenBrown2 commented Mar 17, 2020

Coverage actually updated, no change. The only line that's not covered is still if allow_names and field_name.isidentifier(): in utils.py

@samuelcolvin samuelcolvin merged commit 960b24a into pydantic:master Mar 18, 2020
@StephenBrown2 StephenBrown2 deleted the use-class-attribute branch May 1, 2020 21:52
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.

None yet

2 participants