<!--
title: Minimize reference counting overhead
domain: devtools
topic: Performance Optimization
language: Other
source: microsoft/terminal
updated: 2025-08-16
url: https://awesomereviewers.com/reviewers/terminal-minimize-reference-counting-overhead/
-->

Avoid unnecessary reference counting operations that can impact performance, especially in frequently called code paths. Use move semantics, appropriate pointer types, and efficient parameter passing to reduce AddRef/Release churn.

Key strategies:
1. **Use raw pointers when lifetime is guaranteed**: Replace `std::shared_ptr` with raw pointers when the object lifetime is managed by a parent that outlives the consumer
2. **Prefer move semantics for WinRT types**: Take WinRT parameters by value and use `std::move` when assigning to members to avoid extra AddRef/Release cycles
3. **Avoid expensive WinRT collections**: Use standard containers like `std::vector` internally and wrap with `winrt::multi_threaded_vector` only when needed for WinRT interfaces

Example transformations:
```cpp
// Before: Unnecessary shared_ptr overhead
void Constructor(const std::shared_ptr<Cache>& cache) : _cache(cache) {}

// After: Raw pointer when lifetime is guaranteed
void Constructor(Cache* cache) : _cache(cache) {}

// Before: Extra AddRef/Release on assignment  
Constructor(const IVector<T>& items) : _items(items) {}

// After: Move to avoid reference count churn
Constructor(IVector<T> items) : _items(std::move(items)) {}
```

This optimization is particularly important for frequently instantiated objects and hot code paths where reference counting overhead can accumulate significantly.
