8000 Improve `__setattr__` performance of Pydantic models by caching setter functions by MarkusSintonen · Pull Request #10868 · pydantic/pydantic · GitHub
[go: up one dir, main page]

Skip to content

Improve __setattr__ performance of Pydantic models by caching setter functions #10868

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

Merged
merged 5 commits into from
Nov 19, 2024

Conversation

MarkusSintonen
Copy link
Contributor
@MarkusSintonen MarkusSintonen commented Nov 18, 2024

Change Summary

Attribute setting has been pretty slow for BaseModel due to the extensive checks it has been doing for every __setattr__ call. PR improves performance of __setattr__ by memoizing the attribute specific handlers to the model class. This makes the attribute assigning some 7x faster. Also add missing benchmarks for attribute usage.

from timeit import timeit
from pydantic import BaseModel

class Model(BaseModel):
    field: int

model = Model(field=1)

def run():
    model.field = 2

# Before 1.048
# After 0.147
print(timeit(run, number=1000000))

Related issue number

fix #10853

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

Selected Reviewer: @sydney-runkle

@github-actions github-actions bot added the relnotes-fix Used for bugfixes. label Nov 18, 2024
Copy link
codspeed-hq bot commented Nov 18, 2024

CodSpeed Performance Report

Merging #10868 will not alter performance

Comparing MarkusSintonen:fast-setattr (404b8b7) with main (30ee4f4)

Summary

✅ 44 untouched benchmarks

🆕 2 new benchmarks

Benchmarks breakdown

Benchmark main MarkusSintonen:fast-setattr Change
🆕 test_getattr N/A 54 µs N/A
🆕 test_setattr N/A 87.7 µs N/A

Copy link
Contributor
github-actions bot commented Nov 18, 2024

Coverage report

Click to see where and how coverage changed

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

This report was generated by python-coverage-comment-action

@MarkusSintonen
Copy link
Contributor Author

please review

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.

Hi @MarkusSintonen,

Cool idea, thanks! I think memoization could be helpful here. Let me circle back with some colleagues to verify.

Specifically, @dmontagu, wdyt about this? I recall you've done a lot of work on these setattr branches.

@sydney-runkle sydney-runkle added relnotes-performance Used for performance improvements. and removed relnotes-fix Used for bugfixes. labels Nov 18, 2024
Copy link
Contributor
@dmontagu dmontagu left a comment

Choose a reason for hiding this comment

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

This is great!

Smart idea to memoize most of the checks that are really only dependent on the class and attribute name. I made a couple notes and generally defer to Sydney on specific style nits, but the overall approach seems sensible to me and a good improvement.

Consider the PR approved by me, at least conceptually; I'm just not explicitly approving due to the nit comments maybe meriting some minor changes before merging.

@sydney-runkle
Copy link
Contributor
sydney-runkle commented Nov 18, 2024

Consider the PR approved by me, at least conceptually

Fantastic, thanks for the prompt review.

I've give this a nit-picky review this evening, then we can move forward!

This makes me think more about what else we could potentially memoize in the schema gen department...

One other thing I want to make sure of - this doesn't leave us with any pickling issues? I don't think so, given passing tests, but we should check.

Copy link
Member
@Viicos Viicos left a comment

Choose a reason for hiding this comment

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

Thanks, I think this is a smart idea. We might have to worry about the size of the cache for large models (with a lot of fields). If we encounter such issues, we could use proper functions defined once instead of creating lambdas everytime

@MarkusSintonen
Copy link
Contributor Author

Thanks, I think this is a smart idea. We might have to worry about the size of the cache for large models (with a lot of fields). If we encounter such issues, we could use proper functions defined once instead of creating lambdas everytime

I wouldnt worry about size of it as anyways all the fields are listed in various ClassVars. However if we want to remove the tiny overhead of field name strs we could push the handler fn into eg FieldInfo/ModelPrivateAttr.

functions defined once

I purposely didnt want to touch the model generation side to not make it anymore heavier than it already is. Because of the mentioned large models it could just do work for no good reason in case fields are not even used like this.

@Viicos
Copy link 8000
Member
Viicos commented Nov 19, 2024

Not sure exactly what you mean by field name strs / didnt want to touch the model generation side, but what I wanted to say is we could do something like this:

HANDLERS = {
    'descriptor': lambda m, val: attribute.__set__(m, val),
    'cached_property': lambda m, val: m.__dict__.__setitem__(name, val),
    ...
}

def _setattr_handler(name: str, value: Any):
    ...
    if hasattr(attribute, '__set__'):
        return HANDLERS['descriptor']
    ...
    elif isinstance(attr, cached_property):
        return HANDLERS['cached_property']

So that we don't create a new function every time.

@Viicos Viicos changed the title Fix slow BaseModel.__setattr__ Improve __setattr__ performance of Pydantic models by caching setter functions Nov 19, 2024
@MarkusSintonen
Copy link
Contributor Author

but what I wanted to say is we could do something like this

Ah yes I see! That would make sense yes

@MarkusSintonen
Copy link
Contributor Author

@Viicos the suggestion now done here 7dfbe50

Copy link
Member
@Viicos Viicos left a comment

Choose a reason for hiding this comment

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

Thanks for adding the simple dict. This looks good to me.

Overall this approach can look a bit weird as calling __setattr__ (i.e. at the instance level) mutates a class variable that will be valid for every instance. But functionally it makes sense.

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.

Looking good! A few more questions / comments.

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.

Great work, thank you!

@sydney-runkle sydney-runkle merged commit addf1f9 into pydantic:main Nov 19, 2024
53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready for review relnotes-performance Used for performance improvements.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Slow setting an value of a basic Model
4 participants
0