[libcxx-commits] [libcxx] Improve string allocation (PR #92652)

Valery Mironov via libcxx-commits libcxx-commits at lists.llvm.org
Sat May 18 08:07:29 PDT 2024


MBkkt wrote:

@philnik777 

> Could you expand a bit on why you think these are unnecessary? All of these checks look like they're required for correctness according to the standard or to keep string-internal invariants.

### Change 1
```cpp
    if (__s < __min_cap) {
      return static_cast<size_type>(__min_cap) - 1;
    }
```

This is unnecessary because `__recommend` always called when `__s` doesn't fit to sso.
It's not required by standard because part of implementation

### Change 2
```cpp
  if (__requested_capacity <= capacity())
    return;

  size_type __target_capacity = std::max(__requested_capacity, size());
  __target_capacity           = __recommend(__target_capacity);
```

`size_type __target_capacity = std::max(__requested_capacity, size());` is unnecessary because `size() <= capacity()`.
It's not required by standard because part of implementation.


### Change 3

```cpp
    if (__guess == __min_cap)
      __guess += __endian_factor;
```

I'm not sure about best option, but current code is definitely not the best option 

When string user requested 23/11/5 characters (char, char16_t, char32_t), we need only 24 bytes from allocator, without reason now we requesting 25/26 bytes (depends on endianness)

This change makes it request 24 bytes in such cases


https://github.com/llvm/llvm-project/pull/92652


More information about the libcxx-commits mailing list