llvm / llvm/llvm-project

[libc++] Track "never weakly referenced" in the shared_ptr control block

Open
#216,007 3 comments 0 reactions 0 assignees View on GitHub
libc++ performance
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

This tracks an optimization idea that was discussed with @philnik777 a while ago, to make sure it doesn't fall between the cracks.

Destroying the last `shared_ptr` of an object that was never weakly referenced touches two counters of the control block:

1. atomic decrement of `__shared_owners_`
2. virtual call to `__on_zero_shared()`, which destroys the object
3. call `__release_weak()`, which is not inlinable (only defined in the dylib)
4. atomic load of `__shared_weak_owners_`, the second counter
5. virtual call to `__on_zero_shared_weak()`, which frees the control block

In the common case where we don't have any weak pointers, steps (3), (4) and (5) could be folded into step (2). This could be done by e.g. recording whether there are any weak references in the shared count, and if not, then take the fast path.

The simplest way to do this might be to stick a flag bit in `__shared_owners_` to represent whether it was ever referenced as weak. The moment a weak ref is created, that bit is set and will remain so for the whole life of the shared pointer (unsetting the flag bit would race with the actual weak count).

Contributor guide

Open the contributing guide

Research direction

Read the libc++ shared_ptr control-block ownership and weak-reference paths, focusing on __shared_owners_, __shared_weak_owners_, __release_weak(), and weak-reference creation. Done means a validated fast path for control blocks never weakly referenced, with correct concurrent behavior and performance evidence or tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers, performance
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.