C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Use optional<T> as though it were T

From: Jonathan Wakely <cxx_at_[hidden]>
Date: Wed, 28 Jun 2023 10:56:52 +0100
On Wed, 28 Jun 2023, 10:53 Breno GuimarĂ£es via Std-Proposals, <
std-proposals_at_[hidden]> wrote:

> I think you need an actual benchmark to be able to claim the performance
> quality of the implementation.
>
> For example, wouldn't it be better to just have as "isInitialized" thread
> local boolean and to access the object, you do:
>
>
> thread_local bool isInitialized = false;
> T& initialize() // mark with attribute to never inline
> {
> Emplace on optional with call_once (typing on my phone :) )
> isInitialized = true;
> Return *g_obj;
> }
>
> inline T& g_obj_fn() {
> if (isInitialized) // mark this with likely attribute
> return *g_obj:
> else return initialize();
> }
>
> Then all access to the variable just need to check a boolean (in the hot
> path) instead of calling an opaque function.
>
> I also don't see the need for a mutex on the initialization. The global
> call_once flag should be enough. The thread local variable shouldn't need
> to be atomic either.
>

And what has all this code got to do with std -proposals?

Received on 2023-06-28 09:57:07