In the event the internal function __init is called with an empty string the code will take unnecessary extra steps, in addition, the code generated might be overall greater because, to my understanding, when initializing a string with an empty const char* "" (like in this case), the compiler might be unable to deduce the string is indeed empty at compile time and more code is generated.
The goal of this patch is to make a new internal function that will accept just an error code skipping the empty string argument. It should skip the unnecessary steps and in the event if (ec) is false, it will return an empty string using the correct ctor, avoiding any extra code generation issues.
After the conversation about this patch matured in the libcxx channel on the LLVM Discord server, the patch was analyzed quickly with "Compiler Explorer" and other tools and it was discovered that it does indeed reduce the amount of code generated when using the latest stable clang version (16) which in turn produces faster code.
This patch targets LLVM 18 as it will break the ABI by addressing https://github.com/llvm/llvm-project/issues/63985
Benchmark tests run on other machines as well show in the best case, that the new version without the extra string as an argument performs 10 times faster.
On the buildkite CI run it shows the new code takes less CPU time as well.
In conclusion, the new code should also just appear cleaner because there are fewer checks to do when there is no message.
Just to put things in perspective, system_error is an exception type. I don't think we should pursue changes just for the sake of optimizing the construction of exception types.
However, mixed with https://github.com/llvm/llvm-project/issues/63985, I think this change could be worthwhile since we would both make this faster (ok, whatever) but also remove an unused function from our ABI surface (which is nice).
So personally I think the approach we should take here is to remove __init from our ABI and replace it with a function that is internal to system_error.cpp like you did here. Removing __init from the ABI will require a bit of preparation work to make sure it's safe but that's something we can help with if you update this patch with the full changes.
Just to set expectations, I would target this for LLVM 18, not LLVM 17.