-
Notifications
You must be signed in to change notification settings - Fork 15
Optimize: replace String with Cow<'_, str>
#60
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
base: main
Are you sure you want to change the base?
Conversation
|
@haydenhoang check it out. Does it look good? |
|
There is a problem which I have skipped, in OpenApi generation: Somehow it thinks that |
|
Hey @RoDmitry do you have any benchmark showing performance gain from replacing Doing that make the code more complex and harder to maintain (lifetimes, phantom data), and I think the user might prefer the easy use of |
|
Benchmark of the clones? I don't think that's necessary. That's an obvious optimization. There will be significant performance gain, if you benchmark it. I don't want to, because I understand how slow memory allocations are. User will not be bothered by the lifetimes much. It's either local lifetime or Although I agree that |
|
Wait, isn't the deserialized string owned anyway? Since the So I don't think the performance benefit is worth it. Saving nanoseconds by avoiding clones but added complexity to the code (especially the public API models) while a typical API request already has milliseconds of latency.
If the loop runs only a small number of times, which is typical, because each iteration makes an API call, the cost of cloning |
Yes.
Yes, but the API request is async, while clones are sync, and eat cpu time.
What complexity to the public API? Try it yourself, it doesn't change anything much. It's just an enum wrapper of borrowed/owned string. How is it hard to use?
Basically anything is negligible compared to network I/O. Does it mean that any optimizations are useless?
I clone it hundreds of times in a tight loop. Does it mean that I must struggle? Or just buy a better cpu? 😅 |
|
Or maybe you want to separate the request models from the response models? Because response models contain only owned Or another idea is to create the second model, but with all of the fields as owned types ( |
|
I think the following logic might be possible: preprocess openapi.yml to mark models used as input, then using that mark like |
b34654a to
b296ceb
Compare
b296ceb to
e236905
Compare
|
Optimizations are good as long as it overweigh the trade-offs. I found a post that explain this pretty well. The trade-offs here are adding more code/making the code less clear for some performance gain that we haven't even measured yet. I don't like the public API models to carry unnecessary phantomData and lifetimes because it's more clean that way.
Well, this attempt at optimization really does make our codebase more complex.
I think we should keep the type consistent across models. Would be nice if you can open a smaller PR for the internal derive optimization first. This big PR are slowing us down, right now I'm working on writing the documentation for the derive. |
a7d20be to
a47d6c6
Compare
bc3e2c6 to
bd3bfc9
Compare
|
I know that
But it is a trivially clear optimization! And Premature optimization is the practice of trying to improve a program's performance before it's finished. |
bd3bfc9 to
6ea7c90
Compare
6ea7c90 to
c195860
Compare
fc86156 to
351f164
Compare
351f164 to
2728a6c
Compare
|
I have removed all of the Code generation leaves some extra lifetimes (not a lot), which I have removed manually. After the merge, I will open an issue for it. Currently don't have extra time to fix it. |
a22f31a to
c272f23
Compare
1b24c60 to
5bf1a13
Compare
5bf1a13 to
12af80b
Compare
|
Will look into this later 👍 |
Less
Stringclones -> more performance 😊Also Derive optimizations.