Description
Restore in
optimization for class types
At first, in
parameters were optimized for class types.
Cppfront's optimization of in
parameters was faithful to
F.16: For “in” parameters, pass cheaply-copied types by value and others by reference to const
.
Then, #270 brought to light that the optimization didn't play well with Cpp2's order independence.
After #317, the net effect is that Cppfront never optimizes in
parameters of class type.
I still lament the fact that we lost this optimization to Cpp2 itself.
Similar to #317, here I propose to "improve in
parameter passing (restore old behavior, exclude problem cases only)".
#270 (comment) describes the general problem.
However, it generally doesn't apply to Cpp2.
Cpp2 generally requires parameters of complete type.
That's because most functions are initialized and will have a Cpp1 definition emitted.
So we can generally try to optimize in
parameters of class types to pass-by-value.
I'd love to have this optimization restored for the general case.
The conditions when applying cpp2::in
is wrong are two only (AFAIK).
- The case of [BUG]
in
argument passing to function that uses cpp2 user defined type failed with error #270.
In the current source order,
it only applies to parameters of type that come later in source order. - Virtual functions.
An uninitializedvirtual this
function can have parameters of incomplete type.
When declaring anoverride this
orfinal this
function in a different TU,
we can't know if itsin
parameters of class type had opted-into the optimization with the current Cppfront.
I propose that we identify these conditions,
and only when at least one of these conditions is true,
do we continue emit a version of cpp2::in
that doesn't optimize class types.
Otherwise, we should emit a version of cpp2::in
that does try to optimize class types.
Identified problems:
- In templates, the optimization is observable.
This is evident with function types (e.g., see feat: support function types #526 (comment)).
With this suggestion, the effect would, once again, extend to optimized class types. - Similar to fix(to_cpp1): improve recognition of dependent types and deducible parameters #533, we might need to do lexical analysis
to determine that a parameter's type indeed names a type that shouldn't be optimized.
References:
- [BUG]
in
argument passing to function that uses cpp2 user defined type failed with error #270. - [BUG]
in
argument passing to function that uses cpp2 user defined type failed with error #270 (comment). - [SUGGESTION] Add support for defining type aliases. #273 (comment).
- [BUG] Improve
cpp2::in
(restore old behavior, exclude problem cases only) #317. - feat: support function types #526 (comment).
- fix(to_cpp1): improve recognition of dependent types and deducible parameters #533.
- [SUGGESTION] Independent order and type data members #641 (comment).
- https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rf-in.
Related links:
Will your feature suggestion eliminate X% of security vulnerabilities of a given kind in current C++ code?
No.
Will your feature suggestion automate or eliminate X% of current C++ guidance literature?
Yes.
F.16: For “in” parameters, pass cheaply-copied types by value and others by reference to const
.
Describe alternatives you've considered.
None.