Weak Pointers with std::weak_ptr

Performance Impact of Weak Pointers

Do weak pointers have any performance overhead compared to raw pointers?

Illustration representing computer hardware

Weak pointers do have a slight performance overhead compared to raw pointers due to the additional bookkeeping they perform. However, in most cases, this overhead is negligible and outweighed by the benefits they provide in terms of safety and expressive power.

When you create a weak pointer, it internally maintains a reference to a control block shared with the corresponding shared pointers. This control block keeps track of the reference counts and manages the object's lifetime. Operations like creating, copying, and destroying weak pointers involve atomic operations on the reference counts, which can have a small performance cost.

However, accessing the pointed-to object through a weak pointer (by creating a shared pointer using lock()) has minimal overhead compared to directly using a raw pointer. The main cost is the atomic operation to check and increment the reference count when locking the weak pointer.

Consider the following example:

#include <memory>
#include <iostream>

class MyClass {
public:
  void DoSomething() {
    std::cout << "Doing something";
  }
};

void ProcessObject(std::weak_ptr<
  MyClass> WeakPtr) {
  if (auto SharedPtr{WeakPtr.lock()}) {
    SharedPtr->DoSomething();
  }
}

int main() {
  auto Object{std::make_shared<MyClass>()};
  ProcessObject(Object);
}
Doing something

In this case, the overhead of using a weak pointer in ProcessObject is minimal. The lock() operation checks the reference count atomically, and if the object still exists, it returns a shared_ptr that can be used to access the object efficiently.

In most scenarios, the performance impact of using weak pointers is negligible compared to the benefits they provide in terms of safety, expressiveness, and avoiding resource leaks. However, if you have extremely performance-critical code and have verified that weak pointers are a bottleneck, you may consider using raw pointers with careful manual lifetime management in those specific cases.

This Question is from the Lesson:

Weak Pointers with std::weak_ptr

A full guide to weak pointers, using std::weak_ptr. Learn what they’re for, and how we can use them with practical examples

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

Weak Pointers with std::weak_ptr

A full guide to weak pointers, using std::weak_ptr. Learn what they’re for, and how we can use them with practical examples

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 125 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved