싱글 톤 객체를 사용할 때 캐시 미스를 방지하는 방법이 있습니까?C++에서 싱글턴 캐시 미스 방지
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;
}
가 캐시 미스를 방지하는이 작업을 수행 할 수있는 더 나은 방법이 있나요 : 여기에 내 현재 싱글 구현입니까? 이것은 싱글 톤을 사용하지 않는 또 다른 이유입니까?
업데이트 : 정말 코드가 스택 언 와인딩과 GetSingleton() 호출에서 조건 검사에 대해 생성 한 많은 캐시와는 아무 상관 없었다 밝혀졌습니다. 싱글 톤을 명시 적으로 생성하고 파괴함으로써 (요구 생성 대신), 정적 인스턴스에 대한 접근자를 생성함으로써 오버 헤드를 많이 피할 수 있었고 프로파일 링에서 상당한 속도 향상을 알 수있었습니다.
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;
}
어디에서 캐시가 누락되는 걸까요? –
@Alenxandre C.'GetSingleton()'메서드 내에서 그 주석을 보지 않습니까? – Tom
@Tom : * cache miss *는 질문과 관련이없는 일반적인 의미를 가지고 있습니다. 여기서 문제는 질문이 명확하지 않다는 것, 그가 피하고 싶은 것은 무엇인가? 첫 번째 호출에서 포인터가 null이됩니까? 그것이 파괴 된 후에 그것은 null입니까? 그리고 캐시 미스로 용어를 섞어도 도움이되지 않습니다. –