Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 695215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:56:11+00:00 2026-05-14T02:56:11+00:00

I use std::tr1::shared_ptr extensively throughout my application. This includes passing objects in as function

  • 0

I use std::tr1::shared_ptr extensively throughout my application. This includes passing objects in as function arguments. Consider the following:

class Dataset {...}

void f( shared_ptr< Dataset const > pds ) {...}
void g( shared_ptr< Dataset const > pds ) {...}
...

While passing a dataset object around via shared_ptr guarantees its existence inside f and g, the functions may be called millions of times, which causes a lot of shared_ptr objects being created and destroyed. Here’s a snippet of the flat gprof profile from a recent run:

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total
 time   seconds   seconds    calls   s/call   s/call  name
  9.74    295.39    35.12 2451177304     0.00     0.00  std::tr1::__shared_count::__shared_count(std::tr1::__shared_count const&)
  8.03    324.34    28.95 2451252116     0.00     0.00  std::tr1::__shared_count::~__shared_count()

So, ~17% of the runtime was spent on reference counting with shared_ptr objects. Is this normal?

A large portion of my application is single-threaded and I was thinking about re-writing some of the functions as

void f( const Dataset& ds ) {...}

and replacing the calls

shared_ptr< Dataset > pds( new Dataset(...) );
f( pds );

with

f( *pds );

in places where I know for sure the object will not get destroyed while the flow of the program is inside f(). But before I run off to change a bunch of function signatures / calls, I wanted to know what the typical performance hit of passing by shared_ptr was. Seems like shared_ptr should not be used for functions that get called very often.

Any input would be appreciated. Thanks for reading.

-Artem

Update: After changing a handful of functions to accept const Dataset&, the new profile looks like this:

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total
 time   seconds   seconds    calls   s/call   s/call  name
  0.15    241.62     0.37 24981902     0.00     0.00  std::tr1::__shared_count::~__shared_count()
  0.12    241.91     0.30 28342376     0.00     0.00  std::tr1::__shared_count::__shared_count(std::tr1::__shared_count const&)

I’m a little puzzled by the number of destructor calls being smaller than the number of copy constructor calls, but overall I’m very pleased with the decrease in the associated run-time. Thanks to all for their advice.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:56:11+00:00Added an answer on May 14, 2026 at 2:56 am

    Always pass your shared_ptr by const reference:

    void f(const shared_ptr<Dataset const>& pds) {...} 
    void g(const shared_ptr<Dataset const>& pds) {...} 
    

    Edit: Regarding the safety issues mentioned by others:

    • When using shared_ptr heavily throughout an application, passing by value will take up a tremendous amount of time (I’ve seen it go 50+%).
    • Use const T& instead of const shared_ptr<T const>& when the argument shall not be null.
    • Using const shared_ptr<T const>& is safer than const T* when performance is an issue.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i use smart pointers in the following way: typedef std::tr1::shared_ptr<RealAlgebraicNumber> RealAlgebraicNumberPtr; I'm using the
I use shared_ptr with constructing an object like this: std::tr1::shared_ptr<RawClusterBase> rawCluster(new RawClusterBase()); // ...
Is it safe and correct to use an std::tr1::shared_ptr as in the sample code
This question also applies to boost::function and std::tr1::function . std::function is not equality comparable:
How can I use std::shared_ptr for array of double? Additionally what are advantages/disadvantages of
I have these variables: boost::regex re //regular expression to use std::string stringToChange //replace this
I am leaning towards using std::tr1::shared_ptr to automatically manage a pointer to a utility
I was looking into how std::tr1::shared_ptr<> provides the ability to cast to bool. I've
I declare: typedef std::tr1::shared_ptr<ClassA> SharedPtr; And then: std::vector<SharedPtr> mList; And: typedef std::vector<SharedPtr>::iterator ListIterator; The
I am trying to use std::shared_ptr in my code. I have seen there have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.