Move from out parameter to std::optional for pickling interface
Categories
(Core :: IPC, task)
Tracking
()
People
(Reporter: sergesanspaille, Assigned: sergesanspaille)
References
(Blocks 1 open bug)
Details
Attachments
(1 file)
The pickle interface is of the form bool ReadX(State*, out_type* result)
. This forms a natural barrier for the compiler when it fails to inline it.
In the context of -ftrivial-auto-var-init
, the following pattern:
int result;
if(ReadInt(state, &result)) {...}
is problematic because it's not possible for the compiler to ensure result
is always initialized before use, unless it can inline all the ReadInt
body, which it doesn't.
An alternative is to change the signature of ReadX
to std::optional<out_type> ReadX(State*)
. From the caller perspective, it always yield an initialized object, so that's compatible with -ftrivial-auto-var-init
, but it's also the prefered, modern approach. In many a case, the code generation is either equivalent to the previous interface, or better.
See https://godbolt.org/z/fdq4bb1zf and https://godbolt.org/z/e5TzcoTTh for examples of both interfaces.
Assignee | ||
Comment 1•2 years ago
|
||
std::optional has several benefit compared to using an out parameter: it
leads to better code generation as there are less pointer around for the
compiler to track.
This also prevents insertion by -ftrivial-auto-var-init of extra memory store
that show up in the profile hot map.
As a side effect, get rid of a memcpy
optimization that is no longer
relevant in 2023.
Updated•2 years ago
|
Description
•