C++ Logo

std-proposals

Advanced search

Re: [std-proposals] to_chars_size

From: Thiago Macieira <thiago_at_[hidden]>
Date: Mon, 04 Mar 2024 17:21:11 -0800
On Monday, 4 March 2024 16:21:55 PST Jan Schultke via Std-Proposals wrote:
> You can over-allocate, as you've said in Motivation. Is
> over-allocating so costly that this approach becomes preferable? What
> is the use case here, really? What about over-allocating, performing
> all the conversions without checking for size, then re-allocating? If
> to_chars_size is half the cost of to_chars for floating-point,
> couldn't this be cheaper? After all, copying some memory shouldn't be
> that costly. This needs some benchmarks and deeper discussion.

And usually, shrinking a memory block via realloc() is a very fast O(1)
operation (less than 10 ns).

std::string can't shrink using realloc() because that API is missing from
std::allocator. That's a flaw/limitation that ought to be fixed there, though.
QString can shrink and even grow using realloc().

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel DCAI Cloud Engineering

Received on 2024-03-05 01:21:14