Smart pointer wrapping penalty. Memoization with std::map

前端 未结 4 610
无人及你
无人及你 2021-01-05 17:21

I am currently in the middle of a project where performance is of vital importance. Following are some of the questions I had regarding this issue.

Question1

4条回答
  •  说谎
    说谎 (楼主)
    2021-01-05 18:21

    Question 1:

    I use shared pointers in my project extensively, but I wouldn't want to use shared_ptr. It requires a heap object that is allocated separately from T itself, so memory allocation overhead is doubled and memory usage increases by an amount that depends on your runtime library's implementation. intrusive_ptr is more efficient, but there is one key problem that irks me, and that is function calling:

    void Foo(intrusive_ptr x) {...}
    

    every time you call Foo, the reference count of the parameter x must be incremented with a relatively expensive atomic increment, and then decremented on the way out. But this is redundant, because you can usually assume that the caller already has a reference to x, and that the reference is valid for the duration of the call. There are possible ways that the caller might not already have a reference, but it's not hard to write your code in such a way that the caller's reference is always valid.

    Therefore, I prefer to use my own smart pointer class that is the same as intrusive_ptr except that it converts implicitly to and from T*. Then I always declare my methods to take plain pointers, avoiding unnecessary reference counting:

    void Foo(T* x) {...}
    

    This approach has proven to work well in my project, but to be honest I never actually measured the performance difference it makes.

    Also, prefer to use auto_ptr (C++03) or unique_ptr (C++11) where possible.

    Question 2:

    I don't understand why you are thinking about using a std::map. First of all, hash_map will faster (as long as it's not the VC++ Dinkumware implementation in VS2008/2010, details in here somewhere), and secondly if you only need one vector per method, why not use a static variable of type std::vector?

    If you have to look up the vector in a hashtable every time the method is called, my guess is that you will save little or no time compared to creating a new vector each time. If you look up the vector in a std::map, it will take even longer.

提交回复
热议问题