Reference counting讓我想起了Java,當如果想用C++來實現Java的能力的話,那Reference counting必不可少。Reference counting可以節省程序的運行成本,大量的構造、析構、分配、釋放和拷貝的代價被省略。
實現
classRCObject
{
public:
RCObject():refCount(0),shareable(true){}
RCObject(constRCObject&):refCount(0),shareable(true){}
RCObject& operator=(constRCObject& rhs){return *this;}
virtual ~RCObject()=0;
void AddReference(){++refCount;}
void RemoveReference(){if (--refCount == 0) deletethis;}
void markUnshareable(){shareable = false;}
bool isShareable() const{returnshareable;}
bool isShared() const {returnrefCount > 1;}
private:
int refCount;
bool shareable;
};
RCObject::~RCObject(){}
template <classT>
class RCPtr
{
public:
RCPtr(T* realPtr = 0):pointee(realPtr){init();}
RCPtr(constRCPtr& rhs):pointee(rhs.pointee){init();}
~RCPtr(){if (pointee) pointee->RemoveReference();}
RCPtr& operator = (constRCPtr& rhs)
{
if (pointee!=rhs.pointee)
{
if (pointee)
pointee->RemoveReference();
pointee = rhs.pointee;
init();
}
return *this;
}
T* operator->() const { returnpointee;}
T& operator*() const{return *pointee;}
private:
T* pointee;
void init()
{
if (pointee == 0)
return;
if (pointee->isShareable() == false)
pointee = newT(*pointee);
pointee->AddReference();
}
};
class String
{
public:
String(const char* value = ""):value(newStringValue(value)){}
const char& operator[](intnIndex) const
{
return value->data[nIndex];
}
char& operator[](intnIndex)
{
if (value->isShared())
value = newStringValue(value->data);
value->markUnshareable();
returnvalue->data[nIndex];
}
protected:
private:
struct StringValue:publicRCObject
{
char* data;
String Value(constchar* initValue)
{
init(initValue);
}
String Value(constStringValue& rhs)
{
init(rhs.data);
}
void init(constchar * initValue)
{
data = newchar[strlen(initValue) + 1];
strcpy(data,initValue);
}
~String Value()
{
delete [] data;
}
};
RCPtr<StringValue> value;
};
這是Meyers給出的String的實現,然而我的觀點是如果沒有特別的必要的話,對stirng最好不要使用引用計數,因為在多線程程序中同步的代價要大於引用計數本身的好處,得不償失。
如果StringValue是一個現成的類,無法修改它的實現,那怎麼辦?沒關系可以用委托,下面是一個典型的實現:
classRCObject
{
public:
RCObject():refCount(0),shareable(true){}
RCObject(constRCObject&):refCount(0),shareable(true){}
RCObject& operator=(constRCObject& rhs){return *this;}
virtual ~RCObject()=0;
void AddReference(){++refCount;}
void RemoveReference(){if (--refCount == 0) deletethis;}
void markUnshareable(){shareable = false;}
bool isShareable() const{returnshareable;}
bool isShared() const {returnrefCount > 1;}
private:
int refCount;
bool shareable;
};
RCObject::~RCObject(){}
template<classT>
class RCIPtr
{
public:
RCIPtr(T* realPtr = 0):counter(new CountHolder)
{
counter->pointee = realPtr;
init();
}
RCIPtr(constRCIPtr& rhs):counter(rhs.counter)
{
init();
}
~RCIPtr()
{
counter->RemoveReference();
}
RCIPtr& operator = (constRCIPtr& rhs)
{
if (counter != rhs.counter)
{
counter->RemoveReference();
counter = rhs.counter;
init();
}
return *this;
}
constT* operator->()const
{
returncounter->pointee;
}
T* operator->()
{
makeCopy();
returncounter->pointee;
}
constT& operator*() const
{
return *(counter->pointee);
}
T& operator*()
{
makeCopy();
return *(counter->pointee);
}
private:
struct CountHolder:publicRCObject
{
~Count Holder(){deletepointee;}
T* pointee;
};
Count Holder* counter;
void init()
{
if (counter->isShareable() == false)
{
T* oldValue = counter->pointee;
counter = newCountHolder;
counter->pointee = newT(*oldValue);
}
counter->AddReference();
}
void makeCopy()
{
if (counter->isShared())
{
T* oldValue = counter->pointee;
counter->RemoveReference();
counter = newCountHolder;
counter->pointee = newT(*oldValue);
counter->AddReference();
}
}
};
class Widget
{
public:
Widget(intSize){}
Widget(constWidget& rhs){}
~Widget(){}
Widget operator=(const Widget& rhs){}
void doThis(){printf("doThis()
");return;}
int showThat() const{printf("showThat()
"); return 0;}
protected:
private:
inti;
};
class RCWidget
{
public:
RCWidget(intsize):value(newWidget(size)){}
void doThis(){value->doThis();}
int showThat()const {returnvalue->showThat();}
protected:
private:
RCIPtr<Widget> value;
};
評估
實現引用計數是需要有前提的,不是所有的情況下,使用引用計數都是合適的。適合情況如下:
相對多的對象共享相對少量的實值。
對象的實值產生或者銷毀的成本很高,或者占用很多內存。
但是要記住,即使是Java也會有內存洩漏,不要指望小小的引用計數(上面簡單的實現)不會產生同樣的問題。
引用計數是一項很深奧的技術,想想Java,所以需要很謹慎的對待,但願它能帶來程序設計上的優化。