SlideShare a Scribd company logo
1 of 14
Copy-On-Write in C++/COW/Lazy Copy
2013-11
AGENDA
1. How to avoid object copy in C/C++ program?
2. An example: DataBuffer/Employee
3. Summary: How to implement COW?
4. Overhead: COW is worthy to big object, and lots of copying
but few writing
5. TypeFactory<T> in GOAM
How to avoid object copy in C/C++ program?
• Passing/return reference(&):
void foo(const std::vector<int>& data);
const std::string& XsdDNDescription::toString();
• Passing/return pointer(*):
void bar(std::vector<int>* pData);
DmAttribute* DmTable::getDmAttribute(const DmAttributeId& aid) const;
• Passing/return smart pointer:
void hoo(const boost::shared_ptr<std::list<int> > pData);
DmObject_SmartPtr DmTable::_CreateCachedObject(const DmObjectId& objId_);
• COW: 既想占引用 / 智能指针的好处 ( 延迟 / 避免不必要的拷贝操作,提高
性能 ) ,又想保持对象之间的独立性 ( 写时 ) 。
R4.3, /vob/oam_generic_srv/src/xsdFunction/Src/XsdDNDescription.cc, line 158:
const string& XsdDNDescription::toString()
{
string buffer ="";
for (DnSyntaxMap::const_iterator
iter = _dnSyntaxMap.begin();
iter != _dnSyntaxMap.end();
iter++)
{
buffer = buffer + iter->second->toString() + " / ";
}
return buffer; // !!! 错误 : 返回本地变量的引用 !!!
}
Widely used in Singleton Pattern & STL!
About Smart pointer,
Do you have any
question?
An example: class DataBuffer
class DataBuffer
{
private:
typedef std::vector<int> _InternalBuffer;
_InternalBuffer m_Buffer;
public:
DataBuffer() { m_Buffer.reserve(1024); }
DataBuffer(const DataBuffer& right) { }
DataBuffer& operator=(const DataBuffer& right) { }
~DataBuffer() { }
int GetValue(size_t pos) { return m_Buffer.at(pos); }
……
void AddValue(int newValue) { m_Buffer.push_back(newValue); }
void SetValue(size_t pos, int newValue) { m_Buffer[pos] = newValue; }
void RemoveValue(size_t pos) { m_Buffer.erase (m_Buffer.begin() + pos); }
……
};
先想一想,如果是你,
你会怎样去封装并实现
这样一个类?
1. int* + operator new;
2. Huge int[];
3. std::vector<int>;
4. std::deque<int>;
5. ……
如果 class DataBuffer 的使用上下文里会经常对这种对象
进行拷贝、赋值操作,或者存在大量的这种拷贝行为,但
是修改对象的地方却较少,那么这种 deep-copy 带来的额
外开销会很明显。该如何避免这种无谓的开销?
An example: DataBuffer with COW
class DataBuffer
{
private:
typedef boost::shared_ptr<std::vector<int> > _InternalBuffer_SmartPtr;
_InternalBuffer_SmartPtr m_pBuffer;
public:
DataBuffer() { /* How to do: new std::vector<int> or do-nothing? */ }
DataBuffer(const DataBuffer& right) { … /* How to implement? */ }
DataBuffer& operator=(const DataBuffer& right) {… /* How to implement? */ }
~DataBuffer() { }
// reading data…
int GetValue(size_t pos) {
if (m_pBuffer != NULL)
return m_pBuffer->at(pos);
else …
}
……
// writing data…
void AddValue(int newValue) { … /* How to implement? */ }
void SetValue(size_t pos, int newValue) { … /* How to implement? */ }
void RemoveValue(size_t pos) { … /* How to implement? */ }
……
};
DataBuffer with COW – Only copying
DataBuffer with COW – AddValue(250) 独占
共享
独占
X
DataBuffer with COW – AddValue() coding
class DataBuffer
{
private:
typedef boost::shared_ptr<std::vector<int> > _InternalBuffer_SmartPtr;
_InternalBuffer_SmartPtr m_pBuffer;
public:
// writing data…
void AddValue(int newValue)
{
if (m_pBuffer.get() == NULL) {
m_pBuffer = _InternalBuffer_SmartPtr(new std::vector<int>);
}
if (m_pBuffer.unique()) { // exclusive!
m_pBuffer->push_back(newValue);
} else { // sharing with other objects!
_InternalBuffer_SmartPtr pBackup = m_pBuffer; // make backup!
m_pBuffer = _InternalBuffer_SmartPtr(new std::vector<int>(*pBackup));
m_pBuffer->push_back(newValue);
}
}
void SetValue(size_t pos, int newValue) { … /* How to implement? */ }
void RemoveValue(size_t pos) { … /* How to implement? */ }
……
};
DataBuffer with COW – AddValue()
refactoringclass DataBuffer
{
private:
typedef boost::shared_ptr<std::vector<int> > _InternalBuffer_SmartPtr;
_InternalBuffer_SmartPtr m_pBuffer;
public:
// writing data…
void AddValue(int newValue)
{
_InternalBuffer_SmartPtr pBackup = m_pBuffer; /* make backup! */
try
{
if (m_pBuffer.get() == NULL) {
m_pBuffer = _InternalBuffer_SmartPtr(new std::vector<int>); /* empty! */
}
if (! m_pBuffer.unique()) { /* sharing! */
m_pBuffer = _InternalBuffer_SmartPtr(new std::vector<int>(*pBackup)); /* copy it! */
}
m_pBuffer->push_back(newValue);
}
catch (…)
{
m_pBuffer = pBackup; /* rollback! */
throw; /* rethrow */
}
} // end of AddValue
……
};
COW in big non-collection object
class Employee
{
private:
struct _Employee_structure
{
std::string m_jobNumber;
std::string m_formalName;
std::string m_nickName;
std::string m_idCardNumber;
Datetime m_birthDate;
unsigned int m_workingAge;
……
};
typedef boost::shared_ptr<_Employee_structure> _Internal_SmartPtr;
_Internal_SmartPtr m_pData;
public:
// writing data…
void IncrementWorkingAge()
{
_Internal_SmartPtr pBackup = m_pData; /* make backup! */
try {
if (m_pData.get() == NULL) {
m_pData = _Internal_SmartPtr(new _Employee_structure(…));
}
if (! m_pData.unique()) { /* sharing! */
m_pData = _Internal_SmartPtr(new _Employee_structure(*pBackup)); /* copy it! */
}
(m_pData->m_workingAge) ++;
}
catch (…) {
m_pData = pBackup; /* rollback! */
throw; /* rethrow */
}
} // end of IncrementWorkingAge
……
};
How to implement COW in your class?
• Smart Pointer is necessary;
• Keep sharing with others when there is only copy operation;
• Stop sharing and make new copy from original data, then change it (add,
modify, remove);
• COW: From outside, each DataBuffer/Employee object is completely
independent; but in it’s underlying implementation, the real data is shared
or exclusive;
COW overhead
• Smart Pointer give us an additional indirectness;
• Always allocate memory dynamically(new/delete, because of Smart
pointer);
• Additional “if…else…” branches;
• Compare all above overheads with the benefits to us: COW is worthy
to big object, and with lots of copying operations but few
writing(few amount and few opportunity);
TypeFactor<T> in GOAM
* All of TypeInt, TypeString, TypeFloat, TypeBool, TypeDn are TypeFactory<>
with special data type as template parameter.
TypeFactor<T> in GOAM (cont.)

More Related Content

What's hot

#OOP_D_ITS - 3rd - Pointer And References
#OOP_D_ITS - 3rd - Pointer And References#OOP_D_ITS - 3rd - Pointer And References
#OOP_D_ITS - 3rd - Pointer And References
Hadziq Fabroyir
 
Dynamics allocation
Dynamics allocationDynamics allocation
Dynamics allocation
Kumar
 
13. dynamic allocation
13. dynamic allocation13. dynamic allocation
13. dynamic allocation
웅식 전
 
OOP 2012 - Hint: Dynamic allocation in c++
OOP 2012 - Hint: Dynamic allocation in c++OOP 2012 - Hint: Dynamic allocation in c++
OOP 2012 - Hint: Dynamic allocation in c++
Allan Sun
 
Chapter 7 - Input Output Statements in C++
Chapter 7 - Input Output Statements in C++Chapter 7 - Input Output Statements in C++
Chapter 7 - Input Output Statements in C++
Deepak Singh
 
Aae oop xp_06
Aae oop xp_06Aae oop xp_06
Aae oop xp_06
Niit Care
 

What's hot (20)

#OOP_D_ITS - 3rd - Pointer And References
#OOP_D_ITS - 3rd - Pointer And References#OOP_D_ITS - 3rd - Pointer And References
#OOP_D_ITS - 3rd - Pointer And References
 
Pointers
PointersPointers
Pointers
 
built in function
built in functionbuilt in function
built in function
 
Dynamics allocation
Dynamics allocationDynamics allocation
Dynamics allocation
 
Wcbpijwbpij new
Wcbpijwbpij newWcbpijwbpij new
Wcbpijwbpij new
 
Hotel Management In C++
Hotel Management In C++Hotel Management In C++
Hotel Management In C++
 
Take advantage of C++ from Python
Take advantage of C++ from PythonTake advantage of C++ from Python
Take advantage of C++ from Python
 
13. dynamic allocation
13. dynamic allocation13. dynamic allocation
13. dynamic allocation
 
Notes about moving from python to c++ py contw 2020
Notes about moving from python to c++ py contw 2020Notes about moving from python to c++ py contw 2020
Notes about moving from python to c++ py contw 2020
 
Introduction to c part 2
Introduction to c   part  2Introduction to c   part  2
Introduction to c part 2
 
Lo17
Lo17Lo17
Lo17
 
OOP 2012 - Hint: Dynamic allocation in c++
OOP 2012 - Hint: Dynamic allocation in c++OOP 2012 - Hint: Dynamic allocation in c++
OOP 2012 - Hint: Dynamic allocation in c++
 
Cpp tutorial
Cpp tutorialCpp tutorial
Cpp tutorial
 
String Manipulation Function and Header File Functions
String Manipulation Function and Header File FunctionsString Manipulation Function and Header File Functions
String Manipulation Function and Header File Functions
 
Chapter 7 - Input Output Statements in C++
Chapter 7 - Input Output Statements in C++Chapter 7 - Input Output Statements in C++
Chapter 7 - Input Output Statements in C++
 
C++ programs
C++ programsC++ programs
C++ programs
 
Rumus VB-2
Rumus VB-2Rumus VB-2
Rumus VB-2
 
Aae oop xp_06
Aae oop xp_06Aae oop xp_06
Aae oop xp_06
 
P2
P2P2
P2
 
Lab 13
Lab 13Lab 13
Lab 13
 

Viewers also liked

20120117083445611 1
20120117083445611 120120117083445611 1
20120117083445611 1
ibelenpr
 
準備好最後只剩你自己
 準備好最後只剩你自己 準備好最後只剩你自己
準備好最後只剩你自己
Johnson Gmail
 
How do you increase your pinterest followers
How do you increase your pinterest followersHow do you increase your pinterest followers
How do you increase your pinterest followers
maria784
 

Viewers also liked (14)

Fil12001
Fil12001Fil12001
Fil12001
 
20120117083445611 1
20120117083445611 120120117083445611 1
20120117083445611 1
 
76. munajat penempuh jalan thariqat
76. munajat penempuh jalan thariqat76. munajat penempuh jalan thariqat
76. munajat penempuh jalan thariqat
 
Barracuda Message Archiver presentatie bij ActiveView
Barracuda Message Archiver presentatie bij ActiveViewBarracuda Message Archiver presentatie bij ActiveView
Barracuda Message Archiver presentatie bij ActiveView
 
準備好最後只剩你自己
 準備好最後只剩你自己 準備好最後只剩你自己
準備好最後只剩你自己
 
How do you increase your pinterest followers
How do you increase your pinterest followersHow do you increase your pinterest followers
How do you increase your pinterest followers
 
Net2 event slides USA updated March 30 2016
Net2 event slides USA   updated March 30 2016Net2 event slides USA   updated March 30 2016
Net2 event slides USA updated March 30 2016
 
Website button-narrow
Website button-narrowWebsite button-narrow
Website button-narrow
 
Plug n play office space for lease in gurgaon
Plug n play office space for lease in gurgaonPlug n play office space for lease in gurgaon
Plug n play office space for lease in gurgaon
 
Servicio tecnico secadora Torrent - 96.393.63.43
Servicio tecnico secadora Torrent - 96.393.63.43Servicio tecnico secadora Torrent - 96.393.63.43
Servicio tecnico secadora Torrent - 96.393.63.43
 
Evaluación económica
Evaluación económicaEvaluación económica
Evaluación económica
 
Akhlak terhadap diri sendiri
Akhlak terhadap diri sendiriAkhlak terhadap diri sendiri
Akhlak terhadap diri sendiri
 
Cruise control devices
Cruise control devicesCruise control devices
Cruise control devices
 
環保署開徵畜牧業水污染防治費
環保署開徵畜牧業水污染防治費環保署開徵畜牧業水污染防治費
環保署開徵畜牧業水污染防治費
 

Similar to COW

All I know about rsc.io/c2go
All I know about rsc.io/c2goAll I know about rsc.io/c2go
All I know about rsc.io/c2go
Moriyoshi Koizumi
 
Micro-ORM Introduction - Don't overcomplicate
Micro-ORM Introduction - Don't overcomplicateMicro-ORM Introduction - Don't overcomplicate
Micro-ORM Introduction - Don't overcomplicate
Kiev ALT.NET
 
I really need help with my C++ assignment. The following is the info.pdf
I really need help with my C++ assignment. The following is the info.pdfI really need help with my C++ assignment. The following is the info.pdf
I really need help with my C++ assignment. The following is the info.pdf
wasemanivytreenrco51
 
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docxfilesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
ssuser454af01
 
This is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdfThis is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdf
kostikjaylonshaewe47
 

Similar to COW (20)

Day 1
Day 1Day 1
Day 1
 
All I know about rsc.io/c2go
All I know about rsc.io/c2goAll I know about rsc.io/c2go
All I know about rsc.io/c2go
 
Micro-ORM Introduction - Don't overcomplicate
Micro-ORM Introduction - Don't overcomplicateMicro-ORM Introduction - Don't overcomplicate
Micro-ORM Introduction - Don't overcomplicate
 
Lecture5
Lecture5Lecture5
Lecture5
 
C++ Language
C++ LanguageC++ Language
C++ Language
 
Writing MySQL UDFs
Writing MySQL UDFsWriting MySQL UDFs
Writing MySQL UDFs
 
Oops presentation
Oops presentationOops presentation
Oops presentation
 
I really need help with my C++ assignment. The following is the info.pdf
I really need help with my C++ assignment. The following is the info.pdfI really need help with my C++ assignment. The following is the info.pdf
I really need help with my C++ assignment. The following is the info.pdf
 
Oop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer MelayiOop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer Melayi
 
Roadmap to Object Oriented Programming
Roadmap to Object Oriented ProgrammingRoadmap to Object Oriented Programming
Roadmap to Object Oriented Programming
 
"Optimization of a .NET application- is it simple ! / ?", Yevhen Tatarynov
"Optimization of a .NET application- is it simple ! / ?",  Yevhen Tatarynov"Optimization of a .NET application- is it simple ! / ?",  Yevhen Tatarynov
"Optimization of a .NET application- is it simple ! / ?", Yevhen Tatarynov
 
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docxfilesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
 
C++_notes.pdf
C++_notes.pdfC++_notes.pdf
C++_notes.pdf
 
C++ lab assignment
C++ lab assignmentC++ lab assignment
C++ lab assignment
 
Overloading
OverloadingOverloading
Overloading
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
Intro
IntroIntro
Intro
 
C++ manual Report Full
C++ manual Report FullC++ manual Report Full
C++ manual Report Full
 
This is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdfThis is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdf
 
OO-like C Programming: Struct Inheritance and Virtual Function
OO-like C Programming: Struct Inheritance and Virtual FunctionOO-like C Programming: Struct Inheritance and Virtual Function
OO-like C Programming: Struct Inheritance and Virtual Function
 

COW

  • 2. AGENDA 1. How to avoid object copy in C/C++ program? 2. An example: DataBuffer/Employee 3. Summary: How to implement COW? 4. Overhead: COW is worthy to big object, and lots of copying but few writing 5. TypeFactory<T> in GOAM
  • 3. How to avoid object copy in C/C++ program? • Passing/return reference(&): void foo(const std::vector<int>& data); const std::string& XsdDNDescription::toString(); • Passing/return pointer(*): void bar(std::vector<int>* pData); DmAttribute* DmTable::getDmAttribute(const DmAttributeId& aid) const; • Passing/return smart pointer: void hoo(const boost::shared_ptr<std::list<int> > pData); DmObject_SmartPtr DmTable::_CreateCachedObject(const DmObjectId& objId_); • COW: 既想占引用 / 智能指针的好处 ( 延迟 / 避免不必要的拷贝操作,提高 性能 ) ,又想保持对象之间的独立性 ( 写时 ) 。 R4.3, /vob/oam_generic_srv/src/xsdFunction/Src/XsdDNDescription.cc, line 158: const string& XsdDNDescription::toString() { string buffer =""; for (DnSyntaxMap::const_iterator iter = _dnSyntaxMap.begin(); iter != _dnSyntaxMap.end(); iter++) { buffer = buffer + iter->second->toString() + " / "; } return buffer; // !!! 错误 : 返回本地变量的引用 !!! } Widely used in Singleton Pattern & STL! About Smart pointer, Do you have any question?
  • 4. An example: class DataBuffer class DataBuffer { private: typedef std::vector<int> _InternalBuffer; _InternalBuffer m_Buffer; public: DataBuffer() { m_Buffer.reserve(1024); } DataBuffer(const DataBuffer& right) { } DataBuffer& operator=(const DataBuffer& right) { } ~DataBuffer() { } int GetValue(size_t pos) { return m_Buffer.at(pos); } …… void AddValue(int newValue) { m_Buffer.push_back(newValue); } void SetValue(size_t pos, int newValue) { m_Buffer[pos] = newValue; } void RemoveValue(size_t pos) { m_Buffer.erase (m_Buffer.begin() + pos); } …… }; 先想一想,如果是你, 你会怎样去封装并实现 这样一个类? 1. int* + operator new; 2. Huge int[]; 3. std::vector<int>; 4. std::deque<int>; 5. …… 如果 class DataBuffer 的使用上下文里会经常对这种对象 进行拷贝、赋值操作,或者存在大量的这种拷贝行为,但 是修改对象的地方却较少,那么这种 deep-copy 带来的额 外开销会很明显。该如何避免这种无谓的开销?
  • 5. An example: DataBuffer with COW class DataBuffer { private: typedef boost::shared_ptr<std::vector<int> > _InternalBuffer_SmartPtr; _InternalBuffer_SmartPtr m_pBuffer; public: DataBuffer() { /* How to do: new std::vector<int> or do-nothing? */ } DataBuffer(const DataBuffer& right) { … /* How to implement? */ } DataBuffer& operator=(const DataBuffer& right) {… /* How to implement? */ } ~DataBuffer() { } // reading data… int GetValue(size_t pos) { if (m_pBuffer != NULL) return m_pBuffer->at(pos); else … } …… // writing data… void AddValue(int newValue) { … /* How to implement? */ } void SetValue(size_t pos, int newValue) { … /* How to implement? */ } void RemoveValue(size_t pos) { … /* How to implement? */ } …… };
  • 6. DataBuffer with COW – Only copying
  • 7. DataBuffer with COW – AddValue(250) 独占 共享 独占 X
  • 8. DataBuffer with COW – AddValue() coding class DataBuffer { private: typedef boost::shared_ptr<std::vector<int> > _InternalBuffer_SmartPtr; _InternalBuffer_SmartPtr m_pBuffer; public: // writing data… void AddValue(int newValue) { if (m_pBuffer.get() == NULL) { m_pBuffer = _InternalBuffer_SmartPtr(new std::vector<int>); } if (m_pBuffer.unique()) { // exclusive! m_pBuffer->push_back(newValue); } else { // sharing with other objects! _InternalBuffer_SmartPtr pBackup = m_pBuffer; // make backup! m_pBuffer = _InternalBuffer_SmartPtr(new std::vector<int>(*pBackup)); m_pBuffer->push_back(newValue); } } void SetValue(size_t pos, int newValue) { … /* How to implement? */ } void RemoveValue(size_t pos) { … /* How to implement? */ } …… };
  • 9. DataBuffer with COW – AddValue() refactoringclass DataBuffer { private: typedef boost::shared_ptr<std::vector<int> > _InternalBuffer_SmartPtr; _InternalBuffer_SmartPtr m_pBuffer; public: // writing data… void AddValue(int newValue) { _InternalBuffer_SmartPtr pBackup = m_pBuffer; /* make backup! */ try { if (m_pBuffer.get() == NULL) { m_pBuffer = _InternalBuffer_SmartPtr(new std::vector<int>); /* empty! */ } if (! m_pBuffer.unique()) { /* sharing! */ m_pBuffer = _InternalBuffer_SmartPtr(new std::vector<int>(*pBackup)); /* copy it! */ } m_pBuffer->push_back(newValue); } catch (…) { m_pBuffer = pBackup; /* rollback! */ throw; /* rethrow */ } } // end of AddValue …… };
  • 10. COW in big non-collection object class Employee { private: struct _Employee_structure { std::string m_jobNumber; std::string m_formalName; std::string m_nickName; std::string m_idCardNumber; Datetime m_birthDate; unsigned int m_workingAge; …… }; typedef boost::shared_ptr<_Employee_structure> _Internal_SmartPtr; _Internal_SmartPtr m_pData; public: // writing data… void IncrementWorkingAge() { _Internal_SmartPtr pBackup = m_pData; /* make backup! */ try { if (m_pData.get() == NULL) { m_pData = _Internal_SmartPtr(new _Employee_structure(…)); } if (! m_pData.unique()) { /* sharing! */ m_pData = _Internal_SmartPtr(new _Employee_structure(*pBackup)); /* copy it! */ } (m_pData->m_workingAge) ++; } catch (…) { m_pData = pBackup; /* rollback! */ throw; /* rethrow */ } } // end of IncrementWorkingAge …… };
  • 11. How to implement COW in your class? • Smart Pointer is necessary; • Keep sharing with others when there is only copy operation; • Stop sharing and make new copy from original data, then change it (add, modify, remove); • COW: From outside, each DataBuffer/Employee object is completely independent; but in it’s underlying implementation, the real data is shared or exclusive;
  • 12. COW overhead • Smart Pointer give us an additional indirectness; • Always allocate memory dynamically(new/delete, because of Smart pointer); • Additional “if…else…” branches; • Compare all above overheads with the benefits to us: COW is worthy to big object, and with lots of copying operations but few writing(few amount and few opportunity);
  • 13. TypeFactor<T> in GOAM * All of TypeInt, TypeString, TypeFloat, TypeBool, TypeDn are TypeFactory<> with special data type as template parameter.