[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <917f49a5-dae4-4810-8116-9bc93ddcd9f5@acm.org>
Date: Fri, 21 Feb 2025 14:13:42 -0800
From: Bart Van Assche <bvanassche@....org>
To: David Laight <david.laight.linux@...il.com>,
Laurent Pinchart <laurent.pinchart@...asonboard.com>
Cc: Jan Engelhardt <ej@...i.de>, "H. Peter Anvin" <hpa@...or.com>,
Greg KH <gregkh@...uxfoundation.org>, Boqun Feng <boqun.feng@...il.com>,
Miguel Ojeda <miguel.ojeda.sandonis@...il.com>,
Christoph Hellwig <hch@...radead.org>,
rust-for-linux <rust-for-linux@...r.kernel.org>,
Linus Torvalds <torvalds@...ux-foundation.org>,
David Airlie <airlied@...il.com>, linux-kernel@...r.kernel.org,
ksummit@...ts.linux.dev
Subject: Re: C aggregate passing (Rust kernel policy)
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.
Powered by blists - more mailing lists