Smart Pointer Alternative To shared_ptr

shared_ptr brings convenience to memory management but it also comes at a cost.  It blocks the use of covariant return types.  Also it makes interfacing with other environments difficult, for instance if you wanted to make a C wrapper around your C++ code.  shared_ptr keeps an internal reference count of the object, but if we switch that around and put the reference count inside of the object we could choose when to use smart pointers without losing a consistent count.  That way we can keep our interfaces clean of smart pointers, but use them internally to get their benefit.  Without further ado I present refcountptr with the disclaimer that I wrote it for this post, I’ve only done minimal testing.


 example of usage:


Function Call Benchmarks for Objective-C

Lets run some benchmarks on the different ways to call functions.  Today’s contestants are C Functions, C Function Pointers, C Blocks, C++ Virtual Methods, and Objective-C Methods.

The Code:

The System:

Mac OS X 10.9.3 : LLVM 3.4

Generated Assembly:

c function:

c function pointer:

c block:

c++ virtual method:

objc method:


The Results:

c function – useconds/call:0.004192
c function pointer – useconds/call:0.004705
c block – useconds/call:0.004320
c++ virtual method – useconds/call:0.004731
objc method – useconds/call:0.006549


Objective-C is slow because it is performing more code inside of objc_msgSend but the others are roughly performing the same number of operations.  The only differences are the number of reads from memory they have.