shared_ptr almost like intrusive_ptr

The current C++11 Standard consists of a very important remark in section 20.9.11.2.6. It is about creation of shared_ptr object. The remarks state:

Implementations are encouraged, but not required, to perform no more than one memory allocation.
[Note: this provides efficiency equivalent to an intrusive smart pointer. end note]

At work [1], I use Visual C++ 2010+ implementation of C++. I couldn’t resist myself to check if the standard managed to encourage Stephan T. Lavavej (STL) and his team at Microsoft to go for this optimisation.

I compiled and run a quick test:


#include <memory>
int main()
{
    std::shared_ptr<int> p0(
       new int(3)); // #1

    std::shared_ptr<int> p1
        = std::make_shared<int>(3); // #2
    return 0;
}

Quick check using breakpoints confirmed that Visual C++ 2010 indeed optimises construction of the shared_ptr. The difference between #1 and #2 is an important one: the second version causes 50% less memory allocations than the first one. Namely, one allocation. std::make_shared packs bookkeeping data and the user-defined data into a single block of memory.

Know your tools or suffer fragmented.

GCC At Home

I couldn’t leave a blog post alone without some kind of unrelated off-topic and pointless digression. So, here is one: Stephan T. Lavavej on his homepage provides custom-built distribution of MinGW toolset with What MinGW Is section starting as follows:

I recommend that anyone who is learning Standard C++ and who uses Windows for a primary development environment should use two compilers: the most modern version of Microsoft Visual C++ (currently 2010 SP1) and the most modern version of GCC, the GNU Compiler Collection.

Stephan works at Microsoft. I admire this kind of professionalism free from strategically competitive marketing b******t. GCC should definitely be included in the list of New Seven Wonders of the World. Recently, a new wonder has emerged: LLVM/Clang. I’m a user of all the three compilers (and related toolsets). It is easy for me to dream about Visual C++Visual Studio IDE based on LLVM/Clang as C++ compiler and shipped with C/C++ standard libraries provided by GCC. That would be a real C++ Renaissance

Productive Saturday Climbing

Today, Pantera and I we hit the Castle Climbing Centre early in the morning. It’s nice to have wide range of routes available when the centre is nearly empty as people are recovering after Friday buzz ;-). I am (exceptionally?) weak today. Probably, it’s because the last 2 weeks I conducted endurance oriented sessions only. First, I jumped upstairs to climb a few V1-V3 boulders on the overhang featured bouldering wall. I tried the new orange V5 boulder. It’s beautiful, but I couldn’t prevent my body swinging a bit after quite dynamic move to 4th hold. I’ve put the V5 on my short list, definitely. Next, we hit number of top-rope routes. Pantera was working on technical 5 and 5+ and I was trying to mix strength and stamina problems:

  • The Slabs: 6b (blue), 2 x 6c (pink/green)
  • Tall Walls: 5 (green), 6b+ (green/orange spots)
  • The Fang: 2 x 6b (orange/lilak spots), 2 x 6c (pink s.o.s., including loss of a few layers of my finger tips skin ;-))
  • The Stack: 6c (red), 7a (yellow, made 1/3 of the route, 3 moves only)

No rock rings, no campus. Finished bouldering a couple of V0-V3 on the Panels.

While my muscles were busy pushing enormous amount of ATP molecules through their fibres and cells, my brain was busy solving design issues of how to make C++ interface of GDAL library better. It is clear that neither dataset nor raster band can have semantic of plain value objects. Both, dataset and raster band, represent real world physical resources and they are more like reference objects. In spite of that reference semantic is dominant in the world of GDAL objects, I’d really like to make them CopyConstructible and Assignable. I know it can be achieved straight away:


std::tr1::shared_ptr<GDALDataset> ds(::GDALOpen("file.tif", GA_ReadOnly), ::GDALClose);

But, I just don’t want to make yet another Gigantic Rats Nest Of Pointers. I would be happy to keep all the ugliness out of user’s sight. I think it’s feasible. Perhaps I will even Pimpl my Ride GDAL.

I’ll probably need one or more climbing sessions to finish the design of better GDAL, so I can start slinging some code around. Next climbing on Monday in Swiss Cottage with Jo, Chiara and Pantera.