-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
perf: remove TypeAdapter if has use validate_assignment. #5623
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a measurable impact on the application's performance with this change? We don't call parse_args()
very often - just at startup I believe.
It's hard to say no to faster startup, but I'm curious if there this does anything in practice besides add a few lines of code.
If the TypeAdapter has to be instantiated every time it is used, there will be a performance impact, and as stated in the documentation, it is possible to modify the number of times the test script is run to see this change. For now, it's just a startup call, so it's not a big performance hit. But I think this is a usage specification, so if you want to use TypeAdapter in a function, you can just introduce it into the function. |
I understand that that the change is objectively going to be a performance improvement, but I'm asking about the magnitude of benefit. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think TypeAdapter
is even necessary here.
This model uses validate_assignment=True
, so setting the CLI args is already automatically validated by pydantic. The TypeAdapters
are extraneous.
Depends on how many times the TypeAdapter is called in the code. Take 100 times as an example. Here are the gain multipliers for different types int 0.15
float 15.17
str 19.41
bool 18.27
date 30.53
datetime 45.37 |
I'm sorry, but I have not investigated the use of this model in the project and cannot give a conclusion. |
Sorry, to be clear, I'm asking if the change has a measurable impact on this project. It's OK if you haven't profiled it, was just curious.
I believe it is not needed, because calling If you can explain why it is needed, then we can accept this PR as a minor performance enhancement. Otherwise, the actual change that is needed is to remove the usage of |
I tested the behavior of validate_assignment.
Yes, I agree. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for fixing the deeper issue here
Simple test example:
output:
Refer to: https://docs.pydantic.dev/latest/concepts/performance/#typeadapter-instantiated-once