Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7892207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:48:45+00:00 2026-06-03T06:48:45+00:00

Is there a way to prevent a cache miss when using singleton objects? Here’s

  • 0

Is there a way to prevent a cache miss when using singleton objects? Here’s my current singleton implementation:

SingletonObject.h

#pragma once

class SingletonObject
{
public:
    static SingletonObject* SingletonObject();
    static void SingletonObject();
private:
    static SingletonObject* sSingletonObject;
    SingletonObject();
    ~SingletonObject();
};

SingletonObject.cpp

#include "SingletonObject.h"

SingletonObject* SingletonObject::sSingletonObject = NULL;

SingletonObject:: SingletonObject()
{
}

SingletonObject::~ SingletonObject()
{
}

SingletonObject* SingletonObject::GetSingleton()
{
    if (sSingletonObject == NULL) // cache miss
    {
        sSingletonObject = new SingletonObject();
    }
    return sSingletonObject;  
}

void SingletonObject::DestroySingleton()
{
    delete sSingletonObject; 
    sSingletonObject = NULL;
}

Is there a better way to do this that prevents a cache miss? Is this just another reason to not use singletons?


Update: Turns out it really was nothing to do with cache as much as the code generated for stack unwinding and the conditional check in the GetSingleton() call. By explicitly creating and destroying the singleton (instead of demand-creating it), and creating an accessor for the static instance, I was able to avoid much of the overhead and noted a significant speedup in profiling.

SingletonObject.h

#pragma once

class SingletonObject {
public:
    static void CreateSingleton();
    static void DestroySingleton();
    static inline SingletonObject* GetSingleton() { return sInstance; }
private:
    static SingletonObject* sInstance;

    SingletonObject();
}

SingletonObject.cpp

#include "SingletonObject.h"

void SingletonObject::CreateSingleton() {
    if (sInstance == NULL)
        sInstance = new SingletonObject();`
}

void SingletonObject::DestroySingleton() {
    delete(sInstance);
    sInstance = NULL;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T06:48:46+00:00Added an answer on June 3, 2026 at 6:48 am

    This is a very specific question people arrive to way down the performance optimization road. Are you sure you’re all the way there? Reason I’m asking is if you access your Singleton frequently enough, the pointer to the object will stay in cache. If it’s not in cache, then you’re not accessing the object frequently enough, which means you don’t really need it, so prefetching the pointer (or the object) to cache will just steal precious cache space from something that you’re using more often in reality – this might even hurt the performance in a long run.
    In my understanding arriving the question you’re at currently you’d have to go through the following steps:

    1. profile your application to find out that static SingletonObject* SingletonObject(); function really is a hot spot (>10% of the overall time is spend executing this one function)
    2. profile your application with event-based sampling collector (like Intel VTune) to find out that cache misses are responsible for this function’s execution time. Which it does not have to be. It could just be the number of calls you make to the function (do a call count).
    3. And after figuring out that the pointer is not in cache (which cache by the way? L1 or L2 or LLC? L1 and L2 are quite small and latency to access L2 is ~10 cycles, so L1 miss is not a huge problem) you’d go through your code to figure out why it is not. Meaning you’d look at the amount of data being accessed in between calls to static SingletonObject* SingletonObject(); and inspect if all those accesses are necessary. If they are, then it’s a justified cache miss and you can’t do anything about it. If they’re not, then reduce your working set by as much as you can and re-run the profiler (step 2).
    4. Only when you’re done with 1-3 and you’re still seeing cache misses on access to Singleton object and you see that this hurts the performance, only then you put _mm_prefetch() calls in your code prior to accessing the Singleton object.
    5. And then go through 1-3 once again (well, at least step 1) to make sure that step 4 improved the performance as opposed to hurting it, which it could by polluting your chosen level of cache.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to prevent users from getting into SQL Server Management Studio
Is there a way to prevent Castle Windsor from automatically injecting dependencies into properties
Is there a way to prevent a NSDrawer from being resized? I've tried setting
Is there a way to prevent empty elements of the form <myElement/> being used
Is there a way to prevent a user viewing an file but still use
Is there a way to prevent a component from rendering in Flex (to save
Is there a way to prevent developers from committing code when certain unsafe or
Is there any way to prevent or detect a man-in-the-middle attack over plain HTTP?
HI, Is there a way to prevent a particular dll in C# being opened
Slightly daft, but... Is there a way to prevent Visual Studio treating a .jpg

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.