I am writing myself a little wrapper and I decided to cache values that shouldn't change (often or at all) for returning to the user, I'm coming from the premise that the WinAPI call is more expensive than returning my cached values, which I'm not sure if is true (or much more expensive to the point where it's worth it).

Here is my function:

const SYSTEM_INFO& getSystemInfo(const bool reCache)
        static SYSTEM_INFO nativeSystemInfo;
        static bool cached_nativeSystemInfo{ false };

        if (!cached_nativeSystemInfo || reCache)
            cached_nativeSystemInfo = true;

        return nativeSystemInfo;

After some research on GetNativeSystemInfo and SYSTEM_INFO in general, I've seen that under normal circunstances the return structure should never change, but it is not impossible for it to happen. That is why I came up with the reCache function parameter.

Would you understand what the reCache argument does even without documentation? Is this actually efficient? Would it be fit for it to be inlined?

  • \$\begingroup\$ Welcome to code review where we review working code from a project that you have written and provide suggestions on how to improve that code. From this single function it is very difficult to understand what the entire program is doing (what actions are you wrapping?), and how effective the caching is. This is an optimization, but without more code we can't really tell if this is an effective optimization or not. The question is currently off-topic due to lack of context, please see our guidelines. \$\endgroup\$
    – pacmaninbw
    Jul 24 '20 at 15:53