-
Notifications
You must be signed in to change notification settings - Fork 770
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 PyTuple_Pack
in fixed-size tuple conversions
#3296
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
For good measure I updated all of these to have concrete
inner
functions. Interestingly I found without#[inline]
here the benchmarks were about 3% slower. This makes me very tempted to go apply#[inline]
on all of theinner
functions following merge of #3273. (Probably a separate PR and I'll report on the performance impact.)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 think adding
#[inline]
everywhere would sort of make the point moot because this reintroduces the kind of code bloat this is supposed to fight?I would suggest to a) try this using thin LTO which should give the compiler more leeway in inlining this without generating these separately for each CGU and b) if we still want to use
#[inline]
, then to just drop theinner
layer completely because this is basically say the "code bloat" is worth it and not bloat at all.Furthermore, in this particular case, the body of
inner
is particularly small which suggests to me that the balance is tilted towards b). Especially so since the outer body is potentially large due to callingToPyObject
for each tuple component and the argument passing overhead is potentially large for the same reason.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.
Good points, here's some more thoughts:
lto = "thin"
and alsolto = "fat"
. Both provide a speedup over the default of no LTO. I did still observe that adding#[inline]
here has the same perf bump with thin lto. With fat lto there is no difference which is unsuprising.pydantic-core
does), so if we measure with LTO we're understanding how the most optimized code may perform. My gut says turning LTO on is correct (i.e. measuring most optimized is most appropriate). I would also accept a counter-argument that it's better to have LTO off so that we measure the base case.#[inline] fn inner
is marginally better than not having the concrete inner, because the LLVM IR will be smaller (one copy with aninline
hint, rather than the whole body for each monomorphization of the outer generic). We leave it to the optimizer to decide whether to follow theinline
hint and bloat the binary code. So I'd wager#[inline] fn inner
may be the best compromise of compile times and performance.PyTuple_New
and looping through arguments the body gets bigger. :)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 think they inform different decisions. The base case informs us about missing
#[inline]
attributes on trivial functions which really suffer from the call overhead, e.g. trait impls which should compile down to nothing.Benchmarks with LTO would help us with code size trade-offs like the one discussed here but would completely mask the previous issue for users who want good performance, but do not want to pay the compile-time cost for fat LTO. (Personally, I tend to prefer thin LTO outside of scientific code as it appears to be the sweet spot between performance and productivity.)
Since masking missing
#[inline]
on trivial function bodies is the more "catastrophic" failure mode IMHO, I would prefer to keep LTO off.I don't think this is how this currently works. Adding
#[inline]
means that in addition changing the inlining heuristics, a separate copy of the function is generated into each CGU in the same way generic functions are currently handled. Hence, I think using an#[inline] fn inner
inside the generic outer function would increase LLVM IR because each CGU potentially contains the monomorphizations of the outer function and a copy of the inlined inner function. (This reasoning might change when-Zshare-generics
stabilizes as generics might get more efficient because monomorphizations can be shared between CGU.)I also see the completely independent argument of preferring simpler code (i.e. no inner function at all) when the performance is basically equivalent. Adding additional layers like non-generic inner function should always yield measurable benefits. (Of course, we might not actually measure this in all cases when there is no trade-off involved like here.) Hence, I think keeping the dichotomy of just a generic function for throughput or a non-inlined non-generic inner function for code size is the more helpful approach.
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 agree with what you say here though I still defend that per-CGU having an
#[inline]
inner function works out as less LLVM IR per CGU. Instead of N instantiations of a full generic function without anyinner
I understand there would be N instantiaitions of the much reduced outer generic plus 1 instantiation of the inline inner.That said, I found that in #3321 adding
#[inline]
was actually slower than not. So clearly the moral of the story is we shouldn't try to beat the compiler, and I'll stay away from adding#[inline]
for now.