Re: C aggregate passing (Rust kernel policy)

Bart Van Assche
2025-02-21T22:17:35Z

On 2/21/25 2:02 PM, David Laight wrote:
> And there is nothing to stop (I think even std::string) using ref-counted
> buffers for large malloc()ed strings.

This is what an LLM told me about this topic (this matches what I
remember about the std::string implementation):

<quote>
Does the std::string implementation use a reference count?

No. [ ... ]

Why does std::string not use a reference count? Has this always been the
case?

[ ... ]
Reference counting adds overhead. Every time a string is copied or
assigned, the reference count has to be incremented or decremented, and
when it reaches zero, memory has to be deallocated. This adds both time
complexity (due to the need to update the reference count) and space
complexity (to store the count alongside the string data).

The goal with std::string is to minimize this overhead as much as
possible for the most common cases, particularly short strings, which
are frequent in real-world applications. The small string optimization
(SSO) allows short strings to be stored directly within the std::string
object itself, avoiding heap allocation and reference counting
altogether. For long strings, reference counting might not provide much
of an advantage anyway because memory management would still have to
involve the heap.
[ ... ]
Reference counting introduces unpredictable performance in terms of
memory management, especially in multithreaded applications. Each string
operation might require atomic operations on the reference count,
leading to potential contention in multithreaded environments.
[ ... ]
Initially, early implementations of std::string may have used CoW or
reference counting techniques. However, over time, as the language
evolved and as multithreading and performance became more of a priority,
the C++ standard moved away from these features. Notably, the C++11
standard explicitly banned CoW for std::string in order to avoid its
pitfalls.
[ ... ]
</quote>

Bart.